File size: 8,246 Bytes
258fb37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
/*
* Copyright (C) 2019-2023 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/>.
*/
#include "gtp-path.h"
static void _gtpv2_c_recv_cb(short when, ogs_socket_t fd, void *data)
{
sgwc_event_t *e = NULL;
int rv;
ssize_t size;
ogs_pkbuf_t *pkbuf = NULL;
ogs_sockaddr_t from;
ogs_gtp_node_t *gnode = NULL;
char frombuf[OGS_ADDRSTRLEN];
ogs_assert(fd != INVALID_SOCKET);
pkbuf = ogs_pkbuf_alloc(NULL, OGS_MAX_SDU_LEN);
ogs_assert(pkbuf);
ogs_pkbuf_put(pkbuf, OGS_MAX_SDU_LEN);
size = ogs_recvfrom(fd, pkbuf->data, pkbuf->len, 0, &from);
if (size <= 0) {
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno,
"ogs_recvfrom() failed");
ogs_pkbuf_free(pkbuf);
return;
}
ogs_pkbuf_trim(pkbuf, size);
/*
* 5.5.2 in spec 29.274
*
* If a peer's TEID is not available, the TEID field still shall be
* present in the header and its value shall be set to "0" in the
* following messages:
*
* - Create Session Request message on S2a/S2b/S5/S8
*
* - Create Session Request message on S4/S11, if for a given UE,
* the SGSN/MME has not yet obtained the Control TEID of the SGW.
*
* - If a node receives a message and the TEID-C in the GTPv2 header of
* the received message is not known, it shall respond with
* "Context not found" Cause in the corresponding response message
* to the sender, the TEID used in the GTPv2-C header in the response
* message shall be then set to zero.
*
* - If a node receives a request message containing protocol error,
* e.g. Mandatory IE missing, which requires the receiver to reject
* the message as specified in clause 7.7, it shall reject
* the request message. For the response message, the node should
* look up the remote peer's TEID and accordingly set the GTPv2-C
* header TEID and the message cause code. As an implementation
* option, the node may not look up the remote peer's TEID and
* set the GTPv2-C header TEID to zero in the response message.
* However in this case, the cause code shall not be set to
* "Context not found".
*/
gnode = ogs_gtp_node_find_by_addr(&sgwc_self()->pgw_s5c_list, &from);
if (gnode) {
e = sgwc_event_new(SGWC_EVT_S5C_MESSAGE);
ogs_assert(e);
e->gnode = gnode;
} else {
gnode = ogs_gtp_node_find_by_addr(&sgwc_self()->mme_s11_list, &from);
if (!gnode) {
gnode = ogs_gtp_node_add_by_addr(&sgwc_self()->mme_s11_list, &from);
if (!gnode) {
ogs_error("Failed to create new gnode(%s:%u), mempool full, ignoring msg!",
OGS_ADDR(&from, frombuf), OGS_PORT(&from));
ogs_pkbuf_free(pkbuf);
return;
}
gnode->sock = data;
}
e = sgwc_event_new(SGWC_EVT_S11_MESSAGE);
ogs_assert(e);
e->gnode = gnode;
}
e->pkbuf = pkbuf;
rv = ogs_queue_push(ogs_app()->queue, e);
if (rv != OGS_OK) {
ogs_error("ogs_queue_push() failed:%d", (int)rv);
ogs_pkbuf_free(e->pkbuf);
sgwc_event_free(e);
}
}
int sgwc_gtp_open(void)
{
ogs_socknode_t *node = NULL;
ogs_sock_t *sock = NULL;
ogs_list_for_each(&ogs_gtp_self()->gtpc_list, node) {
sock = ogs_gtp_server(node);
if (!sock) return OGS_ERROR;
node->poll = ogs_pollset_add(ogs_app()->pollset,
OGS_POLLIN, sock->fd, _gtpv2_c_recv_cb, sock);
ogs_assert(node->poll);
}
ogs_list_for_each(&ogs_gtp_self()->gtpc_list6, node) {
sock = ogs_gtp_server(node);
if (!sock) return OGS_ERROR;
node->poll = ogs_pollset_add(ogs_app()->pollset,
OGS_POLLIN, sock->fd, _gtpv2_c_recv_cb, sock);
ogs_assert(node->poll);
}
OGS_SETUP_GTPC_SERVER;
ogs_assert(ogs_gtp_self()->gtpc_sock || ogs_gtp_self()->gtpc_sock6);
ogs_assert(ogs_gtp_self()->gtpc_addr || ogs_gtp_self()->gtpc_addr6);
return OGS_OK;
}
void sgwc_gtp_close(void)
{
ogs_socknode_remove_all(&ogs_gtp_self()->gtpc_list);
ogs_socknode_remove_all(&ogs_gtp_self()->gtpc_list6);
}
static void bearer_timeout(ogs_gtp_xact_t *xact, void *data)
{
sgwc_bearer_t *bearer = data;
ogs_pool_id_t bearer_id = OGS_INVALID_POOL_ID;
sgwc_sess_t *sess = NULL;
sgwc_ue_t *sgwc_ue = NULL;
uint8_t type = 0;
ogs_assert(xact);
type = xact->seq[0].type;
ogs_assert(data);
bearer_id = OGS_POINTER_TO_UINT(data);
ogs_assert(bearer_id >= OGS_MIN_POOL_ID && bearer_id <= OGS_MAX_POOL_ID);
bearer = sgwc_bearer_find_by_id(bearer_id);
if (!bearer) {
ogs_error("Bearer[%d] has already been removed [%d]", bearer_id, type);
return;
}
sess = sgwc_sess_find_by_id(bearer->sess_id);
ogs_assert(sess);
sgwc_ue = sgwc_ue_find_by_id(sess->sgwc_ue_id);
ogs_assert(sgwc_ue);
switch (type) {
case OGS_GTP2_DOWNLINK_DATA_NOTIFICATION_TYPE:
ogs_warn("[%s] No Downlink Data Notification ACK", sgwc_ue->imsi_bcd);
break;
default:
ogs_error("GTP Timeout : IMSI[%s] Message-Type[%d]",
sgwc_ue->imsi_bcd, type);
}
}
int sgwc_gtp_send_create_session_response(
sgwc_sess_t *sess, ogs_gtp_xact_t *xact)
{
int rv;
sgwc_ue_t *sgwc_ue = NULL;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(sess);
sgwc_ue = sgwc_ue_find_by_id(sess->sgwc_ue_id);
ogs_assert(sgwc_ue);
ogs_assert(xact);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_CREATE_SESSION_RESPONSE_TYPE;
h.teid = sgwc_ue->mme_s11_teid;
pkbuf = sgwc_s11_build_create_session_response(h.type, sess);
if (!pkbuf) {
ogs_error("sgwc_s11_build_create_session_response() failed");
return OGS_ERROR;
}
rv = ogs_gtp_xact_update_tx(xact, &h, pkbuf);
if (rv != OGS_OK) {
ogs_error("ogs_gtp_xact_update_tx() failed");
return OGS_ERROR;
}
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int sgwc_gtp_send_downlink_data_notification(
uint8_t cause_value, sgwc_bearer_t *bearer)
{
int rv;
sgwc_ue_t *sgwc_ue = NULL;
sgwc_sess_t *sess = NULL;
ogs_gtp_xact_t *gtp_xact = NULL;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(bearer);
sess = sgwc_sess_find_by_id(bearer->sess_id);
ogs_assert(sess);
sgwc_ue = sgwc_ue_find_by_id(bearer->sgwc_ue_id);
ogs_assert(sgwc_ue);
ogs_assert(sgwc_ue->gnode);
ogs_debug("Downlink Data Notification [%d]", bearer->id);
ogs_debug(" MME_S11_TEID[%d] SGW_S11_TEID[%d]",
sgwc_ue->mme_s11_teid, sgwc_ue->sgw_s11_teid);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_DOWNLINK_DATA_NOTIFICATION_TYPE;
h.teid = sgwc_ue->mme_s11_teid;
pkbuf = sgwc_s11_build_downlink_data_notification(cause_value, bearer);
if (!pkbuf) {
ogs_error("sgwc_s11_build_downlink_data_notification() failed");
return OGS_ERROR;
}
gtp_xact = ogs_gtp_xact_local_create(
sgwc_ue->gnode, &h, pkbuf, bearer_timeout,
OGS_UINT_TO_POINTER(bearer->id));
if (!gtp_xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
gtp_xact->local_teid = sgwc_ue->sgw_s11_teid;
rv = ogs_gtp_xact_commit(gtp_xact);
ogs_expect(rv == OGS_OK);
return rv;
}
|