File size: 11,159 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
/*
* 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"
#include "pfcp-path.h"
#define SGWU_GTP_HANDLED 1
static ogs_pkbuf_pool_t *packet_pool = NULL;
static void _gtpv1_u_recv_cb(short when, ogs_socket_t fd, void *data)
{
int len;
ssize_t size;
char buf1[OGS_ADDRSTRLEN];
char buf2[OGS_ADDRSTRLEN];
sgwu_sess_t *sess = NULL;
ogs_pkbuf_t *pkbuf = NULL;
ogs_sock_t *sock = NULL;
ogs_sockaddr_t from;
ogs_gtp2_header_t *gtp_h = NULL;
ogs_gtp2_header_desc_t header_desc;
ogs_pfcp_user_plane_report_t report;
ogs_assert(fd != INVALID_SOCKET);
sock = data;
ogs_assert(sock);
pkbuf = ogs_pkbuf_alloc(packet_pool, OGS_MAX_PKT_LEN);
ogs_assert(pkbuf);
ogs_pkbuf_put(pkbuf, OGS_MAX_PKT_LEN);
size = ogs_recvfrom(fd, pkbuf->data, pkbuf->len, 0, &from);
if (size <= 0) {
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno,
"ogs_recv() failed");
goto cleanup;
}
ogs_pkbuf_trim(pkbuf, size);
ogs_assert(pkbuf);
ogs_assert(pkbuf->len);
gtp_h = (ogs_gtp2_header_t *)pkbuf->data;
if (gtp_h->version != OGS_GTP2_VERSION_1) {
ogs_error("[DROP] Invalid GTPU version [%d]", gtp_h->version);
ogs_log_hexdump(OGS_LOG_ERROR, pkbuf->data, pkbuf->len);
goto cleanup;
}
len = ogs_gtpu_parse_header(&header_desc, pkbuf);
if (len < 0) {
ogs_error("[DROP] Cannot decode GTPU packet");
ogs_log_hexdump(OGS_LOG_ERROR, pkbuf->data, pkbuf->len);
goto cleanup;
}
if (header_desc.type == OGS_GTPU_MSGTYPE_ECHO_REQ) {
ogs_pkbuf_t *echo_rsp;
ogs_debug("[RECV] Echo Request from [%s]", OGS_ADDR(&from, buf1));
echo_rsp = ogs_gtp2_handle_echo_req(pkbuf);
ogs_expect(echo_rsp);
if (echo_rsp) {
ssize_t sent;
/* Echo reply */
ogs_debug("[SEND] Echo Response to [%s]", OGS_ADDR(&from, buf1));
sent = ogs_sendto(fd, echo_rsp->data, echo_rsp->len, 0, &from);
if (sent < 0 || sent != echo_rsp->len) {
ogs_log_message(OGS_LOG_ERROR, ogs_socket_errno,
"ogs_sendto() failed");
}
ogs_pkbuf_free(echo_rsp);
}
goto cleanup;
}
if (header_desc.type != OGS_GTPU_MSGTYPE_END_MARKER &&
pkbuf->len <= len) {
ogs_error("[DROP] Small GTPU packet(type:%d len:%d)",
header_desc.type, len);
ogs_log_hexdump(OGS_LOG_ERROR, pkbuf->data, pkbuf->len);
goto cleanup;
}
ogs_trace("[RECV] GPU-U Type [%d] from [%s] : TEID[0x%x]",
header_desc.type, OGS_ADDR(&from, buf1), header_desc.teid);
/* Remove GTP header and send packets to peer NF */
ogs_assert(ogs_pkbuf_pull(pkbuf, len));
if (header_desc.type == OGS_GTPU_MSGTYPE_END_MARKER) {
ogs_pfcp_object_t *pfcp_object = NULL;
ogs_pfcp_pdr_t *pdr = NULL;
ogs_gtp2_header_desc_t sendhdr;
ogs_pkbuf_t *sendbuf = NULL;
pfcp_object = ogs_pfcp_object_find_by_teid(header_desc.teid);
if (!pfcp_object) {
/*
* Refer to the following 5G standard
*
* TS23.527 Restoration procedures
* 4.3 UPF Restoration Procedures
* 4.3.2 Restoration Procedure for PSA UPF Restart
*
* The UPF shall not send GTP-U Error indication message
* for a configurable period after an UPF restart
* when the UPF receives a G-PDU not matching any PDRs.
*/
if (ogs_time_ntp32_now() >
(ogs_pfcp_self()->local_recovery +
ogs_time_sec(ogs_local_conf()->time.message.pfcp.
association_interval))) {
ogs_error("[%s] Send Error Indication [TEID:0x%x] to [%s]",
OGS_ADDR(&sock->local_addr, buf1),
header_desc.teid,
OGS_ADDR(&from, buf2));
ogs_gtp1_send_error_indication(
sock, header_desc.teid, 0, &from);
}
goto cleanup;
}
switch(pfcp_object->type) {
case OGS_PFCP_OBJ_PDR_TYPE:
pdr = (ogs_pfcp_pdr_t *)pfcp_object;
ogs_assert(pdr);
break;
default:
ogs_fatal("Unknown type [%d]", pfcp_object->type);
ogs_assert_if_reached();
}
ogs_assert(pdr);
sendbuf = ogs_pkbuf_copy(pkbuf);
ogs_assert(sendbuf);
/* Forward packet */
memset(&sendhdr, 0, sizeof(sendhdr));
sendhdr.type = header_desc.type;
ogs_pfcp_send_g_pdu(pdr, &sendhdr, sendbuf);
} else if (header_desc.type == OGS_GTPU_MSGTYPE_ERR_IND) {
ogs_pfcp_far_t *far = NULL;
far = ogs_pfcp_far_find_by_gtpu_error_indication(pkbuf);
if (far) {
ogs_assert(true ==
ogs_pfcp_up_handle_error_indication(far, &report));
if (report.type.error_indication_report) {
ogs_assert(far->sess);
sess = SGWU_SESS(far->sess);
ogs_assert(sess);
ogs_assert(OGS_OK ==
sgwu_pfcp_send_session_report_request(sess, &report));
}
} else {
ogs_error("[DROP] Cannot find FAR by Error-Indication");
ogs_log_hexdump(OGS_LOG_ERROR, pkbuf->data, pkbuf->len);
}
} else if (header_desc.type == OGS_GTPU_MSGTYPE_GPDU) {
struct ip *ip_h = NULL;
ogs_pfcp_object_t *pfcp_object = NULL;
ogs_pfcp_sess_t *pfcp_sess = NULL;
ogs_pfcp_pdr_t *pdr = NULL;
ip_h = (struct ip *)pkbuf->data;
ogs_assert(ip_h);
pfcp_object = ogs_pfcp_object_find_by_teid(header_desc.teid);
if (!pfcp_object) {
/*
* Refer to the following 5G standard
*
* TS23.527 Restoration procedures
* 4.3 UPF Restoration Procedures
* 4.3.2 Restoration Procedure for PSA UPF Restart
*
* The UPF shall not send GTP-U Error indication message
* for a configurable period after an UPF restart
* when the UPF receives a G-PDU not matching any PDRs.
*/
if (ogs_time_ntp32_now() >
(ogs_pfcp_self()->local_recovery +
ogs_time_sec(ogs_local_conf()->time.message.pfcp.
association_interval))) {
ogs_error("[%s] Send Error Indication [TEID:0x%x] to [%s]",
OGS_ADDR(&sock->local_addr, buf1),
header_desc.teid,
OGS_ADDR(&from, buf2));
ogs_gtp1_send_error_indication(
sock, header_desc.teid, 0, &from);
}
goto cleanup;
}
switch(pfcp_object->type) {
case OGS_PFCP_OBJ_PDR_TYPE:
pdr = (ogs_pfcp_pdr_t *)pfcp_object;
ogs_assert(pdr);
break;
case OGS_PFCP_OBJ_SESS_TYPE:
/* SGWU does not use SESS TYPE */
ogs_assert_if_reached();
pfcp_sess = (ogs_pfcp_sess_t *)pfcp_object;
ogs_assert(pfcp_sess);
ogs_list_for_each(&pfcp_sess->pdr_list, pdr) {
/* Check if TEID */
if (header_desc.teid != pdr->f_teid.teid)
continue;
/* Check if QFI */
if (header_desc.qos_flow_identifier &&
pdr->qfi != header_desc.qos_flow_identifier)
continue;
/* Check if Rule List in PDR */
if (ogs_list_first(&pdr->rule_list) &&
ogs_pfcp_pdr_rule_find_by_packet(pdr, pkbuf) == NULL)
continue;
break;
}
if (!pdr) {
/* TODO : Send Error Indication */
goto cleanup;
}
break;
default:
ogs_fatal("Unknown type [%d]", pfcp_object->type);
ogs_assert_if_reached();
}
ogs_assert(pdr);
ogs_assert(true == ogs_pfcp_up_handle_pdr(
pdr, header_desc.type, &header_desc, pkbuf, &report));
if (report.type.downlink_data_report) {
ogs_assert(pdr->sess);
ogs_assert(pdr->sess->obj.type == OGS_PFCP_OBJ_SESS_TYPE);
sess = SGWU_SESS(pdr->sess);
ogs_assert(sess);
report.downlink_data.pdr_id = pdr->id;
report.downlink_data.qfi =
header_desc.qos_flow_identifier; /* for 5GC */
ogs_assert(OGS_OK ==
sgwu_pfcp_send_session_report_request(sess, &report));
}
/*
* The ogs_pfcp_up_handle_pdr() function
* buffers or frees the Packet Buffer(pkbuf) memory.
*/
return;
} else {
ogs_error("[DROP] Invalid GTPU Type [%d]", header_desc.type);
ogs_log_hexdump(OGS_LOG_ERROR, pkbuf->data, pkbuf->len);
}
cleanup:
ogs_pkbuf_free(pkbuf);
}
int sgwu_gtp_init(void)
{
ogs_pkbuf_config_t config;
memset(&config, 0, sizeof config);
config.cluster_2048_pool = ogs_app()->pool.packet;
#if OGS_USE_TALLOC == 1
/* allocate a talloc pool for GTP to ensure it doesn't have to go back
* to the libc malloc all the time */
packet_pool = talloc_pool(__ogs_talloc_core, 1000*1024);
ogs_assert(packet_pool);
#else
packet_pool = ogs_pkbuf_pool_create(&config);
#endif
return OGS_OK;
}
void sgwu_gtp_final(void)
{
ogs_pkbuf_pool_destroy(packet_pool);
}
int sgwu_gtp_open(void)
{
ogs_socknode_t *node = NULL;
ogs_sock_t *sock = NULL;
ogs_list_for_each(&ogs_gtp_self()->gtpu_list, node) {
sock = ogs_gtp_server(node);
if (!sock) return OGS_ERROR;
if (sock->family == AF_INET)
ogs_gtp_self()->gtpu_sock = sock;
else if (sock->family == AF_INET6)
ogs_gtp_self()->gtpu_sock6 = sock;
node->poll = ogs_pollset_add(ogs_app()->pollset,
OGS_POLLIN, sock->fd, _gtpv1_u_recv_cb, sock);
ogs_assert(node->poll);
}
OGS_SETUP_GTPU_SERVER;
return OGS_OK;
}
void sgwu_gtp_close(void)
{
ogs_socknode_remove_all(&ogs_gtp_self()->gtpu_list);
}
|