2019-06-01 09:52:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of Open5GS.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-04-27 14:54:30 +00:00
|
|
|
#include "ogs-sctp.h"
|
2019-06-15 07:54:49 +00:00
|
|
|
|
2019-06-11 13:10:47 +00:00
|
|
|
#include "mme-event.h"
|
2019-06-01 09:52:38 +00:00
|
|
|
#include "s1ap-path.h"
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2019-10-31 05:17:43 +00:00
|
|
|
#if HAVE_USRSCTP
|
|
|
|
static void usrsctp_recv_handler(struct socket *socket, void *data, int flags);
|
|
|
|
#else
|
|
|
|
static void lksctp_accept_handler(short when, ogs_socket_t fd, void *data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void s1ap_accept_handler(ogs_sock_t *sock);
|
|
|
|
void s1ap_recv_handler(ogs_sock_t *sock);
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2019-06-01 09:43:33 +00:00
|
|
|
ogs_sock_t *s1ap_server(ogs_socknode_t *node)
|
2019-04-27 14:54:30 +00:00
|
|
|
{
|
|
|
|
char buf[OGS_ADDRSTRLEN];
|
2019-05-31 14:22:22 +00:00
|
|
|
ogs_sock_t *sock = NULL;
|
2022-03-15 13:03:50 +00:00
|
|
|
#if !HAVE_USRSCTP
|
|
|
|
ogs_poll_t *poll = NULL;
|
|
|
|
#endif
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2019-05-31 14:22:22 +00:00
|
|
|
ogs_assert(node);
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2019-10-31 05:17:43 +00:00
|
|
|
#if HAVE_USRSCTP
|
2022-03-15 13:03:50 +00:00
|
|
|
sock = ogs_sctp_server(SOCK_SEQPACKET, node->addr, node->option);
|
2021-04-21 08:24:17 +00:00
|
|
|
if (!sock) return NULL;
|
2019-10-31 09:13:49 +00:00
|
|
|
usrsctp_set_non_blocking((struct socket *)sock, 1);
|
2019-10-31 05:17:43 +00:00
|
|
|
usrsctp_set_upcall((struct socket *)sock, usrsctp_recv_handler, NULL);
|
|
|
|
#else
|
2022-03-15 13:03:50 +00:00
|
|
|
sock = ogs_sctp_server(SOCK_STREAM, node->addr, node->option);
|
2021-04-21 08:24:17 +00:00
|
|
|
if (!sock) return NULL;
|
2022-03-15 13:03:50 +00:00
|
|
|
poll = ogs_pollset_add(ogs_app()->pollset,
|
2019-10-31 05:17:43 +00:00
|
|
|
OGS_POLLIN, sock->fd, lksctp_accept_handler, sock);
|
2022-03-15 13:03:50 +00:00
|
|
|
ogs_assert(poll);
|
|
|
|
|
|
|
|
node->poll = poll;
|
2019-10-31 05:17:43 +00:00
|
|
|
#endif
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2022-03-15 13:03:50 +00:00
|
|
|
node->sock = sock;
|
|
|
|
node->cleanup = ogs_sctp_destroy;
|
|
|
|
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_info("s1ap_server() [%s]:%d",
|
2019-05-31 14:22:22 +00:00
|
|
|
OGS_ADDR(node->addr, buf), OGS_PORT(node->addr));
|
2019-06-01 09:43:33 +00:00
|
|
|
|
|
|
|
return sock;
|
2019-04-27 14:54:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 05:17:43 +00:00
|
|
|
void s1ap_recv_upcall(short when, ogs_socket_t fd, void *data)
|
2019-04-27 14:54:30 +00:00
|
|
|
{
|
2019-05-31 14:22:22 +00:00
|
|
|
ogs_sock_t *sock = NULL;
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2019-10-31 05:17:43 +00:00
|
|
|
ogs_assert(fd != INVALID_SOCKET);
|
|
|
|
sock = data;
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_assert(sock);
|
2019-10-31 05:17:43 +00:00
|
|
|
|
|
|
|
s1ap_recv_handler(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_USRSCTP
|
|
|
|
static void usrsctp_recv_handler(struct socket *socket, void *data, int flags)
|
|
|
|
{
|
|
|
|
int events;
|
|
|
|
|
|
|
|
while ((events = usrsctp_get_events(socket)) &&
|
|
|
|
(events & SCTP_EVENT_READ)) {
|
|
|
|
s1ap_recv_handler((ogs_sock_t *)socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void lksctp_accept_handler(short when, ogs_socket_t fd, void *data)
|
|
|
|
{
|
|
|
|
ogs_assert(data);
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_assert(fd != INVALID_SOCKET);
|
|
|
|
|
2019-10-31 05:17:43 +00:00
|
|
|
s1ap_accept_handler(data);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void s1ap_accept_handler(ogs_sock_t *sock)
|
|
|
|
{
|
|
|
|
char buf[OGS_ADDRSTRLEN];
|
|
|
|
ogs_sock_t *new = NULL;
|
|
|
|
|
|
|
|
ogs_assert(sock);
|
|
|
|
|
2019-04-27 14:54:30 +00:00
|
|
|
new = ogs_sock_accept(sock);
|
2019-06-01 09:43:33 +00:00
|
|
|
if (new) {
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_sockaddr_t *addr = NULL;
|
|
|
|
|
|
|
|
addr = ogs_calloc(1, sizeof(ogs_sockaddr_t));
|
|
|
|
ogs_assert(addr);
|
|
|
|
memcpy(addr, &new->remote_addr, sizeof(ogs_sockaddr_t));
|
|
|
|
|
|
|
|
ogs_info("eNB-S1 accepted[%s]:%d in s1_path module",
|
|
|
|
OGS_ADDR(addr, buf), OGS_PORT(addr));
|
|
|
|
|
2020-09-23 01:20:13 +00:00
|
|
|
s1ap_event_push(MME_EVT_S1AP_LO_ACCEPT, new, addr, NULL, 0, 0);
|
2019-06-01 09:43:33 +00:00
|
|
|
} else {
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno, "accept() failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:17:43 +00:00
|
|
|
void s1ap_recv_handler(ogs_sock_t *sock)
|
2019-04-27 14:54:30 +00:00
|
|
|
{
|
|
|
|
ogs_pkbuf_t *pkbuf;
|
|
|
|
int size;
|
|
|
|
ogs_sockaddr_t *addr = NULL;
|
2019-10-31 05:17:43 +00:00
|
|
|
ogs_sockaddr_t from;
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_sctp_info_t sinfo;
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
ogs_assert(sock);
|
|
|
|
|
2019-09-13 12:07:47 +00:00
|
|
|
pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN);
|
2020-09-07 03:53:38 +00:00
|
|
|
ogs_assert(pkbuf);
|
2019-09-13 12:07:47 +00:00
|
|
|
ogs_pkbuf_put(pkbuf, OGS_MAX_SDU_LEN);
|
2019-04-27 14:54:30 +00:00
|
|
|
size = ogs_sctp_recvmsg(
|
2019-10-31 05:17:43 +00:00
|
|
|
sock, pkbuf->data, pkbuf->len, &from, &sinfo, &flags);
|
2021-05-08 06:09:10 +00:00
|
|
|
if (size < 0 || size >= OGS_MAX_SDU_LEN) {
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_error("ogs_sctp_recvmsg(%d) failed(%d:%s)",
|
|
|
|
size, errno, strerror(errno));
|
2019-10-31 05:17:43 +00:00
|
|
|
ogs_pkbuf_free(pkbuf);
|
2019-04-27 14:54:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-01 09:43:33 +00:00
|
|
|
if (flags & MSG_NOTIFICATION) {
|
2019-04-27 14:54:30 +00:00
|
|
|
union sctp_notification *not =
|
|
|
|
(union sctp_notification *)pkbuf->data;
|
|
|
|
|
2019-06-01 09:43:33 +00:00
|
|
|
switch(not->sn_header.sn_type) {
|
|
|
|
case SCTP_ASSOC_CHANGE :
|
|
|
|
ogs_debug("SCTP_ASSOC_CHANGE:"
|
|
|
|
"[T:%d, F:0x%x, S:%d, I/O:%d/%d]",
|
|
|
|
not->sn_assoc_change.sac_type,
|
|
|
|
not->sn_assoc_change.sac_flags,
|
|
|
|
not->sn_assoc_change.sac_state,
|
|
|
|
not->sn_assoc_change.sac_inbound_streams,
|
|
|
|
not->sn_assoc_change.sac_outbound_streams);
|
|
|
|
|
|
|
|
if (not->sn_assoc_change.sac_state == SCTP_COMM_UP) {
|
|
|
|
ogs_debug("SCTP_COMM_UP");
|
|
|
|
|
|
|
|
addr = ogs_calloc(1, sizeof(ogs_sockaddr_t));
|
|
|
|
ogs_assert(addr);
|
2019-10-31 05:17:43 +00:00
|
|
|
memcpy(addr, &from, sizeof(ogs_sockaddr_t));
|
2019-06-01 09:43:33 +00:00
|
|
|
|
2019-06-23 04:22:22 +00:00
|
|
|
s1ap_event_push(MME_EVT_S1AP_LO_SCTP_COMM_UP,
|
|
|
|
sock, addr, NULL,
|
|
|
|
not->sn_assoc_change.sac_inbound_streams,
|
|
|
|
not->sn_assoc_change.sac_outbound_streams);
|
2019-06-01 09:43:33 +00:00
|
|
|
} else if (not->sn_assoc_change.sac_state == SCTP_SHUTDOWN_COMP ||
|
|
|
|
not->sn_assoc_change.sac_state == SCTP_COMM_LOST) {
|
|
|
|
|
|
|
|
if (not->sn_assoc_change.sac_state == SCTP_SHUTDOWN_COMP)
|
|
|
|
ogs_debug("SCTP_SHUTDOWN_COMP");
|
|
|
|
if (not->sn_assoc_change.sac_state == SCTP_COMM_LOST)
|
|
|
|
ogs_debug("SCTP_COMM_LOST");
|
2019-04-27 14:54:30 +00:00
|
|
|
|
|
|
|
addr = ogs_calloc(1, sizeof(ogs_sockaddr_t));
|
|
|
|
ogs_assert(addr);
|
2019-10-31 05:17:43 +00:00
|
|
|
memcpy(addr, &from, sizeof(ogs_sockaddr_t));
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2019-06-23 04:22:22 +00:00
|
|
|
s1ap_event_push(MME_EVT_S1AP_LO_CONNREFUSED,
|
|
|
|
sock, addr, NULL, 0, 0);
|
2019-04-27 14:54:30 +00:00
|
|
|
}
|
2019-06-01 09:43:33 +00:00
|
|
|
break;
|
2021-01-05 04:24:22 +00:00
|
|
|
|
2019-06-01 09:43:33 +00:00
|
|
|
case SCTP_SHUTDOWN_EVENT :
|
2021-01-05 04:24:22 +00:00
|
|
|
ogs_debug("SCTP_SHUTDOWN_EVENT:[T:%d, F:0x%x, L:%d]",
|
|
|
|
not->sn_shutdown_event.sse_type,
|
|
|
|
not->sn_shutdown_event.sse_flags,
|
|
|
|
not->sn_shutdown_event.sse_length);
|
2019-06-01 09:43:33 +00:00
|
|
|
|
|
|
|
addr = ogs_calloc(1, sizeof(ogs_sockaddr_t));
|
|
|
|
ogs_assert(addr);
|
2019-10-31 05:17:43 +00:00
|
|
|
memcpy(addr, &from, sizeof(ogs_sockaddr_t));
|
2019-06-01 09:43:33 +00:00
|
|
|
|
2019-06-23 04:22:22 +00:00
|
|
|
s1ap_event_push(MME_EVT_S1AP_LO_CONNREFUSED,
|
|
|
|
sock, addr, NULL, 0, 0);
|
2019-06-01 09:43:33 +00:00
|
|
|
break;
|
2021-01-05 04:24:22 +00:00
|
|
|
|
|
|
|
case SCTP_SEND_FAILED :
|
|
|
|
#if HAVE_USRSCTP
|
|
|
|
ogs_error("SCTP_SEND_FAILED:[T:%d, F:0x%x, S:%d]",
|
|
|
|
not->sn_send_failed_event.ssfe_type,
|
|
|
|
not->sn_send_failed_event.ssfe_flags,
|
|
|
|
not->sn_send_failed_event.ssfe_error);
|
|
|
|
#else
|
|
|
|
ogs_error("SCTP_SEND_FAILED:[T:%d, F:0x%x, S:%d]",
|
|
|
|
not->sn_send_failed.ssf_type,
|
|
|
|
not->sn_send_failed.ssf_flags,
|
|
|
|
not->sn_send_failed.ssf_error);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
2019-06-01 09:43:33 +00:00
|
|
|
case SCTP_PEER_ADDR_CHANGE:
|
|
|
|
ogs_warn("SCTP_PEER_ADDR_CHANGE:[T:%d, F:0x%x, S:%d]",
|
|
|
|
not->sn_paddr_change.spc_type,
|
|
|
|
not->sn_paddr_change.spc_flags,
|
|
|
|
not->sn_paddr_change.spc_error);
|
|
|
|
break;
|
|
|
|
case SCTP_REMOTE_ERROR:
|
|
|
|
ogs_warn("SCTP_REMOTE_ERROR:[T:%d, F:0x%x, S:%d]",
|
|
|
|
not->sn_remote_error.sre_type,
|
|
|
|
not->sn_remote_error.sre_flags,
|
|
|
|
not->sn_remote_error.sre_error);
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
ogs_error("Discarding event with unknown flags:0x%x type:0x%x",
|
|
|
|
flags, not->sn_header.sn_type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (flags & MSG_EOR) {
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_pkbuf_trim(pkbuf, size);
|
|
|
|
|
|
|
|
addr = ogs_calloc(1, sizeof(ogs_sockaddr_t));
|
|
|
|
ogs_assert(addr);
|
2019-10-31 05:17:43 +00:00
|
|
|
memcpy(addr, &from, sizeof(ogs_sockaddr_t));
|
2019-04-27 14:54:30 +00:00
|
|
|
|
2019-06-23 04:22:22 +00:00
|
|
|
s1ap_event_push(MME_EVT_S1AP_MESSAGE, sock, addr, pkbuf, 0, 0);
|
2019-04-27 14:54:30 +00:00
|
|
|
return;
|
2019-06-01 09:43:33 +00:00
|
|
|
} else {
|
2021-05-08 06:09:10 +00:00
|
|
|
ogs_fatal("Invalid flag(0x%x)", flags);
|
2019-04-27 14:54:30 +00:00
|
|
|
ogs_assert_if_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
ogs_pkbuf_free(pkbuf);
|
|
|
|
}
|