Coverage Report

Created: 2025-08-29 07:10

/src/open5gs/lib/gtp/v1/path.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com>
3
 * Copyright (C) 2022 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4
 * Copyright (C) 2023 by Sukchan Lee <acetcom@gmail.com>
5
 *
6
 * This file is part of Open5GS.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include "ogs-gtp.h"
23
24
ogs_pkbuf_t *ogs_gtp1_handle_echo_req(ogs_pkbuf_t *pkb)
25
0
{
26
0
    ogs_gtp1_header_t *gtph = NULL;
27
0
    ogs_pkbuf_t *pkb_resp = NULL;
28
0
    ogs_gtp1_header_t *gtph_resp = NULL;
29
0
    uint16_t length;
30
0
    int idx;
31
32
0
    ogs_assert(pkb);
33
34
0
    gtph = (ogs_gtp1_header_t *)pkb->data;
35
    /* Check GTP version. Now only support GTPv1(version = 1) */
36
0
    if (gtph->version != 1) {
37
0
        return NULL;
38
0
    }
39
40
0
    if (gtph->type != OGS_GTP1_ECHO_REQUEST_TYPE) {
41
0
        return NULL;
42
0
    }
43
44
45
0
    pkb_resp = ogs_pkbuf_alloc(NULL,
46
0
            100 /* enough for ECHO_RSP; use smaller buffer */);
47
0
    if (!pkb_resp) {
48
0
        ogs_error("ogs_pkbuf_alloc() failed");
49
0
        return NULL;
50
0
    }
51
0
    ogs_pkbuf_put(pkb_resp, 100);
52
0
    gtph_resp = (ogs_gtp1_header_t *)pkb_resp->data;
53
54
    /* reply back immediately */
55
0
    gtph_resp->version = 1; /* set version */
56
0
    gtph_resp->pt = 1; /* set PT */
57
0
    gtph_resp->type = OGS_GTP1_ECHO_RESPONSE_TYPE;
58
0
    length = 0;     /* length of Recovery IE */
59
0
    gtph_resp->length = htobe16(length); /* to be overwriten */
60
0
    gtph_resp->teid = 0;
61
0
    idx = 8;
62
63
0
    if (gtph->e || gtph->s || gtph->pn) {
64
0
        length += 4;
65
0
        if (gtph->s) {
66
            /* sequence exists */
67
0
            gtph_resp->s = 1;
68
0
            *((uint8_t *)pkb_resp->data + idx) = *((uint8_t *)pkb->data + idx);
69
0
            *((uint8_t *)pkb_resp->data + idx + 1) =
70
0
                *((uint8_t *)pkb->data + idx + 1);
71
0
        } else {
72
0
            *((uint8_t *)pkb_resp->data + idx) = 0;
73
0
            *((uint8_t *)pkb_resp->data + idx + 1) = 0;
74
0
        }
75
0
        idx += 2;
76
0
        if (gtph->pn) {
77
            /* sequence exists */
78
0
            gtph_resp->pn = 1;
79
0
            *((uint8_t *)pkb_resp->data + idx) = *((uint8_t *)pkb->data + idx);
80
0
        } else {
81
0
            *((uint8_t *)pkb_resp->data + idx) = 0;
82
0
        }
83
0
        idx++;
84
0
        *((uint8_t *)pkb_resp->data + idx) = 0; /* next-extension header */
85
0
        idx++;
86
0
    }
87
88
    /* fill Recovery IE */
89
0
    length += 2;
90
0
    *((uint8_t *)pkb_resp->data + idx) = 14; idx++; /* type */
91
0
    *((uint8_t *)pkb_resp->data + idx) = 0; idx++; /* restart counter */
92
93
0
    gtph_resp->length = htobe16(length);
94
0
    ogs_pkbuf_trim(pkb_resp, idx); /* buffer length */
95
96
0
    return pkb_resp;
97
0
}
98
99
void ogs_gtp1_send_error_message(
100
        ogs_gtp_xact_t *xact, uint32_t teid, uint8_t type, uint8_t cause_value)
101
0
{
102
0
    int rv;
103
0
    ogs_gtp1_message_t errmsg;
104
0
    ogs_gtp1_tlv_cause_t *tlv = NULL;
105
0
    ogs_pkbuf_t *pkbuf = NULL;
106
107
0
    memset(&errmsg, 0, sizeof(ogs_gtp1_message_t));
108
0
    errmsg.h.type = type;
109
0
    errmsg.h.teid = teid;
110
111
0
    switch (type) {
112
0
    case OGS_GTP1_CREATE_PDP_CONTEXT_RESPONSE_TYPE:
113
0
        tlv = &errmsg.create_pdp_context_response.cause;
114
0
        break;
115
0
    case OGS_GTP1_UPDATE_PDP_CONTEXT_RESPONSE_TYPE:
116
0
        tlv = &errmsg.update_pdp_context_response.cause;
117
0
        break;
118
0
    case OGS_GTP1_DELETE_PDP_CONTEXT_RESPONSE_TYPE:
119
0
        tlv = &errmsg.delete_pdp_context_response.cause;
120
0
        break;
121
0
    default:
122
0
        ogs_assert_if_reached();
123
0
        return;
124
0
    }
125
126
0
    ogs_assert(tlv);
127
128
0
    tlv->presence = 1;
129
0
    tlv->u8 = cause_value;
130
131
0
    pkbuf = ogs_gtp1_build_msg(&errmsg);
132
0
    if (!pkbuf) {
133
0
        ogs_error("ogs_gtp1_build_msg() failed");
134
0
        return;
135
0
    }
136
137
0
    rv = ogs_gtp1_xact_update_tx(xact, &errmsg.h, pkbuf);
138
0
    if (rv != OGS_OK) {
139
0
        ogs_error("ogs_gtp1_xact_update_tx() failed");
140
0
        return;
141
0
    }
142
143
0
    rv = ogs_gtp_xact_commit(xact);
144
0
    ogs_expect(rv == OGS_OK);
145
0
}
146
147
void ogs_gtp1_send_echo_request(ogs_gtp_node_t *gnode)
148
0
{
149
0
    int rv;
150
0
    ogs_pkbuf_t *pkbuf = NULL;
151
0
    ogs_gtp1_header_t h;
152
0
    ogs_gtp_xact_t *xact = NULL;
153
154
0
    ogs_assert(gnode);
155
156
0
    ogs_debug("[GTP] Sending Echo Request");
157
158
0
    memset(&h, 0, sizeof(ogs_gtp1_header_t));
159
0
    h.type = OGS_GTP1_ECHO_REQUEST_TYPE;
160
0
    h.teid = 0;
161
162
0
    pkbuf = ogs_gtp1_build_echo_request(h.type);
163
0
    if (!pkbuf) {
164
0
        ogs_error("ogs_gtp1_build_echo_request() failed");
165
0
        return;
166
0
    }
167
168
0
    xact = ogs_gtp1_xact_local_create(gnode, &h, pkbuf, NULL, NULL);
169
170
0
    rv = ogs_gtp_xact_commit(xact);
171
0
    ogs_expect(rv == OGS_OK);
172
0
}
173
174
void ogs_gtp1_send_echo_response(ogs_gtp_xact_t *xact, uint8_t recovery)
175
0
{
176
0
    int rv;
177
0
    ogs_pkbuf_t *pkbuf = NULL;
178
0
    ogs_gtp1_header_t h;
179
180
0
    ogs_assert(xact);
181
182
0
    ogs_debug("[GTP] Sending Echo Response");
183
184
0
    memset(&h, 0, sizeof(ogs_gtp1_header_t));
185
0
    h.type = OGS_GTP1_ECHO_RESPONSE_TYPE;
186
0
    h.teid = 0;
187
188
0
    pkbuf = ogs_gtp1_build_echo_response(h.type, recovery);
189
0
    if (!pkbuf) {
190
0
        ogs_error("ogs_gtp1_build_echo_response() failed");
191
0
        return;
192
0
    }
193
194
0
    rv = ogs_gtp1_xact_update_tx(xact, &h, pkbuf);
195
0
    if (rv != OGS_OK) {
196
0
        ogs_error("ogs_gtp1_xact_update_tx() failed");
197
0
        return;
198
0
    }
199
200
0
    rv = ogs_gtp_xact_commit(xact);
201
0
    ogs_expect(rv == OGS_OK);
202
0
}