File size: 28,730 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 |
/*
* 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/>.
*/
#include "ogs-gtp.h"
#include "mme-event.h"
#include "mme-gn-build.h"
#include "mme-gtp-path.h"
#include "mme-path.h"
#include "s1ap-path.h"
#include "mme-s11-build.h"
#include "mme-sm.h"
static void _gtpv1v2_c_recv_cb(short when, ogs_socket_t fd, void *data)
{
int rv;
char buf[OGS_ADDRSTRLEN];
ssize_t size;
mme_event_t *e = NULL;
ogs_pkbuf_t *pkbuf = NULL;
ogs_sockaddr_t from;
mme_sgw_t *sgw = NULL;
mme_sgsn_t *sgsn = NULL;
uint8_t gtp_ver;
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);
gtp_ver = ((ogs_gtp2_header_t *)pkbuf->data)->version;
switch (gtp_ver) {
case 1:
sgsn = mme_sgsn_find_by_addr(&from);
if (!sgsn) {
ogs_error("Unknown SGSN : %s", OGS_ADDR(&from, buf));
ogs_pkbuf_free(pkbuf);
return;
}
ogs_assert(sgsn);
e = mme_event_new(MME_EVENT_GN_MESSAGE);
ogs_assert(e);
e->gnode = &sgsn->gnode;
break;
case 2:
sgw = mme_sgw_find_by_addr(&from);
if (!sgw) {
ogs_error("Unknown SGW : %s", OGS_ADDR(&from, buf));
ogs_pkbuf_free(pkbuf);
return;
}
ogs_assert(sgw);
e = mme_event_new(MME_EVENT_S11_MESSAGE);
ogs_assert(e);
e->gnode = &sgw->gnode;
break;
default:
ogs_warn("Rx unexpected GTP version %u", gtp_ver);
ogs_pkbuf_free(pkbuf);
return;
}
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);
mme_event_free(e);
}
}
static void timeout(ogs_gtp_xact_t *xact, void *data)
{
int r;
mme_ue_t *mme_ue = NULL;
ogs_pool_id_t mme_ue_id = OGS_INVALID_POOL_ID;
enb_ue_t *enb_ue = NULL;
mme_sess_t *sess = NULL;
ogs_pool_id_t sess_id = OGS_INVALID_POOL_ID;
mme_bearer_t *bearer = NULL;
ogs_pool_id_t bearer_id = OGS_INVALID_POOL_ID;
uint8_t type = 0;
ogs_assert(xact);
type = xact->seq[0].type;
switch (type) {
case OGS_GTP2_MODIFY_BEARER_REQUEST_TYPE:
case OGS_GTP2_RELEASE_ACCESS_BEARERS_REQUEST_TYPE:
case OGS_GTP2_CREATE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE:
case OGS_GTP2_DELETE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE:
mme_ue_id = OGS_POINTER_TO_UINT(data);
ogs_assert(mme_ue_id >= OGS_MIN_POOL_ID &&
mme_ue_id <= OGS_MAX_POOL_ID);
mme_ue = mme_ue_find_by_id(mme_ue_id);
if (!mme_ue) {
ogs_error("MME-UE[%d] has already been removed [%d]",
mme_ue_id, type);
return;
}
break;
case OGS_GTP2_CREATE_SESSION_REQUEST_TYPE:
case OGS_GTP2_DELETE_SESSION_REQUEST_TYPE:
sess_id = OGS_POINTER_TO_UINT(data);
ogs_assert(sess_id >= OGS_MIN_POOL_ID && sess_id <= OGS_MAX_POOL_ID);
sess = mme_sess_find_by_id(sess_id);
if (!sess) {
ogs_error("Session[%d] has already been removed [%d]",
sess_id, type);
return;
}
mme_ue = mme_ue_find_by_id(sess->mme_ue_id);
ogs_assert(mme_ue);
break;
case OGS_GTP2_BEARER_RESOURCE_COMMAND_TYPE:
bearer_id = OGS_POINTER_TO_UINT(data);
ogs_assert(bearer_id >= OGS_MIN_POOL_ID &&
bearer_id <= OGS_MAX_POOL_ID);
bearer = mme_bearer_find_by_id(bearer_id);
if (!bearer) {
ogs_error("Bearer[%d] has already been removed [%d]",
bearer_id, type);
return;
}
sess = mme_sess_find_by_id(bearer->sess_id);
ogs_assert(sess);
mme_ue = mme_ue_find_by_id(sess->mme_ue_id);
ogs_assert(mme_ue);
break;
default:
ogs_fatal("Invalid type [%d]", type);
ogs_assert_if_reached();
break;
}
ogs_assert(mme_ue);
switch (type) {
case OGS_GTP2_DELETE_SESSION_REQUEST_TYPE:
/*
* If SESSION_CONTEXT_WILL_DELETED(MME_UE) is not cleared,
* The MME cannot send Delete-Session-Request to the SGW-C.
* As such, it could be the infinite loop occurred in EMM state machine.
*
* To prevent this situation,
* force clearing SESSION_CONTEXT_WILL_DELETED variable
* when MME does not receive Delete-Session-Response message from SGW-C.
*/
CLEAR_SESSION_CONTEXT(mme_ue);
enb_ue = enb_ue_find_by_id(mme_ue->enb_ue_id);
if (enb_ue) {
r = s1ap_send_ue_context_release_command(enb_ue,
S1AP_Cause_PR_nas, S1AP_CauseNas_normal_release,
S1AP_UE_CTX_REL_UE_CONTEXT_REMOVE, 0);
ogs_expect(r == OGS_OK);
ogs_assert(r != OGS_ERROR);
} else {
ogs_warn("No S1 Context");
}
break;
case OGS_GTP2_BEARER_RESOURCE_COMMAND_TYPE:
/* Nothing to do */
break;
default:
mme_send_delete_session_or_mme_ue_context_release(mme_ue);
break;
}
ogs_error("GTP Timeout : IMSI[%s] Message-Type[%d]",
mme_ue->imsi_bcd, type);
}
int mme_gtp_open(void)
{
int rv;
ogs_socknode_t *node = NULL;
ogs_sock_t *sock = NULL;
mme_sgw_t *sgw = NULL;
mme_sgsn_t *sgsn = 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, _gtpv1v2_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, _gtpv1v2_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);
mme_self()->pgw_addr = mme_pgw_addr_find_by_apn_enb(
&mme_self()->pgw_list, AF_INET, NULL);
mme_self()->pgw_addr6 = mme_pgw_addr_find_by_apn_enb(
&mme_self()->pgw_list, AF_INET6, NULL);
ogs_assert(mme_self()->pgw_addr || mme_self()->pgw_addr6);
ogs_list_for_each(&mme_self()->sgw_list, sgw) {
rv = ogs_gtp_connect(
ogs_gtp_self()->gtpc_sock, ogs_gtp_self()->gtpc_sock6,
&sgw->gnode);
ogs_assert(rv == OGS_OK);
}
ogs_list_for_each(&mme_self()->sgsn_list, sgsn) {
rv = ogs_gtp_connect(
ogs_gtp_self()->gtpc_sock, ogs_gtp_self()->gtpc_sock6,
&sgsn->gnode);
ogs_assert(rv == OGS_OK);
}
return OGS_OK;
}
void mme_gtp_close(void)
{
ogs_socknode_remove_all(&ogs_gtp_self()->gtpc_list);
ogs_socknode_remove_all(&ogs_gtp_self()->gtpc_list6);
}
int mme_gtp_send_create_session_request(mme_sess_t *sess, int create_action)
{
int rv;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_gtp_xact_t *xact = NULL;
mme_ue_t *mme_ue = NULL;
sgw_ue_t *sgw_ue = NULL;
mme_ue = mme_ue_find_by_id(sess->mme_ue_id);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
if (create_action == OGS_GTP_CREATE_IN_PATH_SWITCH_REQUEST) {
sgw_ue = sgw_ue_find_by_id(sgw_ue->target_ue_id);
ogs_assert(sgw_ue);
}
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_CREATE_SESSION_REQUEST_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_create_session_request(h.type, sess, create_action);
if (!pkbuf) {
ogs_error("mme_s11_build_create_session_request() failed");
return OGS_ERROR;
}
xact = ogs_gtp_xact_local_create(
sgw_ue->gnode, &h, pkbuf, timeout,
OGS_UINT_TO_POINTER(sess->id));
if (!xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
xact->create_action = create_action;
xact->local_teid = mme_ue->gn.mme_gn_teid;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int mme_gtp_send_modify_bearer_request(
mme_ue_t *mme_ue, int uli_presence, int modify_action)
{
int rv;
ogs_gtp_xact_t *xact = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_MODIFY_BEARER_REQUEST_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_modify_bearer_request(h.type, mme_ue, uli_presence);
if (!pkbuf) {
ogs_error("mme_s11_build_modify_bearer_request() failed");
return OGS_ERROR;
}
xact = ogs_gtp_xact_local_create(
sgw_ue->gnode, &h, pkbuf, timeout,
OGS_UINT_TO_POINTER(mme_ue->id));
if (!xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
xact->modify_action = modify_action;
xact->local_teid = mme_ue->gn.mme_gn_teid;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int mme_gtp_send_delete_session_request(
sgw_ue_t *sgw_ue, mme_sess_t *sess, int action)
{
int rv;
ogs_pkbuf_t *s11buf = NULL;
ogs_gtp2_header_t h;
ogs_gtp_xact_t *xact = NULL;
mme_ue_t *mme_ue = NULL;
ogs_assert(action);
ogs_assert(sess);
mme_ue = mme_ue_find_by_id(sess->mme_ue_id);
ogs_assert(mme_ue);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_DELETE_SESSION_REQUEST_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
s11buf = mme_s11_build_delete_session_request(h.type, sess, action);
if (!s11buf) {
ogs_error("mme_s11_build_delete_session_request() failed");
return OGS_ERROR;
}
xact = ogs_gtp_xact_local_create(
sgw_ue->gnode, &h, s11buf, timeout,
OGS_UINT_TO_POINTER(sess->id));
if (!xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
xact->delete_action = action;
xact->local_teid = mme_ue->gn.mme_gn_teid;
ogs_debug("delete_session_request - xact:%p, sess:%p", xact, sess);
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
void mme_gtp_send_delete_all_sessions(mme_ue_t *mme_ue, int action)
{
mme_sess_t *sess = NULL, *next_sess = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
ogs_assert(action);
ogs_list_for_each_safe(&mme_ue->sess_list, next_sess, sess) {
if (MME_HAVE_SGW_S1U_PATH(sess)) {
mme_gtp_send_delete_session_request(sgw_ue, sess, action);
} else {
MME_SESS_CLEAR(sess);
}
}
}
int mme_gtp_send_create_bearer_response(
mme_bearer_t *bearer, uint8_t cause_value)
{
int rv;
ogs_gtp_xact_t *xact = NULL;
mme_ue_t *mme_ue = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(bearer);
ogs_assert(bearer->create.xact_id >= OGS_MIN_POOL_ID &&
bearer->create.xact_id <= OGS_MAX_POOL_ID);
xact = ogs_gtp_xact_find_by_id(bearer->create.xact_id);
if (!xact) {
ogs_error("GTP transaction(CREATE) has already been removed");
return OGS_OK;
}
mme_ue = mme_ue_find_by_id(bearer->mme_ue_id);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_CREATE_BEARER_RESPONSE_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_create_bearer_response(h.type, bearer, cause_value);
if (!pkbuf) {
ogs_error("mme_s11_build_create_bearer_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 mme_gtp_send_update_bearer_response(
mme_bearer_t *bearer, uint8_t cause_value)
{
int rv;
ogs_gtp_xact_t *xact = NULL, *next_xact = NULL;
mme_ue_t *mme_ue = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(bearer);
mme_ue = mme_ue_find_by_id(bearer->mme_ue_id);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
/*
* Issues #3240
*
* SMF->SGW-C->MME: First Update Bearer Request
* MME->UE: First Modify EPS bearer context request
* SMF->SGW-C->MME: Second Update Bearer Request
* MME->UE: Second Modify EPS bearer context request
* UE->MME: First Modify EPS bearer context accept
* MME->SGW-C->SMF: First Update Bearer Response
* UE->MME: Second Modify EPS bearer context accept
* MME->SGW-C->SMF: Second Update Bearer Response
*
* After sending the Update Bearer Response, remove the corresponding
* Transaction Node from the list managed by the Bearer Context.
*/
ogs_list_for_each_entry_safe(
&bearer->update.xact_list, next_xact, xact, to_update_node) {
ogs_list_remove(&bearer->update.xact_list, &xact->to_update_node);
break;
}
if (!xact) {
ogs_warn("GTP transaction(UPDATE) has already been removed");
return OGS_OK;
}
/*
* eNB sends Modify EPS Bearer Accept to the MME
* MME can send Update Bearer Response to the SGW-C,
* so stop the peer waiting timer
*/
ogs_timer_stop(xact->tm_peer);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_UPDATE_BEARER_RESPONSE_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_update_bearer_response(h.type, bearer, cause_value);
if (!pkbuf) {
ogs_error("mme_s11_build_update_bearer_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 mme_gtp_send_delete_bearer_response(
mme_bearer_t *bearer, uint8_t cause_value)
{
int rv;
ogs_gtp_xact_t *xact = NULL;
mme_ue_t *mme_ue = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(bearer);
ogs_assert(bearer->delete.xact_id >= OGS_MIN_POOL_ID &&
bearer->delete.xact_id <= OGS_MAX_POOL_ID);
xact = ogs_gtp_xact_find_by_id(bearer->delete.xact_id);
if (!xact) {
ogs_error("GTP transaction(DELETE) has already been removed");
return OGS_OK;
}
mme_ue = mme_ue_find_by_id(bearer->mme_ue_id);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_DELETE_BEARER_RESPONSE_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_delete_bearer_response(h.type, bearer, cause_value);
if (!pkbuf) {
ogs_error("mme_s11_build_delete_bearer_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 mme_gtp_send_release_access_bearers_request(mme_ue_t *mme_ue, int action)
{
int rv;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_gtp_xact_t *xact = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_assert(action);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_RELEASE_ACCESS_BEARERS_REQUEST_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_release_access_bearers_request(h.type);
if (!pkbuf) {
ogs_error("mme_s11_build_release_access_bearers_request() failed");
return OGS_ERROR;
}
xact = ogs_gtp_xact_local_create(
sgw_ue->gnode, &h, pkbuf, timeout,
OGS_UINT_TO_POINTER(mme_ue->id));
if (!xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
xact->release_action = action;
xact->local_teid = mme_ue->gn.mme_gn_teid;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
void mme_gtp_send_release_all_ue_in_enb(mme_enb_t *enb, int action)
{
mme_ue_t *mme_ue = NULL;
enb_ue_t *enb_ue = NULL, *next = NULL;
ogs_list_for_each_safe(&enb->enb_ue_list, next, enb_ue) {
mme_ue = mme_ue_find_by_id(enb_ue->mme_ue_id);
if (mme_ue) {
if (action == OGS_GTP_RELEASE_S1_CONTEXT_REMOVE_BY_LO_CONNREFUSED) {
/*
* https://github.com/open5gs/open5gs/pull/1497
*
* 1. eNB, SGW-U and UPF go offline at the same time.
* 2. MME sends Release Access Bearer Request to SGW-C
* 3. SGW-C/SMF sends PFCP modification,
* but SGW-U/UPF does not respond.
* 4. MME does not receive Release Access Bearer Response.
* 5. timeout()
* 6. MME sends Delete Session Request to the SGW-C/SMF
* 7. No SGW-U/UPF, so timeout()
* 8. MME sends UEContextReleaseRequest enb_ue.
* 9. But there is no enb_ue, so MME crashed.
*
* To solve this situation,
* Execute enb_ue_unlink(mme_ue) and enb_ue_remove(enb_ue)
* before mme_gtp_send_release_access_bearers_request()
*/
enb_ue_unlink(mme_ue);
enb_ue_remove(enb_ue);
}
ogs_assert(OGS_OK ==
mme_gtp_send_release_access_bearers_request(mme_ue, action));
} else {
ogs_warn("mme_gtp_send_release_all_ue_in_enb()");
ogs_warn(" ENB_UE_S1AP_ID[%d] MME_UE_S1AP_ID[%d] Action[%d]",
enb_ue->enb_ue_s1ap_id, enb_ue->mme_ue_s1ap_id, action);
if (action == OGS_GTP_RELEASE_S1_CONTEXT_REMOVE_BY_LO_CONNREFUSED ||
action == OGS_GTP_RELEASE_S1_CONTEXT_REMOVE_BY_RESET_ALL) {
enb_ue_remove(enb_ue);
} else {
/* At this point, it does not support other action */
ogs_assert_if_reached();
}
}
}
}
int mme_gtp_send_downlink_data_notification_ack(
mme_bearer_t *bearer, uint8_t cause_value)
{
int rv;
mme_ue_t *mme_ue = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_gtp_xact_t *xact = NULL;
ogs_gtp2_header_t h;
ogs_pkbuf_t *s11buf = NULL;
ogs_assert(bearer);
ogs_assert(bearer->notify.xact_id >= OGS_MIN_POOL_ID &&
bearer->notify.xact_id <= OGS_MAX_POOL_ID);
xact = ogs_gtp_xact_find_by_id(bearer->notify.xact_id);
if (!xact) {
ogs_error("GTP transaction(NOTIFY) has already been removed");
return OGS_OK;
}
mme_ue = mme_ue_find_by_id(bearer->mme_ue_id);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
/* Build Downlink data notification ack */
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_DOWNLINK_DATA_NOTIFICATION_ACKNOWLEDGE_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
s11buf = mme_s11_build_downlink_data_notification_ack(h.type, cause_value);
if (!s11buf) {
ogs_error("mme_s11_build_downlink_data_notification_ack() failed");
return OGS_ERROR;
}
rv = ogs_gtp_xact_update_tx(xact, &h, s11buf);
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 mme_gtp_send_create_indirect_data_forwarding_tunnel_request(
mme_ue_t *mme_ue)
{
int rv;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_gtp_xact_t *xact = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_CREATE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_create_indirect_data_forwarding_tunnel_request(
h.type, mme_ue);
if (!pkbuf) {
ogs_error("mme_s11_build_create_indirect_data_forwarding_"
"tunnel_request() failed");
return OGS_ERROR;
}
xact = ogs_gtp_xact_local_create(
sgw_ue->gnode, &h, pkbuf, timeout,
OGS_UINT_TO_POINTER(mme_ue->id));
if (!xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
xact->local_teid = mme_ue->gn.mme_gn_teid;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int mme_gtp_send_delete_indirect_data_forwarding_tunnel_request(
mme_ue_t *mme_ue, int action)
{
int rv;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_gtp_xact_t *xact = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_assert(action);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_DELETE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = ogs_pkbuf_alloc(NULL, OGS_TLV_MAX_HEADROOM);
if (!pkbuf) {
ogs_error("ogs_pkbuf_alloc() failed");
return OGS_ERROR;
}
ogs_pkbuf_reserve(pkbuf, OGS_TLV_MAX_HEADROOM);
xact = ogs_gtp_xact_local_create(
sgw_ue->gnode, &h, pkbuf, timeout,
OGS_UINT_TO_POINTER(mme_ue->id));
if (!xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
xact->delete_indirect_action = action;
xact->local_teid = mme_ue->gn.mme_gn_teid;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int mme_gtp_send_bearer_resource_command(
mme_bearer_t *bearer, ogs_nas_eps_message_t *nas_message)
{
int rv;
ogs_gtp2_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_gtp_xact_t *xact = NULL;
mme_ue_t *mme_ue = NULL;
sgw_ue_t *sgw_ue = NULL;
ogs_assert(bearer);
mme_ue = mme_ue_find_by_id(bearer->mme_ue_id);
ogs_assert(mme_ue);
sgw_ue = sgw_ue_find_by_id(mme_ue->sgw_ue_id);
ogs_assert(sgw_ue);
memset(&h, 0, sizeof(ogs_gtp2_header_t));
h.type = OGS_GTP2_BEARER_RESOURCE_COMMAND_TYPE;
h.teid = sgw_ue->sgw_s11_teid;
pkbuf = mme_s11_build_bearer_resource_command(h.type, bearer, nas_message);
if (!pkbuf) {
ogs_error("mme_s11_build_bearer_resource_command() failed");
return OGS_ERROR;
}
xact = ogs_gtp_xact_local_create(
sgw_ue->gnode, &h, pkbuf, timeout,
OGS_UINT_TO_POINTER(bearer->id));
if (!xact) {
ogs_error("ogs_gtp_xact_local_create() failed");
return OGS_ERROR;
}
xact->xid |= OGS_GTP_CMD_XACT_ID;
xact->local_teid = mme_ue->gn.mme_gn_teid;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
/*************************
* GTPv1C (Gn interface):
*************************/
int mme_gtp1_send_sgsn_context_request(
mme_sgsn_t *sgsn, mme_ue_t *mme_ue, const ogs_nas_p_tmsi_signature_t *ptmsi_sig)
{
int rv;
ogs_gtp1_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_gtp_xact_t *xact = NULL;
ogs_assert(sgsn);
memset(&h, 0, sizeof(ogs_gtp1_header_t));
h.type = OGS_GTP1_SGSN_CONTEXT_REQUEST_TYPE;
h.teid = 0;
pkbuf = mme_gn_build_sgsn_context_request(mme_ue, ptmsi_sig);
if (!pkbuf) {
ogs_error("mme_gn_build_ran_information_relay() failed");
return OGS_ERROR;
}
xact = ogs_gtp1_xact_local_create(&sgsn->gnode, &h, pkbuf, NULL, NULL);
if (!xact) {
ogs_error("ogs_gtp1_xact_local_create() failed");
return OGS_ERROR;
}
/* TS 29.060 8.2: "The SGSN Context Request message, where the Tunnel
* Endpoint Identifier shall be set to all zeroes." */
xact->local_teid = 0;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int mme_gtp1_send_sgsn_context_response(
mme_ue_t *mme_ue, uint8_t cause, ogs_gtp_xact_t *xact)
{
int rv;
ogs_gtp1_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
memset(&h, 0, sizeof(ogs_gtp1_header_t));
h.type = OGS_GTP1_SGSN_CONTEXT_RESPONSE_TYPE;
h.teid = mme_ue ? mme_ue->gn.sgsn_gn_teid : 0;
pkbuf = mme_gn_build_sgsn_context_response(mme_ue, cause);
if (!pkbuf) {
ogs_error("mme_gn_build_sgsn_context_response() failed");
return OGS_ERROR;
}
xact->local_teid = mme_ue ? mme_ue->gn.mme_gn_teid : 0;
rv = ogs_gtp1_xact_update_tx(xact, &h, pkbuf);
if (rv != OGS_OK) {
ogs_error("ogs_gtp1_xact_update_tx() failed");
return OGS_ERROR;
}
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int mme_gtp1_send_sgsn_context_ack(
mme_ue_t *mme_ue, uint8_t cause, ogs_gtp_xact_t *xact)
{
int rv;
ogs_gtp1_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_assert(mme_ue);
memset(&h, 0, sizeof(ogs_gtp1_header_t));
h.type = OGS_GTP1_SGSN_CONTEXT_ACKNOWLEDGE_TYPE;
h.teid = mme_ue->gn.sgsn_gn_teid;
pkbuf = mme_gn_build_sgsn_context_ack(mme_ue, cause);
if (!pkbuf) {
ogs_error("mme_gn_build_sgsn_context_response() failed");
return OGS_ERROR;
}
xact->local_teid = mme_ue->gn.mme_gn_teid;
rv = ogs_gtp1_xact_update_tx(xact, &h, pkbuf);
if (rv != OGS_OK) {
ogs_error("ogs_gtp1_xact_update_tx() failed");
return OGS_ERROR;
}
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
int mme_gtp1_send_ran_information_relay(
mme_sgsn_t *sgsn, const uint8_t *buf, size_t len,
const ogs_nas_rai_t *rai, uint16_t cell_id)
{
int rv;
ogs_gtp1_header_t h;
ogs_pkbuf_t *pkbuf = NULL;
ogs_gtp_xact_t *xact = NULL;
ogs_assert(sgsn);
ogs_assert(buf);
memset(&h, 0, sizeof(ogs_gtp1_header_t));
h.type = OGS_GTP1_RAN_INFORMATION_RELAY_TYPE;
h.teid = 0;
pkbuf = mme_gn_build_ran_information_relay(h.type, buf, len, rai, cell_id);
if (!pkbuf) {
ogs_error("mme_gn_build_ran_information_relay() failed");
return OGS_ERROR;
}
xact = ogs_gtp1_xact_local_create(&sgsn->gnode, &h, pkbuf, NULL, NULL);
if (!xact) {
ogs_error("ogs_gtp1_xact_local_create() failed");
return OGS_ERROR;
}
/* TS 29.060 8.2: "The RAN Information Relay message, where the Tunnel
* Endpoint Identifier shall be set to all zeroes." */
xact->local_teid = 0;
rv = ogs_gtp_xact_commit(xact);
ogs_expect(rv == OGS_OK);
return rv;
}
|