Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lwip support. #583

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ option(sctp_sanitizer_memory "Compile with memory sanitizer" 0)

option(sctp_build_fuzzer "Compile in clang fuzzing mode" 0)

option(sctp_use_lwip "build with lwip" ON)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't include LWIP by default.

Copy link
Contributor Author

@ycyang1229 ycyang1229 May 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will disable it when it is merged into it officially. Right now, it is one very rough version to show the idea.


if(sctp_use_lwip)
add_definitions(-DSCTP_USE_LWIP)
endif()

if (sctp_sanitizer_address AND sctp_sanitizer_memory)
message(FATAL_ERROR "Can not compile with both sanitizer options")
endif ()
Expand Down Expand Up @@ -122,7 +128,9 @@ set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/usrsctplib")

check_include_file(usrsctp.h have_usrsctp_h)
if (NOT have_usrsctp_h)
message(FATAL_ERROR "usrsctp.h not found")
if(NOT sctp_use_lwip)
message(FATAL_ERROR "usrsctp.h not found")
endif()
endif ()

check_struct_has_member("struct sockaddr" "sa_len" "sys/types.h;sys/socket.h" have_sa_len)
Expand All @@ -144,7 +152,7 @@ if (have_sin6_len)
endif ()

check_struct_has_member("struct sockaddr_conn" "sconn_len" "usrsctp.h" have_sconn_len)
if (have_sconn_len)
if (have_sconn_len OR sctp_use_lwip)
message(STATUS "HAVE_SCONN_LEN")
add_definitions(-DHAVE_SCONN_LEN)
endif ()
Expand Down
12 changes: 6 additions & 6 deletions fuzzer/pcap2corpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <sys/types.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/sctp_ip_port.h>
#include <netinet/ip6.h>
#include <pcap/pcap.h>
#include <stdio.h>
Expand Down Expand Up @@ -110,7 +110,7 @@ packet_handler(u_char *user, const struct pcap_pkthdr *pkthdr, const u_char *byt
const u_char *bytes_out;
FILE *file;
char *filename;
const struct ip *ip4_hdr_in;
const STRUCT_IP_HDR *ip4_hdr_in;
const struct ip6_hdr *ip6_hdr_in;
size_t offset, length;
int null = 0;
Expand All @@ -124,15 +124,15 @@ packet_handler(u_char *user, const struct pcap_pkthdr *pkthdr, const u_char *byt
goto out;
}
if (args->is_ipv4(bytes_in)) {
offset = args->offset + sizeof(struct ip) + sizeof(struct sctphdr);
offset = args->offset + sizeof(STRUCT_IP_HDR) + sizeof(struct sctphdr);
if (pkthdr->caplen < offset) {
goto out;
}
ip4_hdr_in = (const struct ip *)(const void *)(bytes_in + args->offset);
if (ip4_hdr_in->ip_p == IPPROTO_SCTP) {
ip4_hdr_in = (const STRUCT_IP_HDR *)(const void *)(bytes_in + args->offset);
if (GET_IP_PROTO(ip4_hdr_in) == IPPROTO_SCTP) {
unsigned int ip4_hdr_len;

ip4_hdr_len = ip4_hdr_in->ip_hl << 2;
ip4_hdr_len = GET_IP_HDR_LEN_VAL(ip4_hdr_in) << 2;
offset = args->offset + ip4_hdr_len + sizeof(struct sctphdr);
if (pkthdr->caplen < offset) {
goto out;
Expand Down
6 changes: 6 additions & 0 deletions usrsctplib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ include(CheckCCompilerFlag)
add_definitions(-D__Userspace__)
add_definitions(-DSCTP_SIMPLE_ALLOCATOR)
add_definitions(-DSCTP_PROCESS_LEVEL_LOCKS)
if(NOT sctp_use_lwip)
add_definitions(-D__native_client__)
endif()


#################################################
Expand Down Expand Up @@ -119,6 +122,7 @@ list(APPEND usrsctp_netinet_headers
netinet/sctp_header.h
netinet/sctp_indata.h
netinet/sctp_input.h
netinet/sctp_ip_port.h
netinet/sctp_lock_userspace.h
netinet/sctp_os_userspace.h
netinet/sctp_os.h
Expand All @@ -130,6 +134,8 @@ list(APPEND usrsctp_netinet_headers
netinet/sctp_structs.h
netinet/sctp_sysctl.h
netinet/sctp_timer.h
netinet/sctp_udp_port.h
netinet/sctp_in_port.h
netinet/sctp_uio.h
netinet/sctp_var.h
netinet/sctputil.h
Expand Down
78 changes: 78 additions & 0 deletions usrsctplib/netinet/sctp_bsd_addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ __FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 366426 2020-10-04 15:37:34Z
#if defined(__FreeBSD__) && !defined(__Userspace__)
#include <sys/unistd.h>
#endif
#if defined(SCTP_USE_LWIP)
#include <lwip/netif.h>
#endif

/* Declare all of our malloc named types */
MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
Expand Down Expand Up @@ -415,6 +418,80 @@ sctp_init_ifns_for_vrf(int vrfid)
static void
sctp_init_ifns_for_vrf(int vrfid)
{
#if defined(SCTP_USE_LWIP)
#define LWIP_IF_NUM_MAX 256

struct sctp_ifa *sctp_ifa;
uint32_t ifa_flags;
uint32_t if_num;
char if_name[NETIF_NAMESIZE];
struct sockaddr* in_addr = malloc(sizeof(struct sockaddr));

for(if_num=1;if_num<LWIP_IF_NUM_MAX; if_num++){
struct netif* tmp_if = netif_get_by_index(if_num);
char * tmp_name = netif_index_to_name(if_num, if_name);
memset(in_addr, 0, sizeof(struct sockaddr));
if(tmp_if != NULL){

if(ip_addr_isloopback(&tmp_if->ip_addr)){
continue;
}

if(tmp_if->ip_addr.type == IPADDR_TYPE_V4){
in_addr->sa_family = AF_INET;
memcpy(&((struct sockaddr_in *)in_addr)->sin_addr, &tmp_if->ip_addr.u_addr, sizeof(uint32_t) );
}else{
in_addr->sa_family = AF_INET6;
memcpy(&((struct sockaddr_in6 *)in_addr)->sin6_addr, &tmp_if->ip_addr.u_addr, sizeof(uint32_t)*4);
}
#if !defined(INET)
if (in_addr->sa_family != AF_INET6) {
/* non inet6 skip */
continue;
}
#elif !defined(INET6)
if (in_addr->sa_family != AF_INET) {
/* non inet skip */
continue;
}
#else
if ((in_addr->sa_family != AF_INET) && (in_addr->sa_family != AF_INET6)) {
/* non inet/inet6 skip */
continue;
}
#endif
#if defined(INET6)
if ((in_addr->sa_family == AF_INET6) &&
IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)in_addr)->sin6_addr)) {
/* skip unspecifed addresses */
continue;
}
#endif
#if defined(INET)
if (in_addr->sa_family == AF_INET &&
((struct sockaddr_in *)in_addr)->sin_addr.s_addr == 0) {
continue;
}
#endif
ifa_flags = 0;
sctp_ifa = sctp_add_addr_to_vrf(vrfid,
NULL,
if_num,
0,
tmp_name,
NULL,
(struct sockaddr*)in_addr,
ifa_flags,
0);
if (sctp_ifa) {
sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
}
}else{
break;
}
}
free(in_addr);
#else
#if defined(INET) || defined(INET6)
int rc;
struct ifaddrs *ifa, *ifas;
Expand Down Expand Up @@ -474,6 +551,7 @@ sctp_init_ifns_for_vrf(int vrfid)
}
freeifaddrs(ifas);
#endif
#endif
}
#endif
#if defined(__APPLE__) && !defined(__Userspace__)
Expand Down
6 changes: 6 additions & 0 deletions usrsctplib/netinet/sctp_callout.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#if !defined(SCTP_USE_LWIP)
#include <errno.h>
#else
#include "lwip/errno.h"
#endif
#include <user_atomic.h>
#include <netinet/sctp_sysctl.h>
#include <netinet/sctp_pcb.h>
Expand Down Expand Up @@ -199,6 +203,8 @@ user_sctp_timer_iterate(void *arg)
for (;;) {
#if defined(_WIN32)
Sleep(TIMEOUT_INTERVAL);
#elif defined(SCTP_USE_LWIP)
usleep(TIMEOUT_INTERVAL*1000);
#else
struct timespec amount, remaining;

Expand Down
26 changes: 14 additions & 12 deletions usrsctplib/netinet/sctp_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,27 +563,29 @@ struct sctp_auth_chunk {

#else
#define SCTP_MAX_OVERHEAD (sizeof(struct sctp_data_chunk) + \
sizeof(struct sctphdr) + \
sizeof(struct sctp_ecne_chunk) + \
sizeof(struct sctp_sack_chunk) + \
sizeof(struct ip))

sizeof(struct sctphdr) + \
sizeof(struct sctp_ecne_chunk) + \
sizeof(struct sctp_sack_chunk) + \
sizeof(STRUCT_IP_HDR))

#define SCTP_MED_OVERHEAD (sizeof(struct sctp_data_chunk) + \
sizeof(struct sctphdr) + \
sizeof(struct ip))
sizeof(struct sctphdr) + \
sizeof(STRUCT_IP_HDR))

#define SCTP_MIN_OVERHEAD (sizeof(struct ip) + \
sizeof(struct sctphdr))

#define SCTP_MIN_OVERHEAD (sizeof(STRUCT_IP_HDR) + \
sizeof(struct sctphdr))

#endif /* INET6 */
#endif /* !SCTP_MAX_OVERHEAD */

#define SCTP_MED_V4_OVERHEAD (sizeof(struct sctp_data_chunk) + \
sizeof(struct sctphdr) + \
sizeof(struct ip))
sizeof(struct sctphdr) + \
sizeof(STRUCT_IP_HDR))

#define SCTP_MIN_V4_OVERHEAD (sizeof(struct ip) + \
sizeof(struct sctphdr))
#define SCTP_MIN_V4_OVERHEAD (sizeof(STRUCT_IP_HDR) + \
sizeof(struct sctphdr))

#if defined(_WIN32) && !defined(__Userspace__)
#include <packoff.h>
Expand Down
86 changes: 86 additions & 0 deletions usrsctplib/netinet/sctp_in_port.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
* Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* a) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* b) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* c) Neither the name of Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#if defined(__FreeBSD__) && !defined(__Userspace__)
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: head/sys/netinet/sctp_timer.h 359195 2020-03-21 16:12:19Z tuexen $");
#endif

#ifndef _NETINET_SCTP_IN_PORT_H_
#define _NETINET_SCTP_IN_PORT_H_

#if defined(SCTP_USE_LWIP)
/* Standard well-known ports. */
enum
{
IPPORT_ECHO = 7, /* Echo service. */
IPPORT_DISCARD = 9, /* Discard transmissions service. */
IPPORT_SYSTAT = 11, /* System status service. */
IPPORT_DAYTIME = 13, /* Time of day service. */
IPPORT_NETSTAT = 15, /* Network status service. */
IPPORT_FTP = 21, /* File Transfer Protocol. */
IPPORT_TELNET = 23, /* Telnet protocol. */
IPPORT_SMTP = 25, /* Simple Mail Transfer Protocol. */
IPPORT_TIMESERVER = 37, /* Timeserver service. */
IPPORT_NAMESERVER = 42, /* Domain Name Service. */
IPPORT_WHOIS = 43, /* Internet Whois service. */
IPPORT_MTP = 57,

IPPORT_TFTP = 69, /* Trivial File Transfer Protocol. */
IPPORT_RJE = 77,
IPPORT_FINGER = 79, /* Finger service. */
IPPORT_TTYLINK = 87,
IPPORT_SUPDUP = 95, /* SUPDUP protocol. */


IPPORT_EXECSERVER = 512, /* execd service. */
IPPORT_LOGINSERVER = 513, /* rlogind service. */
IPPORT_CMDSERVER = 514,
IPPORT_EFSSERVER = 520,

/* UDP ports. */
IPPORT_BIFFUDP = 512,
IPPORT_WHOSERVER = 513,
IPPORT_ROUTESERVER = 520,

/* Ports less than this value are reserved for privileged processes. */
IPPORT_RESERVED = 1024,

/* Ports greater this value are reserved for (non-privileged) servers. */
IPPORT_USERRESERVED = 5000
};

#define IP_HDRINCL 3 /* int; Header is included with data. */
#endif
#endif//!< _NETINET_SCTP_IN_PORT_H_
Loading