/* * Copyright (C) 2019 by Sukchan Lee * * 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 . */ #include "context.h" #include "pfcp-path.h" #include "gtp-path.h" #include "n4-handler.h" static void setup_gtp_node(ogs_pfcp_far_t *far) { int rv; ogs_ip_t ip; ogs_gtp_node_t *gnode = NULL; ogs_assert(far); ogs_pfcp_outer_header_creation_to_ip(&far->outer_header_creation, &ip); /* No Outer Header Creation */ if (ip.len == 0) return; gnode = ogs_gtp_node_find_by_ip(&upf_self()->peer_list, &ip); if (!gnode) { gnode = ogs_gtp_node_add_by_ip( &upf_self()->peer_list, &ip, upf_self()->gtpu_port, ogs_app()->parameter.no_ipv4, ogs_app()->parameter.no_ipv6, ogs_app()->parameter.prefer_ipv4); ogs_assert(gnode); rv = ogs_gtp_connect( upf_self()->gtpu_sock, upf_self()->gtpu_sock6, gnode); ogs_assert(rv == OGS_OK); } OGS_SETUP_GTP_NODE(far, gnode); ogs_pfcp_far_hash_set(far); } void upf_n4_handle_session_establishment_request( upf_sess_t *sess, ogs_pfcp_xact_t *xact, ogs_pfcp_session_establishment_request_t *req) { ogs_pfcp_pdr_t *pdr = NULL; ogs_pfcp_far_t *far = NULL; ogs_pfcp_pdr_t *created_pdr[OGS_MAX_NUM_OF_PDR]; int num_of_created_pdr = 0; uint8_t cause_value = 0; uint8_t offending_ie_value = 0; int i; ogs_assert(xact); ogs_assert(req); ogs_debug("Session Establishment Request"); cause_value = OGS_PFCP_CAUSE_REQUEST_ACCEPTED; if (!sess) { ogs_error("No Context"); ogs_pfcp_send_error_message(xact, 0, OGS_PFCP_SESSION_ESTABLISHMENT_RESPONSE_TYPE, OGS_PFCP_CAUSE_SESSION_CONTEXT_NOT_FOUND, 0); return; } for (i = 0; i < OGS_MAX_NUM_OF_PDR; i++) { created_pdr[i] = ogs_pfcp_handle_create_pdr(&sess->pfcp, &req->create_pdr[i], &cause_value, &offending_ie_value); if (created_pdr[i] == NULL) break; } num_of_created_pdr = i; if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_FAR; i++) { if (ogs_pfcp_handle_create_far(&sess->pfcp, &req->create_far[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_QER; i++) { if (ogs_pfcp_handle_create_qer(&sess->pfcp, &req->create_qer[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; /* Setup GTP Node */ ogs_list_for_each(&sess->pfcp.far_list, far) setup_gtp_node(far); /* Setup UPF-N3-TEID & QFI Hash */ for (i = 0; i < num_of_created_pdr; i++) { pdr = created_pdr[i]; ogs_assert(pdr); if (pdr->src_if == OGS_PFCP_INTERFACE_ACCESS) { /* Uplink */ if (pdr->f_teid_len) ogs_pfcp_pdr_hash_set(pdr); } } /* Send Buffered Packet to gNB/SGW */ ogs_list_for_each(&sess->pfcp.pdr_list, pdr) { if (pdr->src_if == OGS_PFCP_INTERFACE_CORE) { /* Downlink */ ogs_pfcp_send_buffered_packet(pdr); } } upf_pfcp_send_session_establishment_response( xact, sess, created_pdr, num_of_created_pdr); return; cleanup: ogs_pfcp_sess_clear(&sess->pfcp); ogs_pfcp_send_error_message(xact, sess ? sess->smf_n4_seid : 0, OGS_PFCP_SESSION_ESTABLISHMENT_RESPONSE_TYPE, cause_value, offending_ie_value); } void upf_n4_handle_session_modification_request( upf_sess_t *sess, ogs_pfcp_xact_t *xact, ogs_pfcp_session_modification_request_t *req) { ogs_pfcp_pdr_t *pdr = NULL; ogs_pfcp_far_t *far = NULL; ogs_pfcp_pdr_t *created_pdr[OGS_MAX_NUM_OF_PDR]; int num_of_created_pdr = 0; uint8_t cause_value = 0; uint8_t offending_ie_value = 0; int i; ogs_assert(xact); ogs_assert(req); ogs_debug("Session Modification Request"); cause_value = OGS_PFCP_CAUSE_REQUEST_ACCEPTED; if (!sess) { ogs_error("No Context"); ogs_pfcp_send_error_message(xact, 0, OGS_PFCP_SESSION_MODIFICATION_RESPONSE_TYPE, OGS_PFCP_CAUSE_SESSION_CONTEXT_NOT_FOUND, 0); return; } for (i = 0; i < OGS_MAX_NUM_OF_PDR; i++) { created_pdr[i] = ogs_pfcp_handle_create_pdr(&sess->pfcp, &req->create_pdr[i], &cause_value, &offending_ie_value); if (created_pdr[i] == NULL) break; } num_of_created_pdr = i; if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_PDR; i++) { if (ogs_pfcp_handle_update_pdr(&sess->pfcp, &req->update_pdr[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_PDR; i++) { if (ogs_pfcp_handle_remove_pdr(&sess->pfcp, &req->remove_pdr[i], &cause_value, &offending_ie_value) == false) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_FAR; i++) { if (ogs_pfcp_handle_create_far(&sess->pfcp, &req->create_far[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_FAR; i++) { if (ogs_pfcp_handle_update_far_flags(&sess->pfcp, &req->update_far[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; /* Send End Marker to gNB */ ogs_list_for_each(&sess->pfcp.pdr_list, pdr) { far = pdr->far; if (far && far->smreq_flags.send_end_marker_packets) ogs_pfcp_send_end_marker(pdr); } /* Clear PFCPSMReq-Flags */ ogs_list_for_each(&sess->pfcp.far_list, far) far->smreq_flags.value = 0; for (i = 0; i < OGS_MAX_NUM_OF_FAR; i++) { if (ogs_pfcp_handle_update_far(&sess->pfcp, &req->update_far[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_FAR; i++) { if (ogs_pfcp_handle_remove_far(&sess->pfcp, &req->remove_far[i], &cause_value, &offending_ie_value) == false) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_QER; i++) { if (ogs_pfcp_handle_create_qer(&sess->pfcp, &req->create_qer[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_QER; i++) { if (ogs_pfcp_handle_update_qer(&sess->pfcp, &req->update_qer[i], &cause_value, &offending_ie_value) == NULL) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; for (i = 0; i < OGS_MAX_NUM_OF_QER; i++) { if (ogs_pfcp_handle_remove_qer(&sess->pfcp, &req->remove_qer[i], &cause_value, &offending_ie_value) == false) break; } if (cause_value != OGS_PFCP_CAUSE_REQUEST_ACCEPTED) goto cleanup; /* Setup GTP Node */ ogs_list_for_each(&sess->pfcp.far_list, far) setup_gtp_node(far); /* Setup UPF-N3-TEID & QFI Hash */ for (i = 0; i < num_of_created_pdr; i++) { pdr = created_pdr[i]; ogs_assert(pdr); if (pdr->src_if == OGS_PFCP_INTERFACE_ACCESS) { /* Uplink */ if (pdr->f_teid_len) ogs_pfcp_pdr_hash_set(pdr); } } /* Send Buffered Packet to gNB/SGW */ ogs_list_for_each(&sess->pfcp.pdr_list, pdr) { if (pdr->src_if == OGS_PFCP_INTERFACE_CORE) { /* Downlink */ ogs_pfcp_send_buffered_packet(pdr); } } upf_pfcp_send_session_modification_response( xact, sess, created_pdr, num_of_created_pdr); return; cleanup: ogs_pfcp_sess_clear(&sess->pfcp); ogs_pfcp_send_error_message(xact, sess ? sess->smf_n4_seid : 0, OGS_PFCP_SESSION_MODIFICATION_RESPONSE_TYPE, cause_value, offending_ie_value); } void upf_n4_handle_session_deletion_request( upf_sess_t *sess, ogs_pfcp_xact_t *xact, ogs_pfcp_session_deletion_request_t *req) { ogs_assert(xact); ogs_assert(req); ogs_debug("Session Deletion Request"); if (!sess) { ogs_error("No Context"); ogs_pfcp_send_error_message(xact, 0, OGS_PFCP_SESSION_DELETION_RESPONSE_TYPE, OGS_PFCP_CAUSE_SESSION_CONTEXT_NOT_FOUND, 0); return; } ogs_assert(sess); upf_pfcp_send_session_deletion_response(xact, sess); upf_sess_remove(sess); }