1f8829a4aSRandall Stewart /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4b1006367SRandall Stewart * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7f8829a4aSRandall Stewart *
8f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without
9f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met:
10f8829a4aSRandall Stewart *
11f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice,
12f8829a4aSRandall Stewart * this list of conditions and the following disclaimer.
13f8829a4aSRandall Stewart *
14f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright
15f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in
16f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution.
17f8829a4aSRandall Stewart *
18f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its
19f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived
20f8829a4aSRandall Stewart * from this software without specific prior written permission.
21f8829a4aSRandall Stewart *
22f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE.
33f8829a4aSRandall Stewart */
34b48287a3SDavid E. O'Brien
35f8829a4aSRandall Stewart #include <netinet/sctp_os.h>
365e20b91dSMichael Tuexen #ifdef INET6
3793164cf9SRandall Stewart #include <sys/proc.h>
38f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
39f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
41c105859eSRandall Stewart #include <netinet6/sctp6_var.h>
4242551e99SRandall Stewart #include <netinet/sctp_sysctl.h>
43f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
4493164cf9SRandall Stewart #include <netinet/sctp_uio.h>
4593164cf9SRandall Stewart #include <netinet/sctp_asconf.h>
4693164cf9SRandall Stewart #include <netinet/sctputil.h>
4793164cf9SRandall Stewart #include <netinet/sctp_indata.h>
4893164cf9SRandall Stewart #include <netinet/sctp_timer.h>
4993164cf9SRandall Stewart #include <netinet/sctp_auth.h>
50c105859eSRandall Stewart #include <netinet/sctp_input.h>
51c105859eSRandall Stewart #include <netinet/sctp_output.h>
52207304d4SRandall Stewart #include <netinet/sctp_bsd_addr.h>
53a99b6783SRandall Stewart #include <netinet/sctp_crc32.h>
544ff815b7SMichael Tuexen #include <netinet/icmp6.h>
55b3f1ea41SRandall Stewart #include <netinet/udp.h>
56f8829a4aSRandall Stewart
57f8829a4aSRandall Stewart int
sctp6_input_with_port(struct mbuf ** i_pak,int * offp,uint16_t port)583a51a264SMichael Tuexen sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
59f8829a4aSRandall Stewart {
60139bc87fSRandall Stewart struct mbuf *m;
616dc5aabcSMichael Tuexen int iphlen;
62a8775ad9SMichael Tuexen uint32_t vrf_id;
636dc5aabcSMichael Tuexen uint8_t ecn_bits;
64b1754ad1SMichael Tuexen struct sockaddr_in6 src, dst;
65f8829a4aSRandall Stewart struct ip6_hdr *ip6;
66f8829a4aSRandall Stewart struct sctphdr *sh;
67f8829a4aSRandall Stewart struct sctp_chunkhdr *ch;
686dc5aabcSMichael Tuexen int length, offset;
69a8775ad9SMichael Tuexen uint8_t compute_crc;
70a8775ad9SMichael Tuexen uint32_t mflowid;
71457b4b88SMichael Tuexen uint8_t mflowtype;
72d089f9b9SMichael Tuexen uint16_t fibnum;
73f8829a4aSRandall Stewart
746dc5aabcSMichael Tuexen iphlen = *offp;
7517205eccSRandall Stewart if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
7617205eccSRandall Stewart SCTP_RELEASE_PKT(*i_pak);
776dc5aabcSMichael Tuexen return (IPPROTO_DONE);
7817205eccSRandall Stewart }
79c105859eSRandall Stewart m = SCTP_HEADER_TO_CHAIN(*i_pak);
806dc5aabcSMichael Tuexen #ifdef SCTP_MBUF_LOGGING
816dc5aabcSMichael Tuexen /* Log in any input mbufs */
826dc5aabcSMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
834be807c4SMichael Tuexen sctp_log_mbc(m, SCTP_MBUF_INPUT);
846dc5aabcSMichael Tuexen }
856dc5aabcSMichael Tuexen #endif
86207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
87f9384252SMichael Tuexen if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
88f9384252SMichael Tuexen sctp_packet_log(m);
89f9384252SMichael Tuexen }
90207304d4SRandall Stewart #endif
91a8775ad9SMichael Tuexen SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
921a94cdbeSMichael Tuexen "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
93a8775ad9SMichael Tuexen m->m_pkthdr.len,
94a8775ad9SMichael Tuexen if_name(m->m_pkthdr.rcvif),
951a94cdbeSMichael Tuexen (int)m->m_pkthdr.csum_flags, CSUM_BITS);
96f30ac432SMichael Tuexen mflowid = m->m_pkthdr.flowid;
97457b4b88SMichael Tuexen mflowtype = M_HASHTYPE_GET(m);
98d089f9b9SMichael Tuexen fibnum = M_GETFIB(m);
996dc5aabcSMichael Tuexen SCTP_STAT_INCR(sctps_recvpackets);
1006dc5aabcSMichael Tuexen SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
1016dc5aabcSMichael Tuexen /* Get IP, SCTP, and first chunk header together in the first mbuf. */
1026dc5aabcSMichael Tuexen offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
103a4adf6ccSBjoern A. Zeeb if (m->m_len < offset) {
10463abacc2SBjoern A. Zeeb m = m_pullup(m, offset);
10563abacc2SBjoern A. Zeeb if (m == NULL) {
106f8829a4aSRandall Stewart SCTP_STAT_INCR(sctps_hdrops);
10760990c0cSMichael Tuexen return (IPPROTO_DONE);
108f8829a4aSRandall Stewart }
109a4adf6ccSBjoern A. Zeeb }
11063abacc2SBjoern A. Zeeb ip6 = mtod(m, struct ip6_hdr *);
11163abacc2SBjoern A. Zeeb sh = (struct sctphdr *)(mtod(m, caddr_t)+iphlen);
112f8829a4aSRandall Stewart ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
1136dc5aabcSMichael Tuexen offset -= sizeof(struct sctp_chunkhdr);
114b1754ad1SMichael Tuexen memset(&src, 0, sizeof(struct sockaddr_in6));
115b1754ad1SMichael Tuexen src.sin6_family = AF_INET6;
116b1754ad1SMichael Tuexen src.sin6_len = sizeof(struct sockaddr_in6);
117b1754ad1SMichael Tuexen src.sin6_port = sh->src_port;
118b1754ad1SMichael Tuexen src.sin6_addr = ip6->ip6_src;
119b1754ad1SMichael Tuexen if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
120a8775ad9SMichael Tuexen goto out;
121b1754ad1SMichael Tuexen }
122b1754ad1SMichael Tuexen memset(&dst, 0, sizeof(struct sockaddr_in6));
123b1754ad1SMichael Tuexen dst.sin6_family = AF_INET6;
124b1754ad1SMichael Tuexen dst.sin6_len = sizeof(struct sockaddr_in6);
125b1754ad1SMichael Tuexen dst.sin6_port = sh->dest_port;
126b1754ad1SMichael Tuexen dst.sin6_addr = ip6->ip6_dst;
127b1754ad1SMichael Tuexen if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
128a8775ad9SMichael Tuexen goto out;
129b1754ad1SMichael Tuexen }
1306dc5aabcSMichael Tuexen length = ntohs(ip6->ip6_plen) + iphlen;
1316dc5aabcSMichael Tuexen /* Validate mbuf chain length with IP payload length. */
132a8775ad9SMichael Tuexen if (SCTP_HEADER_LEN(m) != length) {
1336dc5aabcSMichael Tuexen SCTPDBG(SCTP_DEBUG_INPUT1,
134a8775ad9SMichael Tuexen "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
1356dc5aabcSMichael Tuexen SCTP_STAT_INCR(sctps_hdrops);
136a8775ad9SMichael Tuexen goto out;
137f8829a4aSRandall Stewart }
1386dc5aabcSMichael Tuexen if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
139a8775ad9SMichael Tuexen goto out;
1406dc5aabcSMichael Tuexen }
141bb4a7d94SKristof Provost ecn_bits = IPV6_TRAFFIC_CLASS(ip6);
142a99b6783SRandall Stewart if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
143a99b6783SRandall Stewart SCTP_STAT_INCR(sctps_recvhwcrc);
144a8775ad9SMichael Tuexen compute_crc = 0;
145a8775ad9SMichael Tuexen } else {
146a99b6783SRandall Stewart SCTP_STAT_INCR(sctps_recvswcrc);
147a8775ad9SMichael Tuexen compute_crc = 1;
148f8829a4aSRandall Stewart }
149b1754ad1SMichael Tuexen sctp_common_input_processing(&m, iphlen, offset, length,
150b1754ad1SMichael Tuexen (struct sockaddr *)&src,
151b1754ad1SMichael Tuexen (struct sockaddr *)&dst,
152a8775ad9SMichael Tuexen sh, ch,
153a8775ad9SMichael Tuexen compute_crc,
154a8775ad9SMichael Tuexen ecn_bits,
155d089f9b9SMichael Tuexen mflowtype, mflowid, fibnum,
156f30ac432SMichael Tuexen vrf_id, port);
157a8775ad9SMichael Tuexen out:
1586dc5aabcSMichael Tuexen if (m) {
15917205eccSRandall Stewart sctp_m_freem(m);
1606dc5aabcSMichael Tuexen }
16160990c0cSMichael Tuexen return (IPPROTO_DONE);
162f8829a4aSRandall Stewart }
163f8829a4aSRandall Stewart
1643a51a264SMichael Tuexen int
sctp6_input(struct mbuf ** i_pak,int * offp,int proto SCTP_UNUSED)1653a51a264SMichael Tuexen sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
1663a51a264SMichael Tuexen {
1673a51a264SMichael Tuexen return (sctp6_input_with_port(i_pak, offp, 0));
1683a51a264SMichael Tuexen }
1693a51a264SMichael Tuexen
17004ee05e8SRandall Stewart void
sctp6_notify(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net,uint8_t icmp6_type,uint8_t icmp6_code,uint32_t next_mtu)171851b7298SRandall Stewart sctp6_notify(struct sctp_inpcb *inp,
172851b7298SRandall Stewart struct sctp_tcb *stcb,
173b9dd6a90SMichael Tuexen struct sctp_nets *net,
174b9dd6a90SMichael Tuexen uint8_t icmp6_type,
175b9dd6a90SMichael Tuexen uint8_t icmp6_code,
1766ebfa5eeSMichael Tuexen uint32_t next_mtu)
177851b7298SRandall Stewart {
178b9dd6a90SMichael Tuexen int timer_stopped;
179a2b42326SMichael Tuexen
180b9dd6a90SMichael Tuexen switch (icmp6_type) {
181b9dd6a90SMichael Tuexen case ICMP6_DST_UNREACH:
182b9dd6a90SMichael Tuexen if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
183b9dd6a90SMichael Tuexen (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
184b9dd6a90SMichael Tuexen (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
185b9dd6a90SMichael Tuexen (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
186b9dd6a90SMichael Tuexen /* Mark the net unreachable. */
187851b7298SRandall Stewart if (net->dest_state & SCTP_ADDR_REACHABLE) {
188b9dd6a90SMichael Tuexen /* Ok that destination is not reachable */
189851b7298SRandall Stewart net->dest_state &= ~SCTP_ADDR_REACHABLE;
190851b7298SRandall Stewart net->dest_state &= ~SCTP_ADDR_PF;
191851b7298SRandall Stewart sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
1921a5b7901SMichael Tuexen stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
193851b7298SRandall Stewart }
194b9dd6a90SMichael Tuexen }
195851b7298SRandall Stewart SCTP_TCB_UNLOCK(stcb);
196b9dd6a90SMichael Tuexen break;
197b9dd6a90SMichael Tuexen case ICMP6_PARAM_PROB:
198b9dd6a90SMichael Tuexen /* Treat it like an ABORT. */
199b9dd6a90SMichael Tuexen if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
200105b68b4SMichael Tuexen sctp_abort_notification(stcb, true, false, 0, NULL, SCTP_SO_NOT_LOCKED);
201b7d130beSMichael Tuexen (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
202b9dd6a90SMichael Tuexen SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
203851b7298SRandall Stewart } else {
204851b7298SRandall Stewart SCTP_TCB_UNLOCK(stcb);
205851b7298SRandall Stewart }
206b9dd6a90SMichael Tuexen break;
207b9dd6a90SMichael Tuexen case ICMP6_PACKET_TOO_BIG:
208efcf28efSMichael Tuexen if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
20928a6addeSMichael Tuexen SCTP_TCB_UNLOCK(stcb);
21028a6addeSMichael Tuexen break;
21128a6addeSMichael Tuexen }
212b9dd6a90SMichael Tuexen if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
213b9dd6a90SMichael Tuexen timer_stopped = 1;
214b9dd6a90SMichael Tuexen sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
215b9dd6a90SMichael Tuexen SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
216b9dd6a90SMichael Tuexen } else {
217b9dd6a90SMichael Tuexen timer_stopped = 0;
218851b7298SRandall Stewart }
219b9dd6a90SMichael Tuexen /* Update the path MTU. */
220d274bcc6SMichael Tuexen if (net->port) {
221d274bcc6SMichael Tuexen next_mtu -= sizeof(struct udphdr);
222d274bcc6SMichael Tuexen }
223b9dd6a90SMichael Tuexen if (net->mtu > next_mtu) {
224b9dd6a90SMichael Tuexen net->mtu = next_mtu;
225e25b0dabSMichael Tuexen if (net->port) {
226e25b0dabSMichael Tuexen sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
227e25b0dabSMichael Tuexen } else {
228e25b0dabSMichael Tuexen sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
229e25b0dabSMichael Tuexen }
230b9dd6a90SMichael Tuexen }
231b9dd6a90SMichael Tuexen /* Update the association MTU */
232b9dd6a90SMichael Tuexen if (stcb->asoc.smallest_mtu > next_mtu) {
2332de2ae33SMichael Tuexen sctp_pathmtu_adjustment(stcb, next_mtu, true);
234b9dd6a90SMichael Tuexen }
235b9dd6a90SMichael Tuexen /* Finally, start the PMTU timer if it was running before. */
236b9dd6a90SMichael Tuexen if (timer_stopped) {
237b9dd6a90SMichael Tuexen sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
238b9dd6a90SMichael Tuexen }
239b9dd6a90SMichael Tuexen SCTP_TCB_UNLOCK(stcb);
240b9dd6a90SMichael Tuexen break;
241b9dd6a90SMichael Tuexen default:
242b9dd6a90SMichael Tuexen SCTP_TCB_UNLOCK(stcb);
243b9dd6a90SMichael Tuexen break;
244b9dd6a90SMichael Tuexen }
245b9dd6a90SMichael Tuexen }
246851b7298SRandall Stewart
247f8829a4aSRandall Stewart void
sctp6_ctlinput(struct ip6ctlparam * ip6cp)248fcb3f813SGleb Smirnoff sctp6_ctlinput(struct ip6ctlparam *ip6cp)
249f8829a4aSRandall Stewart {
250b9dd6a90SMichael Tuexen struct sctp_inpcb *inp;
251b9dd6a90SMichael Tuexen struct sctp_tcb *stcb;
252b9dd6a90SMichael Tuexen struct sctp_nets *net;
253f8829a4aSRandall Stewart struct sctphdr sh;
254b9dd6a90SMichael Tuexen struct sockaddr_in6 src, dst;
25542551e99SRandall Stewart
256f83db644SMichael Tuexen if (icmp6_errmap(ip6cp->ip6c_icmp6) == 0) {
257f8829a4aSRandall Stewart return;
258f83db644SMichael Tuexen }
259f8829a4aSRandall Stewart
260b9dd6a90SMichael Tuexen /*
261f83db644SMichael Tuexen * Check if we can safely examine the ports and the verification tag
262f83db644SMichael Tuexen * of the SCTP common header.
263b9dd6a90SMichael Tuexen */
264b9dd6a90SMichael Tuexen if (ip6cp->ip6c_m->m_pkthdr.len <
265b1deed45SMichael Tuexen (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
26647979581SGleb Smirnoff return;
267b9dd6a90SMichael Tuexen }
2680053ed28SMichael Tuexen
269b9dd6a90SMichael Tuexen /* Copy out the port numbers and the verification tag. */
2705ba7f91fSMichael Tuexen memset(&sh, 0, sizeof(sh));
271b9dd6a90SMichael Tuexen m_copydata(ip6cp->ip6c_m,
272b9dd6a90SMichael Tuexen ip6cp->ip6c_off,
273b9dd6a90SMichael Tuexen sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
274b9dd6a90SMichael Tuexen (caddr_t)&sh);
275b9dd6a90SMichael Tuexen memset(&src, 0, sizeof(struct sockaddr_in6));
276b9dd6a90SMichael Tuexen src.sin6_family = AF_INET6;
277b9dd6a90SMichael Tuexen src.sin6_len = sizeof(struct sockaddr_in6);
278b9dd6a90SMichael Tuexen src.sin6_port = sh.src_port;
279b9dd6a90SMichael Tuexen src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
280b9dd6a90SMichael Tuexen if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
281b9dd6a90SMichael Tuexen return;
282b9dd6a90SMichael Tuexen }
283b9dd6a90SMichael Tuexen memset(&dst, 0, sizeof(struct sockaddr_in6));
284b9dd6a90SMichael Tuexen dst.sin6_family = AF_INET6;
285b9dd6a90SMichael Tuexen dst.sin6_len = sizeof(struct sockaddr_in6);
286b9dd6a90SMichael Tuexen dst.sin6_port = sh.dest_port;
287b9dd6a90SMichael Tuexen dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
288b9dd6a90SMichael Tuexen if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
289b9dd6a90SMichael Tuexen return;
290b9dd6a90SMichael Tuexen }
291f8829a4aSRandall Stewart inp = NULL;
292f8829a4aSRandall Stewart net = NULL;
293b9dd6a90SMichael Tuexen stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
294b9dd6a90SMichael Tuexen (struct sockaddr *)&src,
295b9dd6a90SMichael Tuexen &inp, &net, 1, SCTP_DEFAULT_VRFID);
296b9dd6a90SMichael Tuexen if ((stcb != NULL) &&
297b9dd6a90SMichael Tuexen (net != NULL) &&
29855b8cd93SMichael Tuexen (inp != NULL)) {
299b9dd6a90SMichael Tuexen /* Check the verification tag */
300b9dd6a90SMichael Tuexen if (ntohl(sh.v_tag) != 0) {
301b9dd6a90SMichael Tuexen /*
302f83db644SMichael Tuexen * This must be the verification tag used for
303f83db644SMichael Tuexen * sending out packets. We don't consider packets
304f83db644SMichael Tuexen * reflecting the verification tag.
305b9dd6a90SMichael Tuexen */
306b9dd6a90SMichael Tuexen if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
307b9dd6a90SMichael Tuexen SCTP_TCB_UNLOCK(stcb);
308b9dd6a90SMichael Tuexen return;
309f8829a4aSRandall Stewart }
310f8829a4aSRandall Stewart } else {
311b9dd6a90SMichael Tuexen if (ip6cp->ip6c_m->m_pkthdr.len >=
312b9dd6a90SMichael Tuexen ip6cp->ip6c_off + sizeof(struct sctphdr) +
313b9dd6a90SMichael Tuexen sizeof(struct sctp_chunkhdr) +
314b9dd6a90SMichael Tuexen offsetof(struct sctp_init, a_rwnd)) {
315b9dd6a90SMichael Tuexen /*
316f83db644SMichael Tuexen * In this case we can check if we got an
317f83db644SMichael Tuexen * INIT chunk and if the initiate tag
318f83db644SMichael Tuexen * matches.
319b9dd6a90SMichael Tuexen */
320b9dd6a90SMichael Tuexen uint32_t initiate_tag;
321b9dd6a90SMichael Tuexen uint8_t chunk_type;
322b9dd6a90SMichael Tuexen
323b9dd6a90SMichael Tuexen m_copydata(ip6cp->ip6c_m,
324b9dd6a90SMichael Tuexen ip6cp->ip6c_off +
325b9dd6a90SMichael Tuexen sizeof(struct sctphdr),
326b9dd6a90SMichael Tuexen sizeof(uint8_t),
327b9dd6a90SMichael Tuexen (caddr_t)&chunk_type);
328b9dd6a90SMichael Tuexen m_copydata(ip6cp->ip6c_m,
329b9dd6a90SMichael Tuexen ip6cp->ip6c_off +
330b9dd6a90SMichael Tuexen sizeof(struct sctphdr) +
331b9dd6a90SMichael Tuexen sizeof(struct sctp_chunkhdr),
332b9dd6a90SMichael Tuexen sizeof(uint32_t),
333b9dd6a90SMichael Tuexen (caddr_t)&initiate_tag);
334b9dd6a90SMichael Tuexen if ((chunk_type != SCTP_INITIATION) ||
335b9dd6a90SMichael Tuexen (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
336b9dd6a90SMichael Tuexen SCTP_TCB_UNLOCK(stcb);
337b9dd6a90SMichael Tuexen return;
338f8829a4aSRandall Stewart }
339b9dd6a90SMichael Tuexen } else {
340b9dd6a90SMichael Tuexen SCTP_TCB_UNLOCK(stcb);
341b9dd6a90SMichael Tuexen return;
342b9dd6a90SMichael Tuexen }
343b9dd6a90SMichael Tuexen }
344b9dd6a90SMichael Tuexen sctp6_notify(inp, stcb, net,
345b9dd6a90SMichael Tuexen ip6cp->ip6c_icmp6->icmp6_type,
346b9dd6a90SMichael Tuexen ip6cp->ip6c_icmp6->icmp6_code,
3476ebfa5eeSMichael Tuexen ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
348b9dd6a90SMichael Tuexen } else {
349b9dd6a90SMichael Tuexen if ((stcb == NULL) && (inp != NULL)) {
350f8829a4aSRandall Stewart /* reduce inp's ref-count */
351f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp);
352f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp);
353f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp);
354f8829a4aSRandall Stewart }
355b9dd6a90SMichael Tuexen if (stcb) {
356f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb);
357f8829a4aSRandall Stewart }
358f8829a4aSRandall Stewart }
359f8829a4aSRandall Stewart }
360f8829a4aSRandall Stewart
361f8829a4aSRandall Stewart /*
362*58c99e07SGordon Bergling * this routine can probably be collapsed into the one in sctp_userreq.c
363f8829a4aSRandall Stewart * since they do the same thing and now we lookup with a sockaddr
364f8829a4aSRandall Stewart */
365f8829a4aSRandall Stewart static int
sctp6_getcred(SYSCTL_HANDLER_ARGS)366f8829a4aSRandall Stewart sctp6_getcred(SYSCTL_HANDLER_ARGS)
367f8829a4aSRandall Stewart {
36803b0b021SRandall Stewart struct xucred xuc;
369f8829a4aSRandall Stewart struct sockaddr_in6 addrs[2];
370f8829a4aSRandall Stewart struct sctp_inpcb *inp;
371f8829a4aSRandall Stewart struct sctp_nets *net;
372f8829a4aSRandall Stewart struct sctp_tcb *stcb;
37303b0b021SRandall Stewart int error;
37442551e99SRandall Stewart uint32_t vrf_id;
37542551e99SRandall Stewart
37642551e99SRandall Stewart vrf_id = SCTP_DEFAULT_VRFID;
377f8829a4aSRandall Stewart
37832f9753cSRobert Watson error = priv_check(req->td, PRIV_NETINET_GETCRED);
379f8829a4aSRandall Stewart if (error)
380f8829a4aSRandall Stewart return (error);
381f8829a4aSRandall Stewart
382c4739e2fSRandall Stewart if (req->newlen != sizeof(addrs)) {
383c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
384f8829a4aSRandall Stewart return (EINVAL);
385c4739e2fSRandall Stewart }
386c4739e2fSRandall Stewart if (req->oldlen != sizeof(struct ucred)) {
387c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
388f8829a4aSRandall Stewart return (EINVAL);
389c4739e2fSRandall Stewart }
390f8829a4aSRandall Stewart error = SYSCTL_IN(req, addrs, sizeof(addrs));
391f8829a4aSRandall Stewart if (error)
392f8829a4aSRandall Stewart return (error);
393f8829a4aSRandall Stewart
394b1754ad1SMichael Tuexen stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
395b1754ad1SMichael Tuexen sin6tosa(&addrs[0]),
39642551e99SRandall Stewart &inp, &net, 1, vrf_id);
397f8829a4aSRandall Stewart if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
39803b0b021SRandall Stewart if ((inp != NULL) && (stcb == NULL)) {
39903b0b021SRandall Stewart /* reduce ref-count */
400f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp);
401f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp);
40203b0b021SRandall Stewart goto cred_can_cont;
403f8829a4aSRandall Stewart }
404c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
40503b0b021SRandall Stewart error = ENOENT;
406f8829a4aSRandall Stewart goto out;
407f8829a4aSRandall Stewart }
408f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb);
40903b0b021SRandall Stewart /*
41003b0b021SRandall Stewart * We use the write lock here, only since in the error leg we need
41103b0b021SRandall Stewart * it. If we used RLOCK, then we would have to
41203b0b021SRandall Stewart * wlock/decr/unlock/rlock. Which in theory could create a hole.
41303b0b021SRandall Stewart * Better to use higher wlock.
41403b0b021SRandall Stewart */
41503b0b021SRandall Stewart SCTP_INP_WLOCK(inp);
41603b0b021SRandall Stewart cred_can_cont:
41703b0b021SRandall Stewart error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
41803b0b021SRandall Stewart if (error) {
41903b0b021SRandall Stewart SCTP_INP_WUNLOCK(inp);
42003b0b021SRandall Stewart goto out;
42103b0b021SRandall Stewart }
42203b0b021SRandall Stewart cru2x(inp->sctp_socket->so_cred, &xuc);
42303b0b021SRandall Stewart SCTP_INP_WUNLOCK(inp);
42403b0b021SRandall Stewart error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
425f8829a4aSRandall Stewart out:
426f8829a4aSRandall Stewart return (error);
427f8829a4aSRandall Stewart }
428f8829a4aSRandall Stewart
429295a18d1SPawel Biernacki SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred,
430295a18d1SPawel Biernacki CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
431295a18d1SPawel Biernacki 0, 0, sctp6_getcred, "S,ucred",
432295a18d1SPawel Biernacki "Get the ucred of a SCTP6 connection");
433f8829a4aSRandall Stewart
434f8829a4aSRandall Stewart static int
sctp6_attach(struct socket * so,int proto SCTP_UNUSED,struct thread * p SCTP_UNUSED)4357215cc1bSMichael Tuexen sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
436f8829a4aSRandall Stewart {
43793164cf9SRandall Stewart int error;
438f8829a4aSRandall Stewart struct sctp_inpcb *inp;
439f4c93d24SRandall Stewart uint32_t vrf_id = SCTP_DEFAULT_VRFID;
440f8829a4aSRandall Stewart
441f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb;
442c4739e2fSRandall Stewart if (inp != NULL) {
443c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
44460990c0cSMichael Tuexen return (EINVAL);
445c4739e2fSRandall Stewart }
4460053ed28SMichael Tuexen
447f8829a4aSRandall Stewart if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
448b3f1ea41SRandall Stewart error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
449f8829a4aSRandall Stewart if (error)
45060990c0cSMichael Tuexen return (error);
451f8829a4aSRandall Stewart }
452f4c93d24SRandall Stewart error = sctp_inpcb_alloc(so, vrf_id);
453f8829a4aSRandall Stewart if (error)
45460990c0cSMichael Tuexen return (error);
455f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb;
456f4c93d24SRandall Stewart SCTP_INP_WLOCK(inp);
457f8829a4aSRandall Stewart inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
458f8829a4aSRandall Stewart
4590ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4600ecd976eSBjoern A. Zeeb inp->ip_inp.inp.in6p_hops = -1; /* use kernel default */
4610ecd976eSBjoern A. Zeeb inp->ip_inp.inp.in6p_cksum = -1; /* just to be sure */
462f8829a4aSRandall Stewart #ifdef INET
463f8829a4aSRandall Stewart /*
464f8829a4aSRandall Stewart * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
465f8829a4aSRandall Stewart * socket as well, because the socket may be bound to an IPv6
466f8829a4aSRandall Stewart * wildcard address, which may match an IPv4-mapped IPv6 address.
467f8829a4aSRandall Stewart */
4680ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
469f8829a4aSRandall Stewart #endif
470f4c93d24SRandall Stewart SCTP_INP_WUNLOCK(inp);
47160990c0cSMichael Tuexen return (0);
472f8829a4aSRandall Stewart }
473f8829a4aSRandall Stewart
474f8829a4aSRandall Stewart static int
sctp6_bind(struct socket * so,struct sockaddr * addr,struct thread * p)475f8829a4aSRandall Stewart sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
476f8829a4aSRandall Stewart {
477f8829a4aSRandall Stewart struct sctp_inpcb *inp;
47893164cf9SRandall Stewart int error;
4794a91aa8fSMichael Tuexen u_char vflagsav;
480f8829a4aSRandall Stewart
481f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb;
482dea47f39SMichael Tuexen if (inp == NULL) {
483c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
48460990c0cSMichael Tuexen return (EINVAL);
485c4739e2fSRandall Stewart }
4860053ed28SMichael Tuexen
487d61a0ae0SRandall Stewart if (addr) {
488e6194c2eSMichael Tuexen switch (addr->sa_family) {
489e6194c2eSMichael Tuexen #ifdef INET
490e6194c2eSMichael Tuexen case AF_INET:
491e6194c2eSMichael Tuexen if (addr->sa_len != sizeof(struct sockaddr_in)) {
492c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
49360990c0cSMichael Tuexen return (EINVAL);
494d61a0ae0SRandall Stewart }
495e6194c2eSMichael Tuexen break;
496e6194c2eSMichael Tuexen #endif
497e6194c2eSMichael Tuexen #ifdef INET6
498e6194c2eSMichael Tuexen case AF_INET6:
499e6194c2eSMichael Tuexen if (addr->sa_len != sizeof(struct sockaddr_in6)) {
500e6194c2eSMichael Tuexen SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
50160990c0cSMichael Tuexen return (EINVAL);
502e6194c2eSMichael Tuexen }
503e6194c2eSMichael Tuexen break;
504e6194c2eSMichael Tuexen #endif
505e6194c2eSMichael Tuexen default:
506c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
50760990c0cSMichael Tuexen return (EINVAL);
508d61a0ae0SRandall Stewart }
509d61a0ae0SRandall Stewart }
5104a91aa8fSMichael Tuexen vflagsav = inp->ip_inp.inp.inp_vflag;
5110ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
5120ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag |= INP_IPV6;
51394962f6bSMichael Tuexen if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
514e6194c2eSMichael Tuexen switch (addr->sa_family) {
515e6194c2eSMichael Tuexen #ifdef INET
516e6194c2eSMichael Tuexen case AF_INET:
517f8829a4aSRandall Stewart /* binding v4 addr to v6 socket, so reset flags */
5180ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5190ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
520e6194c2eSMichael Tuexen break;
521e6194c2eSMichael Tuexen #endif
522e6194c2eSMichael Tuexen #ifdef INET6
523e6194c2eSMichael Tuexen case AF_INET6:
524e6194c2eSMichael Tuexen {
525f8829a4aSRandall Stewart struct sockaddr_in6 *sin6_p;
526f8829a4aSRandall Stewart
527f8829a4aSRandall Stewart sin6_p = (struct sockaddr_in6 *)addr;
528f8829a4aSRandall Stewart
529f8829a4aSRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
5300ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag |= INP_IPV4;
531e6194c2eSMichael Tuexen }
532e6194c2eSMichael Tuexen #ifdef INET
533e6194c2eSMichael Tuexen if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
534f8829a4aSRandall Stewart struct sockaddr_in sin;
535f8829a4aSRandall Stewart
536f8829a4aSRandall Stewart in6_sin6_2_sin(&sin, sin6_p);
5370ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5380ecd976eSBjoern A. Zeeb inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
5391b649582SRandall Stewart error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
5404a91aa8fSMichael Tuexen goto out;
541f8829a4aSRandall Stewart }
542e6194c2eSMichael Tuexen #endif
543e6194c2eSMichael Tuexen break;
544e6194c2eSMichael Tuexen }
545e6194c2eSMichael Tuexen #endif
546e6194c2eSMichael Tuexen default:
547e6194c2eSMichael Tuexen break;
548f8829a4aSRandall Stewart }
549f8829a4aSRandall Stewart } else if (addr != NULL) {
550e6194c2eSMichael Tuexen struct sockaddr_in6 *sin6_p;
551e6194c2eSMichael Tuexen
552f8829a4aSRandall Stewart /* IPV6_V6ONLY socket */
553e6194c2eSMichael Tuexen #ifdef INET
554f8829a4aSRandall Stewart if (addr->sa_family == AF_INET) {
555f8829a4aSRandall Stewart /* can't bind v4 addr to v6 only socket! */
556c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
5574a91aa8fSMichael Tuexen error = EINVAL;
5584a91aa8fSMichael Tuexen goto out;
559e6194c2eSMichael Tuexen }
560e6194c2eSMichael Tuexen #endif
561f8829a4aSRandall Stewart sin6_p = (struct sockaddr_in6 *)addr;
562f8829a4aSRandall Stewart
563851b7298SRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
564f8829a4aSRandall Stewart /* can't bind v4-mapped addrs either! */
565f8829a4aSRandall Stewart /* NOTE: we don't support SIIT */
566c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
5674a91aa8fSMichael Tuexen error = EINVAL;
5684a91aa8fSMichael Tuexen goto out;
569f8829a4aSRandall Stewart }
570f8829a4aSRandall Stewart }
5711b649582SRandall Stewart error = sctp_inpcb_bind(so, addr, NULL, p);
5724a91aa8fSMichael Tuexen out:
5734a91aa8fSMichael Tuexen if (error != 0)
5744a91aa8fSMichael Tuexen inp->ip_inp.inp.inp_vflag = vflagsav;
57560990c0cSMichael Tuexen return (error);
576f8829a4aSRandall Stewart }
577f8829a4aSRandall Stewart
578f8829a4aSRandall Stewart static void
sctp6_close(struct socket * so)579f8829a4aSRandall Stewart sctp6_close(struct socket *so)
580f8829a4aSRandall Stewart {
5812afb3e84SRandall Stewart sctp_close(so);
582f8829a4aSRandall Stewart }
583f8829a4aSRandall Stewart
58442551e99SRandall Stewart /* This could be made common with sctp_detach() since they are identical */
585f8829a4aSRandall Stewart
586f8829a4aSRandall Stewart int
587f8829a4aSRandall Stewart sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
588f8829a4aSRandall Stewart struct mbuf *control, struct thread *p);
589f8829a4aSRandall Stewart
590f8829a4aSRandall Stewart static int
sctp6_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct thread * p)591f8829a4aSRandall Stewart sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
592f8829a4aSRandall Stewart struct mbuf *control, struct thread *p)
593f8829a4aSRandall Stewart {
594f8829a4aSRandall Stewart struct sctp_inpcb *inp;
595f8829a4aSRandall Stewart
596f8829a4aSRandall Stewart #ifdef INET
597f8829a4aSRandall Stewart struct sockaddr_in6 *sin6;
598f8829a4aSRandall Stewart #endif /* INET */
599f8829a4aSRandall Stewart /* No SPL needed since sctp_output does this */
600f8829a4aSRandall Stewart
601f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb;
602f8829a4aSRandall Stewart if (inp == NULL) {
603f8829a4aSRandall Stewart if (control) {
60417205eccSRandall Stewart SCTP_RELEASE_PKT(control);
605f8829a4aSRandall Stewart control = NULL;
606f8829a4aSRandall Stewart }
60717205eccSRandall Stewart SCTP_RELEASE_PKT(m);
608c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
60960990c0cSMichael Tuexen return (EINVAL);
610f8829a4aSRandall Stewart }
611f8829a4aSRandall Stewart /*
612f8829a4aSRandall Stewart * For the TCP model we may get a NULL addr, if we are a connected
613f8829a4aSRandall Stewart * socket thats ok.
614f8829a4aSRandall Stewart */
615f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
616f8829a4aSRandall Stewart (addr == NULL)) {
617f8829a4aSRandall Stewart goto connected_type;
618f8829a4aSRandall Stewart }
619f8829a4aSRandall Stewart if (addr == NULL) {
62017205eccSRandall Stewart SCTP_RELEASE_PKT(m);
621f8829a4aSRandall Stewart if (control) {
62217205eccSRandall Stewart SCTP_RELEASE_PKT(control);
623f8829a4aSRandall Stewart control = NULL;
624f8829a4aSRandall Stewart }
625c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
626f8829a4aSRandall Stewart return (EDESTADDRREQ);
627f8829a4aSRandall Stewart }
628f161d294SMark Johnston switch (addr->sa_family) {
629f161d294SMark Johnston #ifdef INET
630f161d294SMark Johnston case AF_INET:
631f161d294SMark Johnston if (addr->sa_len != sizeof(struct sockaddr_in)) {
632d8acd268SMark Johnston if (control) {
633d8acd268SMark Johnston SCTP_RELEASE_PKT(control);
634d8acd268SMark Johnston control = NULL;
635d8acd268SMark Johnston }
636d8acd268SMark Johnston SCTP_RELEASE_PKT(m);
637f161d294SMark Johnston SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
638f161d294SMark Johnston return (EINVAL);
639f161d294SMark Johnston }
640f161d294SMark Johnston break;
641f161d294SMark Johnston #endif
642f161d294SMark Johnston #ifdef INET6
643f161d294SMark Johnston case AF_INET6:
644f161d294SMark Johnston if (addr->sa_len != sizeof(struct sockaddr_in6)) {
645d8acd268SMark Johnston if (control) {
646d8acd268SMark Johnston SCTP_RELEASE_PKT(control);
647d8acd268SMark Johnston control = NULL;
648d8acd268SMark Johnston }
649d8acd268SMark Johnston SCTP_RELEASE_PKT(m);
650f161d294SMark Johnston SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
651f161d294SMark Johnston return (EINVAL);
652f161d294SMark Johnston }
653f161d294SMark Johnston break;
654f161d294SMark Johnston #endif
655f161d294SMark Johnston default:
656d8acd268SMark Johnston if (control) {
657d8acd268SMark Johnston SCTP_RELEASE_PKT(control);
658d8acd268SMark Johnston control = NULL;
659d8acd268SMark Johnston }
660d8acd268SMark Johnston SCTP_RELEASE_PKT(m);
661f161d294SMark Johnston SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
662f161d294SMark Johnston return (EINVAL);
663f161d294SMark Johnston }
664f8829a4aSRandall Stewart #ifdef INET
665f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)addr;
6660ecd976eSBjoern A. Zeeb if (SCTP_IPV6_V6ONLY(inp)) {
667f8829a4aSRandall Stewart /*
668f8829a4aSRandall Stewart * if IPV6_V6ONLY flag, we discard datagrams destined to a
669f8829a4aSRandall Stewart * v4 addr or v4-mapped addr
670f8829a4aSRandall Stewart */
671f8829a4aSRandall Stewart if (addr->sa_family == AF_INET) {
672d8acd268SMark Johnston if (control) {
673d8acd268SMark Johnston SCTP_RELEASE_PKT(control);
674d8acd268SMark Johnston control = NULL;
675d8acd268SMark Johnston }
676d8acd268SMark Johnston SCTP_RELEASE_PKT(m);
677c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
67860990c0cSMichael Tuexen return (EINVAL);
679f8829a4aSRandall Stewart }
680f8829a4aSRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
681d8acd268SMark Johnston if (control) {
682d8acd268SMark Johnston SCTP_RELEASE_PKT(control);
683d8acd268SMark Johnston control = NULL;
684d8acd268SMark Johnston }
685d8acd268SMark Johnston SCTP_RELEASE_PKT(m);
686c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
68760990c0cSMichael Tuexen return (EINVAL);
688f8829a4aSRandall Stewart }
689f8829a4aSRandall Stewart }
690784692c7SMichael Tuexen if ((addr->sa_family == AF_INET6) &&
691784692c7SMichael Tuexen IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
692f8829a4aSRandall Stewart struct sockaddr_in sin;
693f8829a4aSRandall Stewart
694f8829a4aSRandall Stewart /* convert v4-mapped into v4 addr and send */
695f8829a4aSRandall Stewart in6_sin6_2_sin(&sin, sin6);
6963457ccdaSMichael Tuexen return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
697f8829a4aSRandall Stewart }
698f8829a4aSRandall Stewart #endif /* INET */
699f8829a4aSRandall Stewart connected_type:
700f8829a4aSRandall Stewart /* now what about control */
701f8829a4aSRandall Stewart if (control) {
702f8829a4aSRandall Stewart if (inp->control) {
703ad81507eSRandall Stewart SCTP_PRINTF("huh? control set?\n");
70417205eccSRandall Stewart SCTP_RELEASE_PKT(inp->control);
705f8829a4aSRandall Stewart inp->control = NULL;
706f8829a4aSRandall Stewart }
707f8829a4aSRandall Stewart inp->control = control;
708f8829a4aSRandall Stewart }
709f8829a4aSRandall Stewart /* Place the data */
710f8829a4aSRandall Stewart if (inp->pkt) {
711139bc87fSRandall Stewart SCTP_BUF_NEXT(inp->pkt_last) = m;
712f8829a4aSRandall Stewart inp->pkt_last = m;
713f8829a4aSRandall Stewart } else {
714f8829a4aSRandall Stewart inp->pkt_last = inp->pkt = m;
715f8829a4aSRandall Stewart }
716f8829a4aSRandall Stewart if (
717f8829a4aSRandall Stewart /* FreeBSD and MacOSX uses a flag passed */
718f8829a4aSRandall Stewart ((flags & PRUS_MORETOCOME) == 0)
719f8829a4aSRandall Stewart ) {
720f8829a4aSRandall Stewart /*
721f8829a4aSRandall Stewart * note with the current version this code will only be used
722f8829a4aSRandall Stewart * by OpenBSD, NetBSD and FreeBSD have methods for
723f8829a4aSRandall Stewart * re-defining sosend() to use sctp_sosend(). One can
724*58c99e07SGordon Bergling * optionally switch back to this code (by changing back the
725*58c99e07SGordon Bergling * definitions but this is not advisable.
726f8829a4aSRandall Stewart */
727868b51f2SMichael Tuexen struct epoch_tracker et;
728f8829a4aSRandall Stewart int ret;
729f8829a4aSRandall Stewart
730868b51f2SMichael Tuexen NET_EPOCH_ENTER(et);
731f8829a4aSRandall Stewart ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
732868b51f2SMichael Tuexen NET_EPOCH_EXIT(et);
733f8829a4aSRandall Stewart inp->pkt = NULL;
734f8829a4aSRandall Stewart inp->control = NULL;
735f8829a4aSRandall Stewart return (ret);
736f8829a4aSRandall Stewart } else {
737f8829a4aSRandall Stewart return (0);
738f8829a4aSRandall Stewart }
739f8829a4aSRandall Stewart }
740f8829a4aSRandall Stewart
741f8829a4aSRandall Stewart static int
sctp6_connect(struct socket * so,struct sockaddr * addr,struct thread * p)742f8829a4aSRandall Stewart sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
743f8829a4aSRandall Stewart {
744868b51f2SMichael Tuexen struct epoch_tracker et;
74542551e99SRandall Stewart uint32_t vrf_id;
746f8829a4aSRandall Stewart int error = 0;
747f8829a4aSRandall Stewart struct sctp_inpcb *inp;
748f8829a4aSRandall Stewart struct sctp_tcb *stcb;
749f8829a4aSRandall Stewart #ifdef INET
750f8829a4aSRandall Stewart struct sockaddr_in6 *sin6;
75124aaac8dSMichael Tuexen union sctp_sockstore store;
752e6194c2eSMichael Tuexen #endif
753f8829a4aSRandall Stewart
754f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb;
755dea47f39SMichael Tuexen if (inp == NULL) {
756c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
757f8829a4aSRandall Stewart return (ECONNRESET); /* I made the same as TCP since we are
758f8829a4aSRandall Stewart * not setup? */
759f8829a4aSRandall Stewart }
760d61a0ae0SRandall Stewart if (addr == NULL) {
761c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
762d61a0ae0SRandall Stewart return (EINVAL);
763d61a0ae0SRandall Stewart }
764e6194c2eSMichael Tuexen switch (addr->sa_family) {
765e6194c2eSMichael Tuexen #ifdef INET
766e6194c2eSMichael Tuexen case AF_INET:
767e6194c2eSMichael Tuexen if (addr->sa_len != sizeof(struct sockaddr_in)) {
768c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
769d61a0ae0SRandall Stewart return (EINVAL);
770d61a0ae0SRandall Stewart }
771e6194c2eSMichael Tuexen break;
772e6194c2eSMichael Tuexen #endif
773e6194c2eSMichael Tuexen #ifdef INET6
774e6194c2eSMichael Tuexen case AF_INET6:
775e6194c2eSMichael Tuexen if (addr->sa_len != sizeof(struct sockaddr_in6)) {
776c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
777d61a0ae0SRandall Stewart return (EINVAL);
778d61a0ae0SRandall Stewart }
779e6194c2eSMichael Tuexen break;
780e6194c2eSMichael Tuexen #endif
781e6194c2eSMichael Tuexen default:
782e6194c2eSMichael Tuexen SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
783e6194c2eSMichael Tuexen return (EINVAL);
784e6194c2eSMichael Tuexen }
785e6194c2eSMichael Tuexen
786bff64a4dSRandall Stewart vrf_id = inp->def_vrf_id;
787f8829a4aSRandall Stewart SCTP_ASOC_CREATE_LOCK(inp);
788f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp);
789f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
790f8829a4aSRandall Stewart SCTP_PCB_FLAGS_UNBOUND) {
791f8829a4aSRandall Stewart /* Bind a ephemeral port */
792f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
793f8829a4aSRandall Stewart error = sctp6_bind(so, NULL, p);
794f8829a4aSRandall Stewart if (error) {
795f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp);
796f8829a4aSRandall Stewart
797f8829a4aSRandall Stewart return (error);
798f8829a4aSRandall Stewart }
799f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp);
800f8829a4aSRandall Stewart }
801f8829a4aSRandall Stewart if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
802f8829a4aSRandall Stewart (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
803f8829a4aSRandall Stewart /* We are already connected AND the TCP model */
804f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
805f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp);
806c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
807f8829a4aSRandall Stewart return (EADDRINUSE);
808f8829a4aSRandall Stewart }
809f8829a4aSRandall Stewart #ifdef INET
810f8829a4aSRandall Stewart sin6 = (struct sockaddr_in6 *)addr;
8110ecd976eSBjoern A. Zeeb if (SCTP_IPV6_V6ONLY(inp)) {
812f8829a4aSRandall Stewart /*
813f8829a4aSRandall Stewart * if IPV6_V6ONLY flag, ignore connections destined to a v4
814f8829a4aSRandall Stewart * addr or v4-mapped addr
815f8829a4aSRandall Stewart */
816f8829a4aSRandall Stewart if (addr->sa_family == AF_INET) {
817f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
818f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp);
819c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
82060990c0cSMichael Tuexen return (EINVAL);
821f8829a4aSRandall Stewart }
822f8829a4aSRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
823f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
824f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp);
825c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
82660990c0cSMichael Tuexen return (EINVAL);
827f8829a4aSRandall Stewart }
828f8829a4aSRandall Stewart }
829784692c7SMichael Tuexen if ((addr->sa_family == AF_INET6) &&
830784692c7SMichael Tuexen IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
831f8829a4aSRandall Stewart /* convert v4-mapped into v4 addr */
83224aaac8dSMichael Tuexen in6_sin6_2_sin(&store.sin, sin6);
83324aaac8dSMichael Tuexen addr = &store.sa;
834274b0bd5SMichael Tuexen }
835f8829a4aSRandall Stewart #endif /* INET */
836f8829a4aSRandall Stewart /* Now do we connect? */
837f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
838f8829a4aSRandall Stewart stcb = LIST_FIRST(&inp->sctp_asoc_list);
839ad81507eSRandall Stewart if (stcb) {
8402e8bb5ddSMichael Tuexen SCTP_TCB_LOCK(stcb);
841ad81507eSRandall Stewart }
842f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
843f8829a4aSRandall Stewart } else {
844f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
845f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp);
846f8829a4aSRandall Stewart SCTP_INP_INCR_REF(inp);
847f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp);
848f8829a4aSRandall Stewart stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
849f8829a4aSRandall Stewart if (stcb == NULL) {
850f8829a4aSRandall Stewart SCTP_INP_WLOCK(inp);
851f8829a4aSRandall Stewart SCTP_INP_DECR_REF(inp);
852f8829a4aSRandall Stewart SCTP_INP_WUNLOCK(inp);
853f8829a4aSRandall Stewart }
854f8829a4aSRandall Stewart }
855f8829a4aSRandall Stewart
856f8829a4aSRandall Stewart if (stcb != NULL) {
857f8829a4aSRandall Stewart /* Already have or am bring up an association */
858f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp);
859f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb);
860c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
861f8829a4aSRandall Stewart return (EALREADY);
862f8829a4aSRandall Stewart }
863f8829a4aSRandall Stewart /* We are GOOD to go */
8642d5c48ecSMark Johnston stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
865ec70917fSMichael Tuexen inp->sctp_ep.pre_open_stream_count,
8668a956abeSMichael Tuexen inp->sctp_ep.port, p,
8678a956abeSMichael Tuexen SCTP_INITIALIZE_AUTH_PARAMS);
868f8829a4aSRandall Stewart SCTP_ASOC_CREATE_UNLOCK(inp);
869f8829a4aSRandall Stewart if (stcb == NULL) {
870f8829a4aSRandall Stewart /* Gak! no memory */
871f8829a4aSRandall Stewart return (error);
872f8829a4aSRandall Stewart }
87398f04e43SMichael Tuexen SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
874ad81507eSRandall Stewart (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
875868b51f2SMichael Tuexen NET_EPOCH_ENTER(et);
876ceaad40aSRandall Stewart sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
877f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb);
878868b51f2SMichael Tuexen NET_EPOCH_EXIT(et);
87960990c0cSMichael Tuexen return (error);
880f8829a4aSRandall Stewart }
881f8829a4aSRandall Stewart
882f8829a4aSRandall Stewart static int
sctp6_getaddr(struct socket * so,struct sockaddr * sa)8830fac350cSGleb Smirnoff sctp6_getaddr(struct socket *so, struct sockaddr *sa)
884f8829a4aSRandall Stewart {
8850fac350cSGleb Smirnoff struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
886f8829a4aSRandall Stewart struct sctp_inpcb *inp;
88742551e99SRandall Stewart uint32_t vrf_id;
88842551e99SRandall Stewart struct sctp_ifa *sctp_ifa;
889f8829a4aSRandall Stewart int error;
890f8829a4aSRandall Stewart
8910fac350cSGleb Smirnoff *sin6 = (struct sockaddr_in6 ){
8920fac350cSGleb Smirnoff .sin6_len = sizeof(struct sockaddr_in6),
8930fac350cSGleb Smirnoff .sin6_family = AF_INET6,
8940fac350cSGleb Smirnoff };
895f8829a4aSRandall Stewart
896f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb;
897f8829a4aSRandall Stewart if (inp == NULL) {
898c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
89960990c0cSMichael Tuexen return (ECONNRESET);
900f8829a4aSRandall Stewart }
901f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp);
902f8829a4aSRandall Stewart sin6->sin6_port = inp->sctp_lport;
903f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
904f8829a4aSRandall Stewart /* For the bound all case you get back 0 */
905f8829a4aSRandall Stewart if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
906f8829a4aSRandall Stewart struct sctp_tcb *stcb;
907f8829a4aSRandall Stewart struct sockaddr_in6 *sin_a6;
908f8829a4aSRandall Stewart struct sctp_nets *net;
909f8829a4aSRandall Stewart int fnd;
910f8829a4aSRandall Stewart
911f8829a4aSRandall Stewart stcb = LIST_FIRST(&inp->sctp_asoc_list);
912f8829a4aSRandall Stewart if (stcb == NULL) {
9136df7c000SMichael Tuexen SCTP_INP_RUNLOCK(inp);
9146df7c000SMichael Tuexen SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
9156df7c000SMichael Tuexen return (ENOENT);
916f8829a4aSRandall Stewart }
917f8829a4aSRandall Stewart fnd = 0;
918f8829a4aSRandall Stewart sin_a6 = NULL;
919f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
920f8829a4aSRandall Stewart sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
92103b0b021SRandall Stewart if (sin_a6 == NULL)
92203b0b021SRandall Stewart /* this will make coverity happy */
92303b0b021SRandall Stewart continue;
92403b0b021SRandall Stewart
925f8829a4aSRandall Stewart if (sin_a6->sin6_family == AF_INET6) {
926f8829a4aSRandall Stewart fnd = 1;
927f8829a4aSRandall Stewart break;
928f8829a4aSRandall Stewart }
929f8829a4aSRandall Stewart }
930f8829a4aSRandall Stewart if ((!fnd) || (sin_a6 == NULL)) {
931f8829a4aSRandall Stewart /* punt */
9326df7c000SMichael Tuexen SCTP_INP_RUNLOCK(inp);
9336df7c000SMichael Tuexen SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
9346df7c000SMichael Tuexen return (ENOENT);
935f8829a4aSRandall Stewart }
936bff64a4dSRandall Stewart vrf_id = inp->def_vrf_id;
937bff64a4dSRandall Stewart sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
93842551e99SRandall Stewart if (sctp_ifa) {
93942551e99SRandall Stewart sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
94042551e99SRandall Stewart }
941f8829a4aSRandall Stewart } else {
942f8829a4aSRandall Stewart /* For the bound all case you get back 0 */
943f8829a4aSRandall Stewart memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
944f8829a4aSRandall Stewart }
945f8829a4aSRandall Stewart } else {
946f8829a4aSRandall Stewart /* Take the first IPv6 address in the list */
947f8829a4aSRandall Stewart struct sctp_laddr *laddr;
948f8829a4aSRandall Stewart int fnd = 0;
949f8829a4aSRandall Stewart
950f8829a4aSRandall Stewart LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
95142551e99SRandall Stewart if (laddr->ifa->address.sa.sa_family == AF_INET6) {
952f8829a4aSRandall Stewart struct sockaddr_in6 *sin_a;
953f8829a4aSRandall Stewart
95424aaac8dSMichael Tuexen sin_a = &laddr->ifa->address.sin6;
955f8829a4aSRandall Stewart sin6->sin6_addr = sin_a->sin6_addr;
956f8829a4aSRandall Stewart fnd = 1;
957f8829a4aSRandall Stewart break;
958f8829a4aSRandall Stewart }
959f8829a4aSRandall Stewart }
960f8829a4aSRandall Stewart if (!fnd) {
961f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
962c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
96360990c0cSMichael Tuexen return (ENOENT);
964f8829a4aSRandall Stewart }
965f8829a4aSRandall Stewart }
966f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
967f8829a4aSRandall Stewart /* Scoping things for v6 */
96803b0b021SRandall Stewart if ((error = sa6_recoverscope(sin6)) != 0) {
969f8829a4aSRandall Stewart return (error);
97003b0b021SRandall Stewart }
9710fac350cSGleb Smirnoff
972f8829a4aSRandall Stewart return (0);
973f8829a4aSRandall Stewart }
974f8829a4aSRandall Stewart
975f8829a4aSRandall Stewart static int
sctp6_peeraddr(struct socket * so,struct sockaddr * sa)9760fac350cSGleb Smirnoff sctp6_peeraddr(struct socket *so, struct sockaddr *sa)
977f8829a4aSRandall Stewart {
9780fac350cSGleb Smirnoff struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
979f8829a4aSRandall Stewart int fnd;
980f8829a4aSRandall Stewart struct sockaddr_in6 *sin_a6;
981f8829a4aSRandall Stewart struct sctp_inpcb *inp;
982f8829a4aSRandall Stewart struct sctp_tcb *stcb;
983f8829a4aSRandall Stewart struct sctp_nets *net;
984f8829a4aSRandall Stewart int error;
985f8829a4aSRandall Stewart
9860fac350cSGleb Smirnoff *sin6 = (struct sockaddr_in6 ){
9870fac350cSGleb Smirnoff .sin6_len = sizeof(struct sockaddr_in6),
9880fac350cSGleb Smirnoff .sin6_family = AF_INET6,
9890fac350cSGleb Smirnoff };
990f8829a4aSRandall Stewart
991f8829a4aSRandall Stewart inp = (struct sctp_inpcb *)so->so_pcb;
99260990c0cSMichael Tuexen if ((inp == NULL) ||
99360990c0cSMichael Tuexen ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
99460990c0cSMichael Tuexen /* UDP type and listeners will drop out here */
99560990c0cSMichael Tuexen SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
99660990c0cSMichael Tuexen return (ENOTCONN);
997f8829a4aSRandall Stewart }
998f8829a4aSRandall Stewart SCTP_INP_RLOCK(inp);
999f8829a4aSRandall Stewart stcb = LIST_FIRST(&inp->sctp_asoc_list);
1000ad81507eSRandall Stewart if (stcb) {
1001f8829a4aSRandall Stewart SCTP_TCB_LOCK(stcb);
1002ad81507eSRandall Stewart }
1003f8829a4aSRandall Stewart SCTP_INP_RUNLOCK(inp);
1004f8829a4aSRandall Stewart if (stcb == NULL) {
1005c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
100660990c0cSMichael Tuexen return (ECONNRESET);
1007f8829a4aSRandall Stewart }
1008f8829a4aSRandall Stewart fnd = 0;
1009f8829a4aSRandall Stewart TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1010f8829a4aSRandall Stewart sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1011f8829a4aSRandall Stewart if (sin_a6->sin6_family == AF_INET6) {
1012f8829a4aSRandall Stewart fnd = 1;
1013f8829a4aSRandall Stewart sin6->sin6_port = stcb->rport;
1014f8829a4aSRandall Stewart sin6->sin6_addr = sin_a6->sin6_addr;
1015f8829a4aSRandall Stewart break;
1016f8829a4aSRandall Stewart }
1017f8829a4aSRandall Stewart }
1018f8829a4aSRandall Stewart SCTP_TCB_UNLOCK(stcb);
1019f8829a4aSRandall Stewart if (!fnd) {
1020f8829a4aSRandall Stewart /* No IPv4 address */
1021c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
102260990c0cSMichael Tuexen return (ENOENT);
1023f8829a4aSRandall Stewart }
1024caeae63fSMichael Tuexen if ((error = sa6_recoverscope(sin6)) != 0) {
1025caeae63fSMichael Tuexen SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1026f8829a4aSRandall Stewart return (error);
1027caeae63fSMichael Tuexen }
10280fac350cSGleb Smirnoff
1029f8829a4aSRandall Stewart return (0);
1030f8829a4aSRandall Stewart }
1031f8829a4aSRandall Stewart
1032f8829a4aSRandall Stewart static int
sctp6_in6getaddr(struct socket * so,struct sockaddr * sa)10330fac350cSGleb Smirnoff sctp6_in6getaddr(struct socket *so, struct sockaddr *sa)
1034f8829a4aSRandall Stewart {
10350ecd976eSBjoern A. Zeeb struct inpcb *inp = sotoinpcb(so);
103693164cf9SRandall Stewart int error;
1037f8829a4aSRandall Stewart
10380ecd976eSBjoern A. Zeeb if (inp == NULL) {
1039c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
104060990c0cSMichael Tuexen return (EINVAL);
1041c4739e2fSRandall Stewart }
10420053ed28SMichael Tuexen
1043f8829a4aSRandall Stewart /* allow v6 addresses precedence */
10440fac350cSGleb Smirnoff error = sctp6_getaddr(so, sa);
1045e6194c2eSMichael Tuexen #ifdef INET
1046f8829a4aSRandall Stewart if (error) {
10470fac350cSGleb Smirnoff struct sockaddr_in sin;
10486df7c000SMichael Tuexen
1049f8829a4aSRandall Stewart /* try v4 next if v6 failed */
10500fac350cSGleb Smirnoff error = sctp_ingetaddr(so, (struct sockaddr *)&sin);
10510fac350cSGleb Smirnoff if (error)
1052f8829a4aSRandall Stewart return (error);
10530fac350cSGleb Smirnoff in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
1054f8829a4aSRandall Stewart }
1055e6194c2eSMichael Tuexen #endif
1056f8829a4aSRandall Stewart return (error);
1057f8829a4aSRandall Stewart }
1058f8829a4aSRandall Stewart
1059f8829a4aSRandall Stewart static int
sctp6_getpeeraddr(struct socket * so,struct sockaddr * sa)10600fac350cSGleb Smirnoff sctp6_getpeeraddr(struct socket *so, struct sockaddr *sa)
1061f8829a4aSRandall Stewart {
10620ecd976eSBjoern A. Zeeb struct inpcb *inp = sotoinpcb(so);
106393164cf9SRandall Stewart int error;
1064f8829a4aSRandall Stewart
10650ecd976eSBjoern A. Zeeb if (inp == NULL) {
1066c4739e2fSRandall Stewart SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
106760990c0cSMichael Tuexen return (EINVAL);
1068c4739e2fSRandall Stewart }
10690053ed28SMichael Tuexen
1070f8829a4aSRandall Stewart /* allow v6 addresses precedence */
10710fac350cSGleb Smirnoff error = sctp6_peeraddr(so, sa);
1072e6194c2eSMichael Tuexen #ifdef INET
1073f8829a4aSRandall Stewart if (error) {
10740fac350cSGleb Smirnoff struct sockaddr_in sin;
10756df7c000SMichael Tuexen
1076f8829a4aSRandall Stewart /* try v4 next if v6 failed */
10770fac350cSGleb Smirnoff error = sctp_peeraddr(so, (struct sockaddr *)&sin);
10780fac350cSGleb Smirnoff if (error)
1079f8829a4aSRandall Stewart return (error);
10800fac350cSGleb Smirnoff in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
1081f8829a4aSRandall Stewart }
1082e6194c2eSMichael Tuexen #endif
108360990c0cSMichael Tuexen return (error);
1084f8829a4aSRandall Stewart }
1085f8829a4aSRandall Stewart
1086e7d02be1SGleb Smirnoff #define SCTP6_PROTOSW \
1087e7d02be1SGleb Smirnoff .pr_protocol = IPPROTO_SCTP, \
1088e7d02be1SGleb Smirnoff .pr_ctloutput = sctp_ctloutput, \
10899ade2745SMichael Tuexen .pr_abort = sctp_abort, \
1090e7d02be1SGleb Smirnoff .pr_accept = sctp_accept, \
1091e7d02be1SGleb Smirnoff .pr_attach = sctp6_attach, \
1092e7d02be1SGleb Smirnoff .pr_bind = sctp6_bind, \
1093e7d02be1SGleb Smirnoff .pr_connect = sctp6_connect, \
1094e7d02be1SGleb Smirnoff .pr_control = in6_control, \
1095e7d02be1SGleb Smirnoff .pr_close = sctp6_close, \
1096e7d02be1SGleb Smirnoff .pr_detach = sctp6_close, \
1097e7d02be1SGleb Smirnoff .pr_sopoll = sopoll_generic, \
1098c7587f7aSMichael Tuexen .pr_disconnect = sctp_disconnect, \
1099e7d02be1SGleb Smirnoff .pr_listen = sctp_listen, \
1100e7d02be1SGleb Smirnoff .pr_peeraddr = sctp6_getpeeraddr, \
1101e7d02be1SGleb Smirnoff .pr_send = sctp6_send, \
1102e7d02be1SGleb Smirnoff .pr_shutdown = sctp_shutdown, \
1103e7d02be1SGleb Smirnoff .pr_sockaddr = sctp6_in6getaddr, \
1104e7d02be1SGleb Smirnoff .pr_sosend = sctp_sosend, \
1105e7d02be1SGleb Smirnoff .pr_soreceive = sctp_soreceive
1106e7d02be1SGleb Smirnoff
1107e7d02be1SGleb Smirnoff struct protosw sctp6_seqpacket_protosw = {
1108e7d02be1SGleb Smirnoff .pr_type = SOCK_SEQPACKET,
1109e7d02be1SGleb Smirnoff .pr_flags = PR_WANTRCVD,
1110e7d02be1SGleb Smirnoff SCTP6_PROTOSW
1111f8829a4aSRandall Stewart };
11125e20b91dSMichael Tuexen
1113e7d02be1SGleb Smirnoff struct protosw sctp6_stream_protosw = {
1114e7d02be1SGleb Smirnoff .pr_type = SOCK_STREAM,
1115e7d02be1SGleb Smirnoff .pr_flags = PR_CONNREQUIRED | PR_WANTRCVD,
1116e7d02be1SGleb Smirnoff SCTP6_PROTOSW
1117e7d02be1SGleb Smirnoff };
11185e20b91dSMichael Tuexen #endif
1119