| 6 |
| * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | * |
| 29 | */ |
| 30 |
| 31 | #if defined(INET) || defined(INET6) |
| 32 | #include <sys/types.h> |
| 33 | #if !defined(__Userspace_os_Windows) |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <unistd.h> |
| 37 | #include <pthread.h> |
| 38 | #if !defined(__Userspace_os_DragonFly) && !defined(__Userspace_os_FreeBSD) && !defined(__Userspace_os_NetBSD) |
| 39 | #include <sys/uio.h> |
| 40 | #else |
| 41 | #include <user_ip6_var.h> |
| 42 | #endif |
| 43 | #endif |
| 44 | #include <netinet/sctp_os.h> |
| 45 | #include <netinet/sctp_var.h> |
| 46 | #include <netinet/sctp_pcb.h> |
| 47 | #include <netinet/sctp_input.h> |
| 48 | #if 0 |
| 49 | #if defined(__Userspace_os_Linux) |
| 50 | #include <linux/netlink.h> |
| 51 | #ifdef HAVE_LINUX_IF_ADDR_H |
| 52 | #include <linux/if_addr.h> |
| 53 | #endif |
| 54 | #ifdef HAVE_LINUX_RTNETLINK_H |
| 55 | #include <linux/rtnetlink.h> |
| 56 | #endif |
| 57 | #endif |
| 58 | #endif |
| 59 | #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) |
| 60 | #include <net/route.h> |
| 61 | #endif |
| 62 | /* local macros and datatypes used to get IP addresses system independently */ |
| 63 | #if !defined(IP_PKTINFO ) && ! defined(IP_RECVDSTADDR) |
| 64 | # error "Can't determine socket option to use to get UDP IP" |
| 65 | #endif |
| 66 |
| 67 | void recv_thread_destroy(void); |
| 68 | #define MAXLEN_MBUF_CHAIN 32 /* What should this value be? */ |
| 69 | #define ROUNDUP(a, size) (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a)) |
| 70 | #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) |
| 71 | #define NEXT_SA(ap) ap = (struct sockaddr *) \ |
| 72 | ((caddr_t) ap + (ap->sa_len ? ROUNDUP(ap->sa_len, sizeof (uint32_t)) : sizeof(uint32_t))) |
| 73 | #endif |
| 74 |
| 75 | #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) |
| 76 | static void |
| 77 | sctp_get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info) |
| 78 | { |
| 79 | int i; |
| 80 |
| 81 | for (i = 0; i < RTAX_MAX; i++) { |
| 82 | if (addrs & (1 << i)) { |
| 83 | rti_info[i] = sa; |
| 84 | NEXT_SA(sa); |
| 85 | } else { |
| 86 | rti_info[i] = NULL; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 |
| 91 | static void |
| 92 | sctp_handle_ifamsg(unsigned char type, unsigned short index, struct sockaddr *sa) |
| 93 | { |
| 94 | int rc; |
| 95 | struct ifaddrs *ifa, *ifas; |
| 96 |
| 97 | /* handle only the types we want */ |
| 98 | if ((type != RTM_NEWADDR) && (type != RTM_DELADDR)) { |
| 99 | return; |
| 100 | } |
| 101 |
| 102 | rc = getifaddrs(&ifas); |
| 103 | if (rc != 0) { |
| 104 | return; |
| 105 | } |
| 106 | for (ifa = ifas; ifa; ifa = ifa->ifa_next) { |
| 107 | if (index == if_nametoindex(ifa->ifa_name)) { |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | if (ifa == NULL) { |
| 112 | freeifaddrs(ifas); |
| 113 | return; |
| 114 | } |
| 115 |
| 116 | /* relay the appropriate address change to the base code */ |
| 117 | if (type == RTM_NEWADDR) { |
| 118 | (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, |
| 119 | NULL, |
| 120 | if_nametoindex(ifa->ifa_name), |
| 121 | 0, |
| 122 | ifa->ifa_name, |
| 123 | NULL, |
| 124 | sa, |
| 125 | 0, |
| 126 | 1); |
| 127 | } else { |
| 128 | sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr, |
| 129 | if_nametoindex(ifa->ifa_name), |
| 130 | ifa->ifa_name); |
| 131 | } |
| 132 | freeifaddrs(ifas); |
| 133 | } |
| 134 |
| 135 | static void * |
| 136 | recv_function_route(void *arg) |
| 137 | { |
| 138 | ssize_t ret; |
| 139 | struct ifa_msghdr *ifa; |
| 140 | char rt_buffer[1024]; |
| 141 | struct sockaddr *sa, *rti_info[RTAX_MAX]; |
| 142 |
| 143 | sctp_userspace_set_threadname("SCTP addr mon"); |
| 144 |
| 145 | while (1) { |
| 146 | memset(rt_buffer, 0, sizeof(rt_buffer)); |
| 147 | ret = recv(SCTP_BASE_VAR(userspace_route), rt_buffer, sizeof(rt_buffer), 0); |
| 148 |
| 149 | if (ret > 0) { |
| 150 | ifa = (struct ifa_msghdr *) rt_buffer; |
| 151 | if (ifa->ifam_type != RTM_DELADDR && ifa->ifam_type != RTM_NEWADDR) { |
| 152 | continue; |
| 153 | } |
| 154 | sa = (struct sockaddr *) (ifa + 1); |
| 155 | sctp_get_rtaddrs(ifa->ifam_addrs, sa, rti_info); |
| 156 | switch (ifa->ifam_type) { |
| 157 | case RTM_DELADDR: |
| 158 | case RTM_NEWADDR: |
| 159 | sctp_handle_ifamsg(ifa->ifam_type, ifa->ifam_index, rti_info[RTAX_IFA]); |
| 160 | break; |
| 161 | default: |
| 162 | /* ignore this routing event */ |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | if (ret < 0) { |
| 167 | if (errno == EAGAIN || errno == EINTR) { |
| 168 | continue; |
| 169 | } else { |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return (NULL); |
| 175 | } |
| 176 | #endif |
| 177 |
| 178 | #if 0 |
| 179 | /* This does not yet work on Linux */ |
| 180 | static void * |
| 181 | recv_function_route(void *arg) |
| 182 | { |
| 183 | int len; |
| 184 | char buf[4096]; |
| 185 | struct iovec iov = { buf, sizeof(buf) }; |
| 186 | struct msghdr msg; |
| 187 | struct nlmsghdr *nh; |
| 188 | struct ifaddrmsg *rtmsg; |
| 189 | struct rtattr *rtatp; |
| 190 | struct in_addr *inp; |
| 191 | struct sockaddr_nl sanl; |
| 192 | #ifdef INET |
| 193 | struct sockaddr_in *sa; |
| 194 | #endif |
| 195 | #ifdef INET6 |
| 196 | struct sockaddr_in6 *sa6; |
| 197 | #endif |
| 198 |
| 199 | for (;;) { |
| 200 | memset(&sanl, 0, sizeof(sanl)); |
| 201 | sanl.nl_family = AF_NETLINK; |
| 202 | sanl.nl_groups = RTMGRP_IPV6_IFADDR | RTMGRP_IPV4_IFADDR; |
| 203 | memset(&msg, 0, sizeof(struct msghdr)); |
| 204 | msg.msg_name = (void *)&sanl; |
| 205 | msg.msg_namelen = sizeof(sanl); |
| 206 | msg.msg_iov = &iov; |
| 207 | msg.msg_iovlen = 1; |
| 208 | msg.msg_control = NULL; |
| 209 | msg.msg_controllen = 0; |
| 210 |
| 211 | len = recvmsg(SCTP_BASE_VAR(userspace_route), &msg, 0); |
| 212 |
| 213 | if (len < 0) { |
| 214 | if (errno == EAGAIN || errno == EINTR) { |
| 215 | continue; |
| 216 | } else { |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len); |
| 221 | nh = NLMSG_NEXT (nh, len)) { |
| 222 | if (nh->nlmsg_type == NLMSG_DONE) |
| 223 | break; |
| 224 |
| 225 | if (nh->nlmsg_type == RTM_NEWADDR || nh->nlmsg_type == RTM_DELADDR) { |
| 226 | rtmsg = (struct ifaddrmsg *)NLMSG_DATA(nh); |
| 227 | rtatp = (struct rtattr *)IFA_RTA(rtmsg); |
| 228 | if (rtatp->rta_type == IFA_ADDRESS) { |
| 229 | inp = (struct in_addr *)RTA_DATA(rtatp); |
| 230 | switch (rtmsg->ifa_family) { |
| 231 | #ifdef INET |
| 232 | case AF_INET: |
| 233 | sa = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in)); |
| 234 | sa->sin_family = rtmsg->ifa_family; |
| 235 | sa->sin_port = 0; |
| 236 | memcpy(&sa->sin_addr, inp, sizeof(struct in_addr)); |
| 237 | sctp_handle_ifamsg(nh->nlmsg_type, rtmsg->ifa_index, (struct sockaddr *)sa); |
| 238 | break; |
| 239 | #endif |
| 240 | #ifdef INET6 |
| 241 | case AF_INET6: |
| 242 | sa6 = (struct sockaddr_in6 *)malloc(sizeof(struct sockaddr_in6)); |
| 243 | sa6->sin6_family = rtmsg->ifa_family; |
| 244 | sa6->sin6_port = 0; |
| 245 | memcpy(&sa6->sin6_addr, inp, sizeof(struct in6_addr)); |
| 246 | sctp_handle_ifamsg(nh->nlmsg_type, rtmsg->ifa_index, (struct sockaddr *)sa6); |
| 247 | break; |
| 248 | #endif |
| 249 | default: |
| 250 | SCTPDBG(SCTP_DEBUG_USR, "Address family %d not supported.\n", rtmsg->ifa_family); |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return (NULL); |
| 258 | } |
| 259 | #endif |
| 260 |
| 261 | #ifdef INET |
| 262 | static void * |
| 263 | recv_function_raw(void *arg) |
| 264 | { |
| 265 | struct mbuf **recvmbuf; |
| 266 | struct ip *iphdr; |
| 267 | struct sctphdr *sh; |
| 268 | uint16_t port; |
| 269 | int offset, ecn = 0; |
| 270 | int compute_crc = 1; |
| 271 | struct sctp_chunkhdr *ch; |
| 272 | struct sockaddr_in src, dst; |
| 273 | #if !defined(__Userspace_os_Windows) |
| 274 | unsigned int ncounter; |
| 275 | struct msghdr msg; |
| 276 | struct iovec recv_iovec[MAXLEN_MBUF_CHAIN]; |
| 277 | #else |
| 278 | WSABUF recv_iovec[MAXLEN_MBUF_CHAIN]; |
| 279 | int nResult, m_ErrorCode; |
| 280 | DWORD flags; |
| 281 | DWORD ncounter; |
| 282 | struct sockaddr_in from; |
| 283 | int fromlen; |
| 284 | #endif |
| 285 | /*Initially the entire set of mbufs is to be allocated. |
| 286 | to_fill indicates this amount. */ |
| 287 | int to_fill = MAXLEN_MBUF_CHAIN; |
| 288 | /* iovlen is the size of each mbuf in the chain */ |
| 289 | int i, n; |
| 290 | unsigned int iovlen = MCLBYTES; |
| 291 | int want_ext = (iovlen > MLEN)? 1 : 0; |
| 292 | int want_header = 0; |
| 293 |
| 294 | sctp_userspace_set_threadname("SCTP/IP4 rcv"); |
| 295 |
| 296 | memset(&src, 0, sizeof(struct sockaddr_in)); |
| 297 | memset(&dst, 0, sizeof(struct sockaddr_in)); |
| 298 |
| 299 | recvmbuf = malloc(sizeof(struct mbuf *) * MAXLEN_MBUF_CHAIN); |
| 300 |
| 301 | while (1) { |
| 302 | for (i = 0; i < to_fill; i++) { |
| 303 | /* Not getting the packet header. Tests with chain of one run |
| 304 | as usual without having the packet header. |
| 305 | Have tried both sending and receiving |
| 306 | */ |
| 307 | recvmbuf[i] = sctp_get_mbuf_for_msg(iovlen, want_header, M_NOWAIT, want_ext, MT_DATA); |
| 308 | #if !defined(__Userspace_os_Windows) |
| 309 | recv_iovec[i].iov_base = (caddr_t)recvmbuf[i]->m_data; |
| 310 | recv_iovec[i].iov_len = iovlen; |
| 311 | #else |
| 312 | recv_iovec[i].buf = (caddr_t)recvmbuf[i]->m_data; |
| 313 | recv_iovec[i].len = iovlen; |
| 314 | #endif |
| 315 | } |
| 316 | to_fill = 0; |
| 317 | #if defined(__Userspace_os_Windows) |
| 318 | flags = 0; |
| 319 | ncounter = 0; |
| 320 | fromlen = sizeof(struct sockaddr_in); |
| 321 | memset(&from, 0, sizeof(struct sockaddr_in)); |
| 322 |
| 323 | nResult = WSARecvFrom(SCTP_BASE_VAR(userspace_rawsctp), recv_iovec, MAXLEN_MBUF_CHAIN, &ncounter, &flags, (struct sockaddr *)&from, &fromlen, NULL, NULL); |
| 324 | if (nResult != 0) { |
| 325 | m_ErrorCode = WSAGetLastError(); |
| 326 | if ((m_ErrorCode == WSAENOTSOCK) || (m_ErrorCode == WSAEINTR)) { |
| 327 | break; |
| 328 | } |
| 329 | continue; |
| 330 | } |
| 331 | n = ncounter; |
| 332 | #else |
| 333 | memset(&msg, 0, sizeof(struct msghdr)); |
| 334 | msg.msg_name = NULL; |
| 335 | msg.msg_namelen = 0; |
| 336 | msg.msg_iov = recv_iovec; |
| 337 | msg.msg_iovlen = MAXLEN_MBUF_CHAIN; |
| 338 | msg.msg_control = NULL; |
| 339 | msg.msg_controllen = 0; |
| 340 | ncounter = n = recvmsg(SCTP_BASE_VAR(userspace_rawsctp), &msg, 0); |
| 341 | if (n < 0) { |
| 342 | if (errno == EAGAIN || errno == EINTR) { |
| 343 | continue; |
| 344 | } else { |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | #endif |
| 349 | SCTP_HEADER_LEN(recvmbuf[0]) = n; /* length of total packet */ |
| 350 | SCTP_STAT_INCR(sctps_recvpackets); |
| 351 | SCTP_STAT_INCR_COUNTER64(sctps_inpackets); |
| 352 |
| 353 | if ((unsigned int)n <= iovlen) { |
| 354 | SCTP_BUF_LEN(recvmbuf[0]) = n; |
| 355 | (to_fill)++; |
| 356 | } else { |
| 357 | i = 0; |
| 358 | SCTP_BUF_LEN(recvmbuf[0]) = iovlen; |
| 359 |
| 360 | ncounter -= min(ncounter, iovlen); |
| 361 | (to_fill)++; |
| 362 | do { |
| 363 | recvmbuf[i]->m_next = recvmbuf[i+1]; |
| 364 | SCTP_BUF_LEN(recvmbuf[i]->m_next) = min(ncounter, iovlen); |
| 365 | i++; |
| 366 | ncounter -= min(ncounter, iovlen); |
| 367 | (to_fill)++; |
| 368 | } while (ncounter > 0); |
| 369 | } |
| 370 |
| 371 | iphdr = mtod(recvmbuf[0], struct ip *); |
| 372 | sh = (struct sctphdr *)((caddr_t)iphdr + sizeof(struct ip)); |
| 373 | ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); |
| 374 | offset = sizeof(struct ip) + sizeof(struct sctphdr); |
| 375 |
| 376 | if (iphdr->ip_tos != 0) { |
| 377 | ecn = iphdr->ip_tos & 0x02; |
| 378 | } |
| 379 |
| 380 | dst.sin_family = AF_INET; |
| 381 | #ifdef HAVE_SIN_LEN |
| 382 | dst.sin_len = sizeof(struct sockaddr_in); |
| 383 | #endif |
| 384 | dst.sin_addr = iphdr->ip_dst; |
| 385 | dst.sin_port = sh->dest_port; |
| 386 |
| 387 | src.sin_family = AF_INET; |
| 388 | #ifdef HAVE_SIN_LEN |
| 389 | src.sin_len = sizeof(struct sockaddr_in); |
| 390 | #endif |
| 391 | src.sin_addr = iphdr->ip_src; |
| 392 | src.sin_port = sh->src_port; |
| 393 |
| 394 | /* SCTP does not allow broadcasts or multicasts */ |
| 395 | if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) { |
| 396 | m_freem(recvmbuf[0]); |
| 397 | continue; |
| 398 | } |
| 399 | if (SCTP_IS_IT_BROADCAST(dst.sin_addr, recvmbuf[0])) { |
| 400 | m_freem(recvmbuf[0]); |
| 401 | continue; |
| 402 | } |
| 403 |
| 404 | port = 0; |
| 405 |
| 406 | if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) && |
| 407 | ((IN4_ISLOOPBACK_ADDRESS(&src.sin_addr) && |
| 408 | IN4_ISLOOPBACK_ADDRESS(&dst.sin_addr)) || |
| 409 | (src.sin_addr.s_addr == dst.sin_addr.s_addr))) { |
| 410 | compute_crc = 0; |
| 411 | SCTP_STAT_INCR(sctps_recvhwcrc); |
| 412 | } else { |
| 413 | SCTP_STAT_INCR(sctps_recvswcrc); |
| 414 | } |
| 415 | SCTPDBG(SCTP_DEBUG_USR, "%s: Received %d bytes.", __func__, n); |
| 416 | SCTPDBG(SCTP_DEBUG_USR, " - calling sctp_common_input_processing with off=%d\n", offset); |
| 417 | sctp_common_input_processing(&recvmbuf[0], sizeof(struct ip), offset, n, |
| 418 | (struct sockaddr *)&src, |
| 419 | (struct sockaddr *)&dst, |
| 420 | sh, ch, |
| 421 | compute_crc, |
| 422 | ecn, |
| 423 | SCTP_DEFAULT_VRFID, port); |
| 424 | if (recvmbuf[0]) { |
| 425 | m_freem(recvmbuf[0]); |
| 426 | } |
| 427 | } |
| 428 | for (i = 0; i < MAXLEN_MBUF_CHAIN; i++) { |
| 429 | m_free(recvmbuf[i]); |
| 430 | } |
| 431 | /* free the array itself */ |
| 432 | free(recvmbuf); |
| 433 | SCTPDBG(SCTP_DEBUG_USR, "%s: Exiting SCTP/IP4 rcv", __func__); |
| 434 | return (NULL); |
| 435 | } |
| 436 | #endif |
| 437 |
| 438 | #if defined(INET6) |
| 439 | static void * |
| 440 | recv_function_raw6(void *arg) |
| 441 | { |
| 442 | struct mbuf **recvmbuf6; |
| 443 | #if !defined(__Userspace_os_Windows) |
| 444 | unsigned int ncounter = 0; |
| 445 | struct iovec recv_iovec[MAXLEN_MBUF_CHAIN]; |
| 446 | struct msghdr msg; |
| 447 | struct cmsghdr *cmsgptr; |
| 448 | char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))]; |
| 449 | #else |
| 450 | WSABUF recv_iovec[MAXLEN_MBUF_CHAIN]; |
| 451 | int nResult, m_ErrorCode; |
| 452 | DWORD ncounter = 0; |
| 453 | struct sockaddr_in6 from; |
| 454 | GUID WSARecvMsg_GUID = WSAID_WSARECVMSG; |
| 455 | LPFN_WSARECVMSG WSARecvMsg; |
| 456 | WSACMSGHDR *cmsgptr; |
| 457 | WSAMSG msg; |
| 458 | char ControlBuffer[1024]; |
| 459 | #endif |
| 460 | struct sockaddr_in6 src, dst; |
| 461 | struct sctphdr *sh; |
| 462 | int offset; |
| 463 | struct sctp_chunkhdr *ch; |
| 464 | /*Initially the entire set of mbufs is to be allocated. |
| 465 | to_fill indicates this amount. */ |
| 466 | int to_fill = MAXLEN_MBUF_CHAIN; |
| 467 | /* iovlen is the size of each mbuf in the chain */ |
| 468 | int i, n; |
| 469 | int compute_crc = 1; |
| 470 | unsigned int iovlen = MCLBYTES; |
| 471 | int want_ext = (iovlen > MLEN)? 1 : 0; |
| 472 | int want_header = 0; |
| 473 |
| 474 | sctp_userspace_set_threadname("SCTP/IP6 rcv"); |
| 475 |
| 476 | recvmbuf6 = malloc(sizeof(struct mbuf *) * MAXLEN_MBUF_CHAIN); |
| 477 |
| 478 | for (;;) { |
| 479 | for (i = 0; i < to_fill; i++) { |
| 480 | /* Not getting the packet header. Tests with chain of one run |
| 481 | as usual without having the packet header. |
| 482 | Have tried both sending and receiving |
| 483 | */ |
| 484 | recvmbuf6[i] = sctp_get_mbuf_for_msg(iovlen, want_header, M_NOWAIT, want_ext, MT_DATA); |
| 485 | #if !defined(__Userspace_os_Windows) |
| 486 | recv_iovec[i].iov_base = (caddr_t)recvmbuf6[i]->m_data; |
| 487 | recv_iovec[i].iov_len = iovlen; |
| 488 | #else |
| 489 | recv_iovec[i].buf = (caddr_t)recvmbuf6[i]->m_data; |
| 490 | recv_iovec[i].len = iovlen; |
| 491 | #endif |
| 492 | } |
| 493 | to_fill = 0; |
| 494 | #if defined(__Userspace_os_Windows) |
| 495 | ncounter = 0; |
| 496 | memset(&from, 0, sizeof(struct sockaddr_in6)); |
| 497 | nResult = WSAIoctl(SCTP_BASE_VAR(userspace_rawsctp6), SIO_GET_EXTENSION_FUNCTION_POINTER, |
| 498 | &WSARecvMsg_GUID, sizeof WSARecvMsg_GUID, |
| 499 | &WSARecvMsg, sizeof WSARecvMsg, |
| 500 | &ncounter, NULL, NULL); |
| 501 | if (nResult == 0) { |
| 502 | msg.name = (void *)&src; |
| 503 | msg.namelen = sizeof(struct sockaddr_in6); |
| 504 | msg.lpBuffers = recv_iovec; |
| 505 | msg.dwBufferCount = MAXLEN_MBUF_CHAIN; |
| 506 | msg.Control.len = sizeof ControlBuffer; |
| 507 | msg.Control.buf = ControlBuffer; |
| 508 | msg.dwFlags = 0; |
| 509 | nResult = WSARecvMsg(SCTP_BASE_VAR(userspace_rawsctp6), &msg, &ncounter, NULL, NULL); |
| 510 | } |
| 511 | if (nResult != 0) { |
| 512 | m_ErrorCode = WSAGetLastError(); |
| 513 | if ((m_ErrorCode == WSAENOTSOCK) || (m_ErrorCode == WSAEINTR)) { |
| 514 | break; |
| 515 | } |
| 516 | continue; |
| 517 | } |
| 518 | n = ncounter; |
| 519 | #else |
| 520 | memset(&msg, 0, sizeof(struct msghdr)); |
| 521 | memset(&src, 0, sizeof(struct sockaddr_in6)); |
| 522 | memset(&dst, 0, sizeof(struct sockaddr_in6)); |
| 523 | memset(cmsgbuf, 0, CMSG_SPACE(sizeof (struct in6_pktinfo))); |
| 524 | msg.msg_name = (void *)&src; |
| 525 | msg.msg_namelen = sizeof(struct sockaddr_in6); |
| 526 | msg.msg_iov = recv_iovec; |
| 527 | msg.msg_iovlen = MAXLEN_MBUF_CHAIN; |
| 528 | msg.msg_control = (void *)cmsgbuf; |
| 529 | msg.msg_controllen = (socklen_t)CMSG_SPACE(sizeof (struct in6_pktinfo)); |
| 530 | msg.msg_flags = 0; |
| 531 |
| 532 | ncounter = n = recvmsg(SCTP_BASE_VAR(userspace_rawsctp6), &msg, 0); |
| 533 | if (n < 0) { |
| 534 | if (errno == EAGAIN || errno == EINTR) { |
| 535 | continue; |
| 536 | } else { |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | #endif |
| 541 | SCTP_HEADER_LEN(recvmbuf6[0]) = n; /* length of total packet */ |
| 542 | SCTP_STAT_INCR(sctps_recvpackets); |
| 543 | SCTP_STAT_INCR_COUNTER64(sctps_inpackets); |
| 544 |
| 545 | if ((unsigned int)n <= iovlen) { |
| 546 | SCTP_BUF_LEN(recvmbuf6[0]) = n; |
| 547 | (to_fill)++; |
| 548 | } else { |
| 549 | i = 0; |
| 550 | SCTP_BUF_LEN(recvmbuf6[0]) = iovlen; |
| 551 |
| 552 | ncounter -= min(ncounter, iovlen); |
| 553 | (to_fill)++; |
| 554 | do { |
| 555 | recvmbuf6[i]->m_next = recvmbuf6[i+1]; |
| 556 | SCTP_BUF_LEN(recvmbuf6[i]->m_next) = min(ncounter, iovlen); |
| 557 | i++; |
| 558 | ncounter -= min(ncounter, iovlen); |
| 559 | (to_fill)++; |
| 560 | } while (ncounter > 0); |
| 561 | } |
| 562 |
| 563 | for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { |
| 564 | if ((cmsgptr->cmsg_level == IPPROTO_IPV6) && (cmsgptr->cmsg_type == IPV6_PKTINFO)) { |
| 565 | struct in6_pktinfo * info; |
| 566 |
| 567 | info = (struct in6_pktinfo *)CMSG_DATA(cmsgptr); |
| 568 | memcpy((void *)&dst.sin6_addr, (const void *) &(info->ipi6_addr), sizeof(struct in6_addr)); |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 |
| 573 | /* SCTP does not allow broadcasts or multicasts */ |
| 574 | if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr)) { |
| 575 | m_freem(recvmbuf6[0]); |
| 576 | continue; |
| 577 | } |
| 578 |
| 579 | sh = mtod(recvmbuf6[0], struct sctphdr *); |
| 580 | ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); |
| 581 | offset = sizeof(struct sctphdr); |
| 582 |
| 583 | dst.sin6_family = AF_INET6; |
| 584 | #ifdef HAVE_SIN6_LEN |
| 585 | dst.sin6_len = sizeof(struct sockaddr_in6); |
| 586 | #endif |
| 587 | dst.sin6_port = sh->dest_port; |
| 588 |
| 589 | src.sin6_family = AF_INET6; |
| 590 | #ifdef HAVE_SIN6_LEN |
| 591 | src.sin6_len = sizeof(struct sockaddr_in6); |
| 592 | #endif |
| 593 | src.sin6_port = sh->src_port; |
| 594 | if (memcmp(&src.sin6_addr, &dst.sin6_addr, sizeof(struct in6_addr)) == 0) { |
| 595 | compute_crc = 0; |
| 596 | SCTP_STAT_INCR(sctps_recvhwcrc); |
| 597 | } else { |
| 598 | SCTP_STAT_INCR(sctps_recvswcrc); |
| 599 | } |
| 600 | SCTPDBG(SCTP_DEBUG_USR, "%s: Received %d bytes.", __func__, n); |
| 601 | SCTPDBG(SCTP_DEBUG_USR, " - calling sctp_common_input_processing with off=%d\n", offset); |
| 602 | sctp_common_input_processing(&recvmbuf6[0], 0, offset, n, |
| 603 | (struct sockaddr *)&src, |
| 604 | (struct sockaddr *)&dst, |
| 605 | sh, ch, |
| 606 | compute_crc, |
| 607 | 0, |
| 608 | SCTP_DEFAULT_VRFID, 0); |
| 609 | if (recvmbuf6[0]) { |
| 610 | m_freem(recvmbuf6[0]); |
| 611 | } |
| 612 | } |
| 613 | for (i = 0; i < MAXLEN_MBUF_CHAIN; i++) { |
| 614 | m_free(recvmbuf6[i]); |
| 615 | } |
| 616 | /* free the array itself */ |
| 617 | free(recvmbuf6); |
| 618 | SCTPDBG(SCTP_DEBUG_USR, "%s: Exiting SCTP/IP6 rcv", __func__); |
| 619 | return (NULL); |
| 620 | } |
| 621 | #endif |
| 622 |
| 623 | #ifdef INET |
| 624 | static void * |
| 625 | recv_function_udp(void *arg) |
| 626 | { |
| 627 | struct mbuf **udprecvmbuf; |
| 628 | /*Initially the entire set of mbufs is to be allocated. |
| 629 | to_fill indicates this amount. */ |
| 630 | int to_fill = MAXLEN_MBUF_CHAIN; |
| 631 | /* iovlen is the size of each mbuf in the chain */ |
| 632 | int i, n, offset; |
| 633 | unsigned int iovlen = MCLBYTES; |
| 634 | int want_ext = (iovlen > MLEN)? 1 : 0; |
| 635 | int want_header = 0; |
| 636 | struct sctphdr *sh; |
| 637 | uint16_t port; |
| 638 | struct sctp_chunkhdr *ch; |
| 639 | struct sockaddr_in src, dst; |
| 640 | #if defined(IP_PKTINFO) |
| 641 | char cmsgbuf[CMSG_SPACE(sizeof(struct in_pktinfo))]; |
| 642 | #else |
| 643 | char cmsgbuf[CMSG_SPACE(sizeof(struct in_addr))]; |
| 644 | #endif |
| 645 | int compute_crc = 1; |
| 646 | #if !defined(__Userspace_os_Windows) |
| 647 | unsigned int ncounter; |
| 648 | struct iovec iov[MAXLEN_MBUF_CHAIN]; |
| 649 | struct msghdr msg; |
| 650 | struct cmsghdr *cmsgptr; |
| 651 | #else |
| 652 | GUID WSARecvMsg_GUID = WSAID_WSARECVMSG; |
| 653 | LPFN_WSARECVMSG WSARecvMsg; |
| 654 | char ControlBuffer[1024]; |
| 655 | WSABUF iov[MAXLEN_MBUF_CHAIN]; |
| 656 | WSAMSG msg; |
| 657 | int nResult, m_ErrorCode; |
| 658 | WSACMSGHDR *cmsgptr; |
| 659 | DWORD ncounter; |
| 660 | #endif |
| 661 |
| 662 | sctp_userspace_set_threadname("SCTP/UDP/IP4 rcv"); |
| 663 |
| 664 | udprecvmbuf = malloc(sizeof(struct mbuf *) * MAXLEN_MBUF_CHAIN); |
| 665 |
| 666 | while (1) { |
| 667 | for (i = 0; i < to_fill; i++) { |
| 668 | /* Not getting the packet header. Tests with chain of one run |
| 669 | as usual without having the packet header. |
| 670 | Have tried both sending and receiving |
| 671 | */ |
| 672 | udprecvmbuf[i] = sctp_get_mbuf_for_msg(iovlen, want_header, M_NOWAIT, want_ext, MT_DATA); |
| 673 | #if !defined(__Userspace_os_Windows) |
| 674 | iov[i].iov_base = (caddr_t)udprecvmbuf[i]->m_data; |
| 675 | iov[i].iov_len = iovlen; |
| 676 | #else |
| 677 | iov[i].buf = (caddr_t)udprecvmbuf[i]->m_data; |
| 678 | iov[i].len = iovlen; |
| 679 | #endif |
| 680 | } |
| 681 | to_fill = 0; |
| 682 | #if !defined(__Userspace_os_Windows) |
| 683 | memset(&msg, 0, sizeof(struct msghdr)); |
| 684 | #else |
| 685 | memset(&msg, 0, sizeof(WSAMSG)); |
| 686 | #endif |
| 687 | memset(&src, 0, sizeof(struct sockaddr_in)); |
| 688 | memset(&dst, 0, sizeof(struct sockaddr_in)); |
| 689 | memset(cmsgbuf, 0, sizeof(cmsgbuf)); |
| 690 |
| 691 | #if !defined(__Userspace_os_Windows) |
| 692 | msg.msg_name = (void *)&src; |
| 693 | msg.msg_namelen = sizeof(struct sockaddr_in); |
| 694 | msg.msg_iov = iov; |
| 695 | msg.msg_iovlen = MAXLEN_MBUF_CHAIN; |
| 696 | msg.msg_control = (void *)cmsgbuf; |
| 697 | msg.msg_controllen = sizeof(cmsgbuf); |
| 698 | msg.msg_flags = 0; |
| 699 |
| 700 | ncounter = n = recvmsg(SCTP_BASE_VAR(userspace_udpsctp), &msg, 0); |
| 701 | if (n < 0) { |
| 702 | if (errno == EAGAIN || errno == EINTR) { |
| 703 | continue; |
| 704 | } else { |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | #else |
| 709 | nResult = WSAIoctl(SCTP_BASE_VAR(userspace_udpsctp), SIO_GET_EXTENSION_FUNCTION_POINTER, |
| 710 | &WSARecvMsg_GUID, sizeof WSARecvMsg_GUID, |
| 711 | &WSARecvMsg, sizeof WSARecvMsg, |
| 712 | &ncounter, NULL, NULL); |
| 713 | if (nResult == 0) { |
| 714 | msg.name = (void *)&src; |
| 715 | msg.namelen = sizeof(struct sockaddr_in); |
| 716 | msg.lpBuffers = iov; |
| 717 | msg.dwBufferCount = MAXLEN_MBUF_CHAIN; |
| 718 | msg.Control.len = sizeof ControlBuffer; |
| 719 | msg.Control.buf = ControlBuffer; |
| 720 | msg.dwFlags = 0; |
| 721 | nResult = WSARecvMsg(SCTP_BASE_VAR(userspace_udpsctp), &msg, &ncounter, NULL, NULL); |
| 722 | } |
| 723 | if (nResult != 0) { |
| 724 | m_ErrorCode = WSAGetLastError(); |
| 725 | if ((m_ErrorCode == WSAENOTSOCK) || (m_ErrorCode == WSAEINTR)) { |
| 726 | break; |
| 727 | } |
| 728 | continue; |
| 729 | } |
| 730 | n = ncounter; |
| 731 | #endif |
| 732 | SCTP_HEADER_LEN(udprecvmbuf[0]) = n; /* length of total packet */ |
| 733 | SCTP_STAT_INCR(sctps_recvpackets); |
| 734 | SCTP_STAT_INCR_COUNTER64(sctps_inpackets); |
| 735 |
| 736 | if ((unsigned int)n <= iovlen) { |
| 737 | SCTP_BUF_LEN(udprecvmbuf[0]) = n; |
| 738 | (to_fill)++; |
| 739 | } else { |
| 740 | i = 0; |
| 741 | SCTP_BUF_LEN(udprecvmbuf[0]) = iovlen; |
| 742 |
| 743 | ncounter -= min(ncounter, iovlen); |
| 744 | (to_fill)++; |
| 745 | do { |
| 746 | udprecvmbuf[i]->m_next = udprecvmbuf[i+1]; |
| 747 | SCTP_BUF_LEN(udprecvmbuf[i]->m_next) = min(ncounter, iovlen); |
| 748 | i++; |
| 749 | ncounter -= min(ncounter, iovlen); |
| 750 | (to_fill)++; |
| 751 | } while (ncounter > 0); |
| 752 | } |
| 753 |
| 754 | for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { |
| 755 | #if defined(IP_PKTINFO) |
| 756 | if ((cmsgptr->cmsg_level == IPPROTO_IP) && (cmsgptr->cmsg_type == IP_PKTINFO)) { |
| 757 | struct in_pktinfo *info; |
| 758 |
| 759 | dst.sin_family = AF_INET; |
| 760 | #ifdef HAVE_SIN_LEN |
| 761 | dst.sin_len = sizeof(struct sockaddr_in); |
| 762 | #endif |
| 763 | info = (struct in_pktinfo *)CMSG_DATA(cmsgptr); |
| 764 | memcpy((void *)&dst.sin_addr, (const void *)&(info->ipi_addr), sizeof(struct in_addr)); |
| 765 | break; |
| 766 | } |
| 767 | #else |
| 768 | if ((cmsgptr->cmsg_level == IPPROTO_IP) && (cmsgptr->cmsg_type == IP_RECVDSTADDR)) { |
| 769 | struct in_addr *addr; |
| 770 |
| 771 | dst.sin_family = AF_INET; |
| 772 | #ifdef HAVE_SIN_LEN |
| 773 | dst.sin_len = sizeof(struct sockaddr_in); |
| 774 | #endif |
| 775 | addr = (struct in_addr *)CMSG_DATA(cmsgptr); |
| 776 | memcpy((void *)&dst.sin_addr, (const void *)addr, sizeof(struct in_addr)); |
| 777 | break; |
| 778 | } |
| 779 | #endif |
| 780 | } |
| 781 |
| 782 | /* SCTP does not allow broadcasts or multicasts */ |
| 783 | if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) { |
| 784 | m_freem(udprecvmbuf[0]); |
| 785 | continue; |
| 786 | } |
| 787 | if (SCTP_IS_IT_BROADCAST(dst.sin_addr, udprecvmbuf[0])) { |
| 788 | m_freem(udprecvmbuf[0]); |
| 789 | continue; |
| 790 | } |
| 791 |
| 792 | /*offset = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);*/ |
| 793 | sh = mtod(udprecvmbuf[0], struct sctphdr *); |
| 794 | ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); |
| 795 | offset = sizeof(struct sctphdr); |
| 796 | port = src.sin_port; |
| 797 | src.sin_port = sh->src_port; |
| 798 | dst.sin_port = sh->dest_port; |
| 799 | if (src.sin_addr.s_addr == dst.sin_addr.s_addr) { |
| 800 | compute_crc = 0; |
| 801 | SCTP_STAT_INCR(sctps_recvhwcrc); |
| 802 | } else { |
| 803 | SCTP_STAT_INCR(sctps_recvswcrc); |
| 804 | } |
| 805 | SCTPDBG(SCTP_DEBUG_USR, "%s: Received %d bytes.", __func__, n); |
| 806 | SCTPDBG(SCTP_DEBUG_USR, " - calling sctp_common_input_processing with off=%d\n", offset); |
| 807 | sctp_common_input_processing(&udprecvmbuf[0], 0, offset, n, |
| 808 | (struct sockaddr *)&src, |
| 809 | (struct sockaddr *)&dst, |
| 810 | sh, ch, |
| 811 | compute_crc, |
| 812 | 0, |
| 813 | SCTP_DEFAULT_VRFID, port); |
| 814 | if (udprecvmbuf[0]) { |
| 815 | m_freem(udprecvmbuf[0]); |
| 816 | } |
| 817 | } |
| 818 | for (i = 0; i < MAXLEN_MBUF_CHAIN; i++) { |
| 819 | m_free(udprecvmbuf[i]); |
| 820 | } |
| 821 | /* free the array itself */ |
| 822 | free(udprecvmbuf); |
| 823 | SCTPDBG(SCTP_DEBUG_USR, "%s: Exiting SCTP/UDP/IP4 rcv", __func__); |
| 824 | return (NULL); |
| 825 | } |
| 826 | #endif |
| 827 |
| 828 | #if defined(INET6) |
| 829 | static void * |
| 830 | recv_function_udp6(void *arg) |
| 831 | { |
| 832 | struct mbuf **udprecvmbuf6; |
| 833 | /*Initially the entire set of mbufs is to be allocated. |
| 834 | to_fill indicates this amount. */ |
| 835 | int to_fill = MAXLEN_MBUF_CHAIN; |
| 836 | /* iovlen is the size of each mbuf in the chain */ |
| 837 | int i, n, offset; |
| 838 | unsigned int iovlen = MCLBYTES; |
| 839 | int want_ext = (iovlen > MLEN)? 1 : 0; |
| 840 | int want_header = 0; |
| 841 | struct sockaddr_in6 src, dst; |
| 842 | struct sctphdr *sh; |
| 843 | uint16_t port; |
| 844 | struct sctp_chunkhdr *ch; |
| 845 | char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))]; |
| 846 | int compute_crc = 1; |
| 847 | #if !defined(__Userspace_os_Windows) |
| 848 | struct iovec iov[MAXLEN_MBUF_CHAIN]; |
| 849 | struct msghdr msg; |
| 850 | struct cmsghdr *cmsgptr; |
| 851 | unsigned int ncounter; |
| 852 | #else |
| 853 | GUID WSARecvMsg_GUID = WSAID_WSARECVMSG; |
| 854 | LPFN_WSARECVMSG WSARecvMsg; |
| 855 | char ControlBuffer[1024]; |
| 856 | WSABUF iov[MAXLEN_MBUF_CHAIN]; |
| 857 | WSAMSG msg; |
| 858 | int nResult, m_ErrorCode; |
| 859 | WSACMSGHDR *cmsgptr; |
| 860 | DWORD ncounter; |
| 861 | #endif |
| 862 |
| 863 | sctp_userspace_set_threadname("SCTP/UDP/IP6 rcv"); |
| 864 |
| 865 | udprecvmbuf6 = malloc(sizeof(struct mbuf *) * MAXLEN_MBUF_CHAIN); |
| 866 | while (1) { |
| 867 | for (i = 0; i < to_fill; i++) { |
| 868 | /* Not getting the packet header. Tests with chain of one run |
| 869 | as usual without having the packet header. |
| 870 | Have tried both sending and receiving |
| 871 | */ |
| 872 | udprecvmbuf6[i] = sctp_get_mbuf_for_msg(iovlen, want_header, M_NOWAIT, want_ext, MT_DATA); |
| 873 | #if !defined(__Userspace_os_Windows) |
| 874 | iov[i].iov_base = (caddr_t)udprecvmbuf6[i]->m_data; |
| 875 | iov[i].iov_len = iovlen; |
| 876 | #else |
| 877 | iov[i].buf = (caddr_t)udprecvmbuf6[i]->m_data; |
| 878 | iov[i].len = iovlen; |
| 879 | #endif |
| 880 | } |
| 881 | to_fill = 0; |
| 882 |
| 883 | #if !defined(__Userspace_os_Windows) |
| 884 | memset(&msg, 0, sizeof(struct msghdr)); |
| 885 | #else |
| 886 | memset(&msg, 0, sizeof(WSAMSG)); |
| 887 | #endif |
| 888 | memset(&src, 0, sizeof(struct sockaddr_in6)); |
| 889 | memset(&dst, 0, sizeof(struct sockaddr_in6)); |
| 890 | memset(cmsgbuf, 0, CMSG_SPACE(sizeof (struct in6_pktinfo))); |
| 891 |
| 892 | #if !defined(__Userspace_os_Windows) |
| 893 | msg.msg_name = (void *)&src; |
| 894 | msg.msg_namelen = sizeof(struct sockaddr_in6); |
| 895 | msg.msg_iov = iov; |
| 896 | msg.msg_iovlen = MAXLEN_MBUF_CHAIN; |
| 897 | msg.msg_control = (void *)cmsgbuf; |
| 898 | msg.msg_controllen = (socklen_t)CMSG_SPACE(sizeof (struct in6_pktinfo)); |
| 899 | msg.msg_flags = 0; |
| 900 |
| 901 | ncounter = n = recvmsg(SCTP_BASE_VAR(userspace_udpsctp6), &msg, 0); |
| 902 | if (n < 0) { |
| 903 | if (errno == EAGAIN || errno == EINTR) { |
| 904 | continue; |
| 905 | } else { |
| 906 | break; |
| 907 | } |
| 908 | } |
| 909 | #else |
| 910 | nResult = WSAIoctl(SCTP_BASE_VAR(userspace_udpsctp6), SIO_GET_EXTENSION_FUNCTION_POINTER, |
| 911 | &WSARecvMsg_GUID, sizeof WSARecvMsg_GUID, |
| 912 | &WSARecvMsg, sizeof WSARecvMsg, |
| 913 | &ncounter, NULL, NULL); |
| 914 | if (nResult == SOCKET_ERROR) { |
| 915 | m_ErrorCode = WSAGetLastError(); |
| 916 | WSARecvMsg = NULL; |
| 917 | } |
| 918 | if (nResult == 0) { |
| 919 | msg.name = (void *)&src; |
| 920 | msg.namelen = sizeof(struct sockaddr_in6); |
| 921 | msg.lpBuffers = iov; |
| 922 | msg.dwBufferCount = MAXLEN_MBUF_CHAIN; |
| 923 | msg.Control.len = sizeof ControlBuffer; |
| 924 | msg.Control.buf = ControlBuffer; |
| 925 | msg.dwFlags = 0; |
| 926 | nResult = WSARecvMsg(SCTP_BASE_VAR(userspace_udpsctp6), &msg, &ncounter, NULL, NULL); |
| 927 | } |
| 928 | if (nResult != 0) { |
| 929 | m_ErrorCode = WSAGetLastError(); |
| 930 | if ((m_ErrorCode == WSAENOTSOCK) || (m_ErrorCode == WSAEINTR)) { |
| 931 | break; |
| 932 | } |
| 933 | continue; |
| 934 | } |
| 935 | n = ncounter; |
| 936 | #endif |
| 937 | SCTP_HEADER_LEN(udprecvmbuf6[0]) = n; /* length of total packet */ |
| 938 | SCTP_STAT_INCR(sctps_recvpackets); |
| 939 | SCTP_STAT_INCR_COUNTER64(sctps_inpackets); |
| 940 |
| 941 | if ((unsigned int)n <= iovlen) { |
| 942 | SCTP_BUF_LEN(udprecvmbuf6[0]) = n; |
| 943 | (to_fill)++; |
| 944 | } else { |
| 945 | i = 0; |
| 946 | SCTP_BUF_LEN(udprecvmbuf6[0]) = iovlen; |
| 947 |
| 948 | ncounter -= min(ncounter, iovlen); |
| 949 | (to_fill)++; |
| 950 | do { |
| 951 | udprecvmbuf6[i]->m_next = udprecvmbuf6[i+1]; |
| 952 | SCTP_BUF_LEN(udprecvmbuf6[i]->m_next) = min(ncounter, iovlen); |
| 953 | i++; |
| 954 | ncounter -= min(ncounter, iovlen); |
| 955 | (to_fill)++; |
| 956 | } while (ncounter > 0); |
| 957 | } |
| 958 |
| 959 | for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { |
| 960 | if ((cmsgptr->cmsg_level == IPPROTO_IPV6) && (cmsgptr->cmsg_type == IPV6_PKTINFO)) { |
| 961 | struct in6_pktinfo *info; |
| 962 |
| 963 | dst.sin6_family = AF_INET6; |
| 964 | #ifdef HAVE_SIN6_LEN |
| 965 | dst.sin6_len = sizeof(struct sockaddr_in6); |
| 966 | #endif |
| 967 | info = (struct in6_pktinfo *)CMSG_DATA(cmsgptr); |
| 968 | /*dst.sin6_port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));*/ |
| 969 | memcpy((void *)&dst.sin6_addr, (const void *)&(info->ipi6_addr), sizeof(struct in6_addr)); |
| 970 | } |
| 971 | } |
| 972 |
| 973 | /* SCTP does not allow broadcasts or multicasts */ |
| 974 | if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr)) { |
| 975 | m_freem(udprecvmbuf6[0]); |
| 976 | continue; |
| 977 | } |
| 978 |
| 979 | sh = mtod(udprecvmbuf6[0], struct sctphdr *); |
| 980 | ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); |
| 981 | offset = sizeof(struct sctphdr); |
| 982 |
| 983 | port = src.sin6_port; |
| 984 | src.sin6_port = sh->src_port; |
| 985 | dst.sin6_port = sh->dest_port; |
| 986 | if ((memcmp(&src.sin6_addr, &dst.sin6_addr, sizeof(struct in6_addr)) == 0)) { |
| 987 | compute_crc = 0; |
| 988 | SCTP_STAT_INCR(sctps_recvhwcrc); |
| 989 | } else { |
| 990 | SCTP_STAT_INCR(sctps_recvswcrc); |
| 991 | } |
| 992 | SCTPDBG(SCTP_DEBUG_USR, "%s: Received %d bytes.", __func__, n); |
| 993 | SCTPDBG(SCTP_DEBUG_USR, " - calling sctp_common_input_processing with off=%d\n", (int)sizeof(struct sctphdr)); |
| 994 | sctp_common_input_processing(&udprecvmbuf6[0], 0, offset, n, |
| 995 | (struct sockaddr *)&src, |
| 996 | (struct sockaddr *)&dst, |
| 997 | sh, ch, |
| 998 | compute_crc, |
| 999 | 0, |
| 1000 | SCTP_DEFAULT_VRFID, port); |
| 1001 | if (udprecvmbuf6[0]) { |
| 1002 | m_freem(udprecvmbuf6[0]); |
| 1003 | } |
| 1004 | } |
| 1005 | for (i = 0; i < MAXLEN_MBUF_CHAIN; i++) { |
| 1006 | m_free(udprecvmbuf6[i]); |
| 1007 | } |
| 1008 | /* free the array itself */ |
| 1009 | free(udprecvmbuf6); |
| 1010 | SCTPDBG(SCTP_DEBUG_USR, "%s: Exiting SCTP/UDP/IP6 rcv", __func__); |
| 1011 | return (NULL); |
| 1012 | } |
| 1013 | #endif |
| 1014 |
| 1015 | #if defined (__Userspace_os_Windows) |
| 1016 | static void |
| 1017 | setReceiveBufferSize(SOCKET sfd, int new_size) |
| 1018 | #else |
| 1019 | static void |
| 1020 | setReceiveBufferSize(int sfd, int new_size) |
| 1021 | #endif |
| 1022 | { |
| 1023 | int ch = new_size; |
| 1024 |
| 1025 | if (setsockopt (sfd, SOL_SOCKET, SO_RCVBUF, (void*)&ch, sizeof(ch)) < 0) { |
| 1026 | #if defined (__Userspace_os_Windows) |
| 1027 | SCTPDBG(SCTP_DEBUG_USR, "Can't set recv-buffers size (errno = %d).\n", WSAGetLastError()); |
| 1028 | #else |
| 1029 | SCTPDBG(SCTP_DEBUG_USR, "Can't set recv-buffers size (errno = %d).\n", errno); |
| 1030 | #endif |
| 1031 | } |
| 1032 | return; |
| 1033 | } |
| 1034 |
| 1035 | #if defined (__Userspace_os_Windows) |
| 1036 | static void |
| 1037 | setSendBufferSize(SOCKET sfd, int new_size) |
| 1038 | #else |
| 1039 | static void |
| 1040 | setSendBufferSize(int sfd, int new_size) |
| 1041 | #endif |
| 1042 | { |
| 1043 | int ch = new_size; |
| 1044 |
| 1045 | if (setsockopt (sfd, SOL_SOCKET, SO_SNDBUF, (void*)&ch, sizeof(ch)) < 0) { |
| 1046 | #if defined (__Userspace_os_Windows) |
| 1047 | SCTPDBG(SCTP_DEBUG_USR, "Can't set send-buffers size (errno = %d).\n", WSAGetLastError()); |
| 1048 | #else |
| 1049 | SCTPDBG(SCTP_DEBUG_USR, "Can't set send-buffers size (errno = %d).\n", errno); |
| 1050 | #endif |
| 1051 | } |
| 1052 | return; |
| 1053 | } |
| 1054 |
| 1055 | #define SOCKET_TIMEOUT 100 /* in ms */ |
| 1056 | void |
| 1057 | recv_thread_init(void) |
| 1058 | { |
| 1059 | #if defined(INET) |
| 1060 | struct sockaddr_in addr_ipv4; |
| 1061 | const int hdrincl = 1; |
| 1062 | #endif |
| 1063 | #if defined(INET6) |
| 1064 | struct sockaddr_in6 addr_ipv6; |
| 1065 | #endif |
| 1066 | #if defined(INET) || defined(INET6) |
| 1067 | const int on = 1; |
| 1068 | #endif |
| 1069 | #if !defined(__Userspace_os_Windows) |
| 1070 | struct timeval timeout; |
| 1071 |
| 1072 | memset(&timeout, 0, sizeof(struct timeval)); |
| 1073 | timeout.tv_sec = (SOCKET_TIMEOUT / 1000); |
| 1074 | timeout.tv_usec = (SOCKET_TIMEOUT % 1000) * 1000; |
| 1075 | #else |
| 1076 | unsigned int timeout = SOCKET_TIMEOUT; /* Timeout in milliseconds */ |
| 1077 | #endif |
| 1078 | #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) |
| 1079 | if (SCTP_BASE_VAR(userspace_route) == -1) { |
| 1080 | if ((SCTP_BASE_VAR(userspace_route) = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) { |
| 1081 | SCTPDBG(SCTP_DEBUG_USR, "Can't create routing socket (errno = %d).\n", errno); |
| 1082 | } |
| 1083 | #if 0 |
| 1084 | struct sockaddr_nl sanl; |
| 1085 |
| 1086 | if ((SCTP_BASE_VAR(userspace_route) = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) { |
| 1087 | SCTPDBG(SCTP_DEBUG_USR, "Can't create routing socket (errno = %d.\n", errno); |
| 1088 | } |
| 1089 | memset(&sanl, 0, sizeof(sanl)); |
| 1090 | sanl.nl_family = AF_NETLINK; |
| 1091 | sanl.nl_groups = 0; |
| 1092 | #ifdef INET |
| 1093 | sanl.nl_groups |= RTMGRP_IPV4_IFADDR; |
| 1094 | #endif |
| 1095 | #ifdef INET6 |
| 1096 | sanl.nl_groups |= RTMGRP_IPV6_IFADDR; |
| 1097 | #endif |
| 1098 | if (bind(SCTP_BASE_VAR(userspace_route), (struct sockaddr *) &sanl, sizeof(sanl)) < 0) { |
| 1099 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind routing socket (errno = %d).\n", errno); |
| 1100 | close(SCTP_BASE_VAR(userspace_route)); |
| 1101 | SCTP_BASE_VAR(userspace_route) = -1; |
| 1102 | } |
| 1103 | #endif |
| 1104 | if (SCTP_BASE_VAR(userspace_route) != -1) { |
| 1105 | if (setsockopt(SCTP_BASE_VAR(userspace_route), SOL_SOCKET, SO_RCVTIMEO,(const void*)&timeout, sizeof(struct timeval)) < 0) { |
| 1106 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on routing socket (errno = %d).\n", errno); |
| 1107 | #if defined(__Userspace_os_Windows) |
| 1108 | closesocket(SCTP_BASE_VAR(userspace_route)); |
| 1109 | #else |
| 1110 | close(SCTP_BASE_VAR(userspace_route)); |
| 1111 | #endif |
| 1112 | SCTP_BASE_VAR(userspace_route) = -1; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | #endif |
| 1117 | #if defined(INET) |
| 1118 | if (SCTP_BASE_VAR(userspace_rawsctp) == -1) { |
| 1119 | if ((SCTP_BASE_VAR(userspace_rawsctp) = socket(AF_INET, SOCK_RAW, IPPROTO_SCTP)) == -1) { |
| 1120 | #if defined(__Userspace_os_Windows) |
| 1121 | SCTPDBG(SCTP_DEBUG_USR, "Can't create raw socket for IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1122 | #else |
| 1123 | SCTPDBG(SCTP_DEBUG_USR, "Can't create raw socket for IPv4 (errno = %d).\n", errno); |
| 1124 | #endif |
| 1125 | } else { |
| 1126 | /* complete setting up the raw SCTP socket */ |
| 1127 | if (setsockopt(SCTP_BASE_VAR(userspace_rawsctp), IPPROTO_IP, IP_HDRINCL,(const void*)&hdrincl, sizeof(int)) < 0) { |
| 1128 | #if defined(__Userspace_os_Windows) |
| 1129 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IP_HDRINCL (errno = %d).\n", WSAGetLastError()); |
| 1130 | closesocket(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1131 | #else |
| 1132 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IP_HDRINCL (errno = %d).\n", errno); |
| 1133 | close(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1134 | #endif |
| 1135 | SCTP_BASE_VAR(userspace_rawsctp) = -1; |
| 1136 | } else if (setsockopt(SCTP_BASE_VAR(userspace_rawsctp), SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(timeout)) < 0) { |
| 1137 | #if defined(__Userspace_os_Windows) |
| 1138 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1139 | closesocket(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1140 | #else |
| 1141 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/IPv4 (errno = %d).\n", errno); |
| 1142 | close(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1143 | #endif |
| 1144 | SCTP_BASE_VAR(userspace_rawsctp) = -1; |
| 1145 | } else { |
| 1146 | memset((void *)&addr_ipv4, 0, sizeof(struct sockaddr_in)); |
| 1147 | #ifdef HAVE_SIN_LEN |
| 1148 | addr_ipv4.sin_len = sizeof(struct sockaddr_in); |
| 1149 | #endif |
| 1150 | addr_ipv4.sin_family = AF_INET; |
| 1151 | addr_ipv4.sin_port = htons(0); |
| 1152 | addr_ipv4.sin_addr.s_addr = htonl(INADDR_ANY); |
| 1153 | if (bind(SCTP_BASE_VAR(userspace_rawsctp), (const struct sockaddr *)&addr_ipv4, sizeof(struct sockaddr_in)) < 0) { |
| 1154 | #if defined(__Userspace_os_Windows) |
| 1155 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1156 | closesocket(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1157 | #else |
| 1158 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/IPv4 (errno = %d).\n", errno); |
| 1159 | close(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1160 | #endif |
| 1161 | SCTP_BASE_VAR(userspace_rawsctp) = -1; |
| 1162 | } else { |
| 1163 | setReceiveBufferSize(SCTP_BASE_VAR(userspace_rawsctp), SB_RAW); /* 128K */ |
| 1164 | setSendBufferSize(SCTP_BASE_VAR(userspace_rawsctp), SB_RAW); /* 128K Is this setting net.inet.raw.maxdgram value? Should it be set to 64K? */ |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | if (SCTP_BASE_VAR(userspace_udpsctp) == -1) { |
| 1170 | if ((SCTP_BASE_VAR(userspace_udpsctp) = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
| 1171 | #if defined(__Userspace_os_Windows) |
| 1172 | SCTPDBG(SCTP_DEBUG_USR, "Can't create socket for SCTP/UDP/IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1173 | #else |
| 1174 | SCTPDBG(SCTP_DEBUG_USR, "Can't create socket for SCTP/UDP/IPv4 (errno = %d).\n", errno); |
| 1175 | #endif |
| 1176 | } else { |
| 1177 | #if defined(IP_PKTINFO) |
| 1178 | if (setsockopt(SCTP_BASE_VAR(userspace_udpsctp), IPPROTO_IP, IP_PKTINFO, (const void *)&on, (int)sizeof(int)) < 0) { |
| 1179 | #else |
| 1180 | if (setsockopt(SCTP_BASE_VAR(userspace_udpsctp), IPPROTO_IP, IP_RECVDSTADDR, (const void *)&on, (int)sizeof(int)) < 0) { |
| 1181 | #endif |
| 1182 | #if defined(__Userspace_os_Windows) |
| 1183 | #if defined(IP_PKTINFO) |
| 1184 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IP_PKTINFO on socket for SCTP/UDP/IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1185 | #else |
| 1186 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IP_RECVDSTADDR on socket for SCTP/UDP/IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1187 | #endif |
| 1188 | closesocket(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1189 | #else |
| 1190 | #if defined(IP_PKTINFO) |
| 1191 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IP_PKTINFO on socket for SCTP/UDP/IPv4 (errno = %d).\n", errno); |
| 1192 | #else |
| 1193 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IP_RECVDSTADDR on socket for SCTP/UDP/IPv4 (errno = %d).\n", errno); |
| 1194 | #endif |
| 1195 | close(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1196 | #endif |
| 1197 | SCTP_BASE_VAR(userspace_udpsctp) = -1; |
| 1198 | } else if (setsockopt(SCTP_BASE_VAR(userspace_udpsctp), SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(timeout)) < 0) { |
| 1199 | #if defined(__Userspace_os_Windows) |
| 1200 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/UDP/IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1201 | closesocket(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1202 | #else |
| 1203 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/UDP/IPv4 (errno = %d).\n", errno); |
| 1204 | close(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1205 | #endif |
| 1206 | SCTP_BASE_VAR(userspace_udpsctp) = -1; |
| 1207 | } else { |
| 1208 | memset((void *)&addr_ipv4, 0, sizeof(struct sockaddr_in)); |
| 1209 | #ifdef HAVE_SIN_LEN |
| 1210 | addr_ipv4.sin_len = sizeof(struct sockaddr_in); |
| 1211 | #endif |
| 1212 | addr_ipv4.sin_family = AF_INET; |
| 1213 | addr_ipv4.sin_port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 1214 | addr_ipv4.sin_addr.s_addr = htonl(INADDR_ANY); |
| 1215 | if (bind(SCTP_BASE_VAR(userspace_udpsctp), (const struct sockaddr *)&addr_ipv4, sizeof(struct sockaddr_in)) < 0) { |
| 1216 | #if defined(__Userspace_os_Windows) |
| 1217 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/UDP/IPv4 (errno = %d).\n", WSAGetLastError()); |
| 1218 | closesocket(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1219 | #else |
| 1220 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/UDP/IPv4 (errno = %d).\n", errno); |
| 1221 | close(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1222 | #endif |
| 1223 | SCTP_BASE_VAR(userspace_udpsctp) = -1; |
| 1224 | } else { |
| 1225 | setReceiveBufferSize(SCTP_BASE_VAR(userspace_udpsctp), SB_RAW); /* 128K */ |
| 1226 | setSendBufferSize(SCTP_BASE_VAR(userspace_udpsctp), SB_RAW); /* 128K Is this setting net.inet.raw.maxdgram value? Should it be set to 64K? */ |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | #endif |
| 1232 | #if defined(INET6) |
| 1233 | if (SCTP_BASE_VAR(userspace_rawsctp6) == -1) { |
| 1234 | if ((SCTP_BASE_VAR(userspace_rawsctp6) = socket(AF_INET6, SOCK_RAW, IPPROTO_SCTP)) == -1) { |
| 1235 | #if defined(__Userspace_os_Windows) |
| 1236 | SCTPDBG(SCTP_DEBUG_USR, "Can't create socket for SCTP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1237 | #else |
| 1238 | SCTPDBG(SCTP_DEBUG_USR, "Can't create socket for SCTP/IPv6 (errno = %d).\n", errno); |
| 1239 | #endif |
| 1240 | } else { |
| 1241 | /* complete setting up the raw SCTP socket */ |
| 1242 | #if defined(IPV6_RECVPKTINFO) |
| 1243 | if (setsockopt(SCTP_BASE_VAR(userspace_rawsctp6), IPPROTO_IPV6, IPV6_RECVPKTINFO, (const void *)&on, sizeof(on)) < 0) { |
| 1244 | #if defined(__Userspace_os_Windows) |
| 1245 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_RECVPKTINFO on socket for SCTP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1246 | closesocket(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1247 | #else |
| 1248 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_RECVPKTINFO on socket for SCTP/IPv6 (errno = %d).\n", errno); |
| 1249 | close(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1250 | #endif |
| 1251 | SCTP_BASE_VAR(userspace_rawsctp6) = -1; |
| 1252 | } else { |
| 1253 | #else |
| 1254 | if (setsockopt(SCTP_BASE_VAR(userspace_rawsctp6), IPPROTO_IPV6, IPV6_PKTINFO,(const void*)&on, sizeof(on)) < 0) { |
| 1255 | #if defined(__Userspace_os_Windows) |
| 1256 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_PKTINFO on socket for SCTP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1257 | closesocket(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1258 | #else |
| 1259 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_PKTINFO on socket for SCTP/IPv6 (errno = %d).\n", errno); |
| 1260 | close(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1261 | #endif |
| 1262 | SCTP_BASE_VAR(userspace_rawsctp6) = -1; |
| 1263 | } else { |
| 1264 | #endif |
| 1265 | if (setsockopt(SCTP_BASE_VAR(userspace_rawsctp6), IPPROTO_IPV6, IPV6_V6ONLY, (const void*)&on, (socklen_t)sizeof(on)) < 0) { |
| 1266 | #if defined(__Userspace_os_Windows) |
| 1267 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_V6ONLY on socket for SCTP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1268 | #else |
| 1269 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_V6ONLY on socket for SCTP/IPv6 (errno = %d).\n", errno); |
| 1270 | #endif |
| 1271 | } |
| 1272 | if (setsockopt(SCTP_BASE_VAR(userspace_rawsctp6), SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(timeout)) < 0) { |
| 1273 | #if defined(__Userspace_os_Windows) |
| 1274 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1275 | closesocket(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1276 | #else |
| 1277 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/IPv6 (errno = %d).\n", errno); |
| 1278 | close(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1279 | #endif |
| 1280 | SCTP_BASE_VAR(userspace_rawsctp6) = -1; |
| 1281 | } else { |
| 1282 | memset((void *)&addr_ipv6, 0, sizeof(struct sockaddr_in6)); |
| 1283 | #ifdef HAVE_SIN6_LEN |
| 1284 | addr_ipv6.sin6_len = sizeof(struct sockaddr_in6); |
| 1285 | #endif |
| 1286 | addr_ipv6.sin6_family = AF_INET6; |
| 1287 | addr_ipv6.sin6_port = htons(0); |
| 1288 | addr_ipv6.sin6_addr = in6addr_any; |
| 1289 | if (bind(SCTP_BASE_VAR(userspace_rawsctp6), (const struct sockaddr *)&addr_ipv6, sizeof(struct sockaddr_in6)) < 0) { |
| 1290 | #if defined(__Userspace_os_Windows) |
| 1291 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1292 | closesocket(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1293 | #else |
| 1294 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/IPv6 (errno = %d).\n", errno); |
| 1295 | close(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1296 | #endif |
| 1297 | SCTP_BASE_VAR(userspace_rawsctp6) = -1; |
| 1298 | } else { |
| 1299 | setReceiveBufferSize(SCTP_BASE_VAR(userspace_rawsctp6), SB_RAW); /* 128K */ |
| 1300 | setSendBufferSize(SCTP_BASE_VAR(userspace_rawsctp6), SB_RAW); /* 128K Is this setting net.inet.raw.maxdgram value? Should it be set to 64K? */ |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | if (SCTP_BASE_VAR(userspace_udpsctp6) == -1) { |
| 1307 | if ((SCTP_BASE_VAR(userspace_udpsctp6) = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1) { |
| 1308 | #if defined(__Userspace_os_Windows) |
| 1309 | SCTPDBG(SCTP_DEBUG_USR, "Can't create socket for SCTP/UDP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1310 | #else |
| 1311 | SCTPDBG(SCTP_DEBUG_USR, "Can't create socket for SCTP/UDP/IPv6 (errno = %d).\n", errno); |
| 1312 | #endif |
| 1313 | } |
| 1314 | #if defined(IPV6_RECVPKTINFO) |
| 1315 | if (setsockopt(SCTP_BASE_VAR(userspace_udpsctp6), IPPROTO_IPV6, IPV6_RECVPKTINFO, (const void *)&on, (int)sizeof(int)) < 0) { |
| 1316 | #if defined(__Userspace_os_Windows) |
| 1317 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_RECVPKTINFO on socket for SCTP/UDP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1318 | closesocket(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1319 | #else |
| 1320 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_RECVPKTINFO on socket for SCTP/UDP/IPv6 (errno = %d).\n", errno); |
| 1321 | close(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1322 | #endif |
| 1323 | SCTP_BASE_VAR(userspace_udpsctp6) = -1; |
| 1324 | } else { |
| 1325 | #else |
| 1326 | if (setsockopt(SCTP_BASE_VAR(userspace_udpsctp6), IPPROTO_IPV6, IPV6_PKTINFO, (const void *)&on, (int)sizeof(int)) < 0) { |
| 1327 | #if defined(__Userspace_os_Windows) |
| 1328 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_PKTINFO on socket for SCTP/UDP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1329 | closesocket(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1330 | #else |
| 1331 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_PKTINFO on socket for SCTP/UDP/IPv6 (errno = %d).\n", errno); |
| 1332 | close(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1333 | #endif |
| 1334 | SCTP_BASE_VAR(userspace_udpsctp6) = -1; |
| 1335 | } else { |
| 1336 | #endif |
| 1337 | if (setsockopt(SCTP_BASE_VAR(userspace_udpsctp6), IPPROTO_IPV6, IPV6_V6ONLY, (const void *)&on, (socklen_t)sizeof(on)) < 0) { |
| 1338 | #if defined(__Userspace_os_Windows) |
| 1339 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_V6ONLY on socket for SCTP/UDP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1340 | #else |
| 1341 | SCTPDBG(SCTP_DEBUG_USR, "Can't set IPV6_V6ONLY on socket for SCTP/UDP/IPv6 (errno = %d).\n", errno); |
| 1342 | #endif |
| 1343 | } |
| 1344 | if (setsockopt(SCTP_BASE_VAR(userspace_udpsctp6), SOL_SOCKET, SO_RCVTIMEO, (const void *)&timeout, sizeof(timeout)) < 0) { |
| 1345 | #if defined(__Userspace_os_Windows) |
| 1346 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/UDP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1347 | closesocket(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1348 | #else |
| 1349 | SCTPDBG(SCTP_DEBUG_USR, "Can't set timeout on socket for SCTP/UDP/IPv6 (errno = %d).\n", errno); |
| 1350 | close(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1351 | #endif |
| 1352 | SCTP_BASE_VAR(userspace_udpsctp6) = -1; |
| 1353 | } else { |
| 1354 | memset((void *)&addr_ipv6, 0, sizeof(struct sockaddr_in6)); |
| 1355 | #ifdef HAVE_SIN6_LEN |
| 1356 | addr_ipv6.sin6_len = sizeof(struct sockaddr_in6); |
| 1357 | #endif |
| 1358 | addr_ipv6.sin6_family = AF_INET6; |
| 1359 | addr_ipv6.sin6_port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)); |
| 1360 | addr_ipv6.sin6_addr = in6addr_any; |
| 1361 | if (bind(SCTP_BASE_VAR(userspace_udpsctp6), (const struct sockaddr *)&addr_ipv6, sizeof(struct sockaddr_in6)) < 0) { |
| 1362 | #if defined(__Userspace_os_Windows) |
| 1363 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/UDP/IPv6 (errno = %d).\n", WSAGetLastError()); |
| 1364 | closesocket(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1365 | #else |
| 1366 | SCTPDBG(SCTP_DEBUG_USR, "Can't bind socket for SCTP/UDP/IPv6 (errno = %d).\n", errno); |
| 1367 | close(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1368 | #endif |
| 1369 | SCTP_BASE_VAR(userspace_udpsctp6) = -1; |
| 1370 | } else { |
| 1371 | setReceiveBufferSize(SCTP_BASE_VAR(userspace_udpsctp6), SB_RAW); /* 128K */ |
| 1372 | setSendBufferSize(SCTP_BASE_VAR(userspace_udpsctp6), SB_RAW); /* 128K Is this setting net.inet.raw.maxdgram value? Should it be set to 64K? */ |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | #endif |
| 1378 | #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) |
| 1379 | #if defined(INET) || defined(INET6) |
| 1380 | if (SCTP_BASE_VAR(userspace_route) != -1) { |
| 1381 | int rc; |
| 1382 |
| 1383 | if ((rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(recvthreadroute), &recv_function_route))) { |
| 1384 | SCTPDBG(SCTP_DEBUG_USR, "Can't start routing thread (%d).\n", rc); |
| 1385 | close(SCTP_BASE_VAR(userspace_route)); |
| 1386 | SCTP_BASE_VAR(userspace_route) = -1; |
| 1387 | } |
| 1388 | } |
| 1389 | #endif |
| 1390 | #endif |
| 1391 | #if defined(INET) |
| 1392 | if (SCTP_BASE_VAR(userspace_rawsctp) != -1) { |
| 1393 | int rc; |
| 1394 |
| 1395 | if ((rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(recvthreadraw), &recv_function_raw))) { |
| 1396 | SCTPDBG(SCTP_DEBUG_USR, "Can't start SCTP/IPv4 recv thread (%d).\n", rc); |
| 1397 | #if defined(__Userspace_os_Windows) |
| 1398 | closesocket(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1399 | #else |
| 1400 | close(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1401 | #endif |
| 1402 | SCTP_BASE_VAR(userspace_rawsctp) = -1; |
| 1403 | } |
| 1404 | } |
| 1405 | if (SCTP_BASE_VAR(userspace_udpsctp) != -1) { |
| 1406 | int rc; |
| 1407 |
| 1408 | if ((rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(recvthreadudp), &recv_function_udp))) { |
| 1409 | SCTPDBG(SCTP_DEBUG_USR, "Can't start SCTP/UDP/IPv4 recv thread (%d).\n", rc); |
| 1410 | #if defined(__Userspace_os_Windows) |
| 1411 | closesocket(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1412 | #else |
| 1413 | close(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1414 | #endif |
| 1415 | SCTP_BASE_VAR(userspace_udpsctp) = -1; |
| 1416 | } |
| 1417 | } |
| 1418 | #endif |
| 1419 | #if defined(INET6) |
| 1420 | if (SCTP_BASE_VAR(userspace_rawsctp6) != -1) { |
| 1421 | int rc; |
| 1422 |
| 1423 | if ((rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(recvthreadraw6), &recv_function_raw6))) { |
| 1424 | SCTPDBG(SCTP_DEBUG_USR, "Can't start SCTP/IPv6 recv thread (%d).\n", rc); |
| 1425 | #if defined(__Userspace_os_Windows) |
| 1426 | closesocket(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1427 | #else |
| 1428 | close(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1429 | #endif |
| 1430 | SCTP_BASE_VAR(userspace_rawsctp6) = -1; |
| 1431 | } |
| 1432 | } |
| 1433 | if (SCTP_BASE_VAR(userspace_udpsctp6) != -1) { |
| 1434 | int rc; |
| 1435 |
| 1436 | if ((rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(recvthreadudp6), &recv_function_udp6))) { |
| 1437 | SCTPDBG(SCTP_DEBUG_USR, "Can't start SCTP/UDP/IPv6 recv thread (%d).\n", rc); |
| 1438 | #if defined(__Userspace_os_Windows) |
| 1439 | closesocket(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1440 | #else |
| 1441 | close(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1442 | #endif |
| 1443 | SCTP_BASE_VAR(userspace_udpsctp6) = -1; |
| 1444 | } |
| 1445 | } |
| 1446 | #endif |
| 1447 | } |
| 1448 |
| 1449 | void |
| 1450 | recv_thread_destroy(void) |
| 1451 | { |
| 1452 | #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) |
| 1453 | #if defined(INET) || defined(INET6) |
| 1454 | if (SCTP_BASE_VAR(userspace_route) != -1) { |
| 1455 | close(SCTP_BASE_VAR(userspace_route)); |
| 1456 | } |
| 1457 | #endif |
| 1458 | #endif |
| 1459 | #if defined(INET) |
| 1460 | if (SCTP_BASE_VAR(userspace_rawsctp) != -1) { |
| 1461 | #if defined(__Userspace_os_Windows) |
| 1462 | closesocket(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1463 | #else |
| 1464 | close(SCTP_BASE_VAR(userspace_rawsctp)); |
| 1465 | #endif |
| 1466 | } |
| 1467 | if (SCTP_BASE_VAR(userspace_udpsctp) != -1) { |
| 1468 | #if defined(__Userspace_os_Windows) |
| 1469 | closesocket(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1470 | #else |
| 1471 | close(SCTP_BASE_VAR(userspace_udpsctp)); |
| 1472 | #endif |
| 1473 | } |
| 1474 | #endif |
| 1475 | #if defined(INET6) |
| 1476 | if (SCTP_BASE_VAR(userspace_rawsctp6) != -1) { |
| 1477 | #if defined(__Userspace_os_Windows) |
| 1478 | closesocket(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1479 | #else |
| 1480 | close(SCTP_BASE_VAR(userspace_rawsctp6)); |
| 1481 | #endif |
| 1482 | } |
| 1483 | if (SCTP_BASE_VAR(userspace_udpsctp6) != -1) { |
| 1484 | #if defined(__Userspace_os_Windows) |
| 1485 | closesocket(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1486 | #else |
| 1487 | close(SCTP_BASE_VAR(userspace_udpsctp6)); |
| 1488 | #endif |
| 1489 | } |
| 1490 | #endif |
| 1491 | } |
| 1492 | #else |
| 1493 | int foo; |
| 1494 | #endif |
| 1495 |