/src/open5gs/lib/gtp/v2/path.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2019-2024 by Sukchan Lee <acetcom@gmail.com> |
3 | | * |
4 | | * This file is part of Open5GS. |
5 | | * |
6 | | * This program is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Affero General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #include "ogs-gtp.h" |
21 | | |
22 | | ogs_pkbuf_t *ogs_gtp2_handle_echo_req(ogs_pkbuf_t *pkb) |
23 | 0 | { |
24 | 0 | ogs_gtp2_header_t *gtph = NULL; |
25 | 0 | ogs_pkbuf_t *pkb_resp = NULL; |
26 | 0 | ogs_gtp2_header_t *gtph_resp = NULL; |
27 | 0 | uint16_t length; |
28 | 0 | int idx; |
29 | |
|
30 | 0 | ogs_assert(pkb); |
31 | | |
32 | 0 | gtph = (ogs_gtp2_header_t *)pkb->data; |
33 | | /* Check GTP version. Now only support GTPv1(version = 1) */ |
34 | 0 | if ((gtph->flags >> 5) != 1) { |
35 | 0 | return NULL; |
36 | 0 | } |
37 | | |
38 | 0 | if (gtph->type != OGS_GTPU_MSGTYPE_ECHO_REQ) { |
39 | 0 | return NULL; |
40 | 0 | } |
41 | | |
42 | | |
43 | 0 | pkb_resp = ogs_pkbuf_alloc(NULL, |
44 | 0 | 100 /* enough for ECHO_RSP; use smaller buffer */); |
45 | 0 | if (!pkb_resp) { |
46 | 0 | ogs_error("ogs_pkbuf_alloc() failed"); |
47 | 0 | return NULL; |
48 | 0 | } |
49 | 0 | ogs_pkbuf_put(pkb_resp, 100); |
50 | 0 | gtph_resp = (ogs_gtp2_header_t *)pkb_resp->data; |
51 | | |
52 | | /* reply back immediately */ |
53 | 0 | gtph_resp->flags = (1 << 5); /* set version */ |
54 | 0 | gtph_resp->flags |= (1 << 4); /* set PT */ |
55 | 0 | gtph_resp->type = OGS_GTPU_MSGTYPE_ECHO_RSP; |
56 | 0 | length = 0; /* length of Recovery IE */ |
57 | 0 | gtph_resp->length = htobe16(length); /* to be overwriten */ |
58 | 0 | gtph_resp->teid = 0; |
59 | 0 | idx = 8; |
60 | |
|
61 | 0 | if (gtph->flags & (OGS_GTPU_FLAGS_PN | OGS_GTPU_FLAGS_S)) { |
62 | 0 | length += 4; |
63 | 0 | if (gtph->flags & OGS_GTPU_FLAGS_S) { |
64 | | /* sequence exists */ |
65 | 0 | gtph_resp->flags |= OGS_GTPU_FLAGS_S; |
66 | 0 | *((uint8_t *)pkb_resp->data + idx) = *((uint8_t *)pkb->data + idx); |
67 | 0 | *((uint8_t *)pkb_resp->data + idx + 1) = |
68 | 0 | *((uint8_t *)pkb->data + idx + 1); |
69 | 0 | } else { |
70 | 0 | *((uint8_t *)pkb_resp->data + idx) = 0; |
71 | 0 | *((uint8_t *)pkb_resp->data + idx + 1) = 0; |
72 | 0 | } |
73 | 0 | idx += 2; |
74 | 0 | if (gtph->flags & OGS_GTPU_FLAGS_PN) { |
75 | | /* sequence exists */ |
76 | 0 | gtph_resp->flags |= OGS_GTPU_FLAGS_PN; |
77 | 0 | *((uint8_t *)pkb_resp->data + idx) = *((uint8_t *)pkb->data + idx); |
78 | 0 | } else { |
79 | 0 | *((uint8_t *)pkb_resp->data + idx) = 0; |
80 | 0 | } |
81 | 0 | idx++; |
82 | 0 | *((uint8_t *)pkb_resp->data + idx) = 0; /* next-extension header */ |
83 | 0 | idx++; |
84 | 0 | } |
85 | | |
86 | | /* fill Recovery IE */ |
87 | 0 | length += 2; |
88 | 0 | *((uint8_t *)pkb_resp->data + idx) = 14; idx++; /* type */ |
89 | 0 | *((uint8_t *)pkb_resp->data + idx) = 0; idx++; /* restart counter */ |
90 | |
|
91 | 0 | gtph_resp->length = htobe16(length); |
92 | 0 | ogs_pkbuf_trim(pkb_resp, idx); /* buffer length */ |
93 | |
|
94 | 0 | return pkb_resp; |
95 | 0 | } |
96 | | |
97 | | void ogs_gtp2_send_error_message( |
98 | | ogs_gtp_xact_t *xact, uint32_t teid, uint8_t type, uint8_t cause_value) |
99 | 0 | { |
100 | 0 | int rv; |
101 | 0 | ogs_gtp2_message_t errmsg; |
102 | 0 | ogs_gtp2_cause_t cause; |
103 | 0 | ogs_gtp2_tlv_cause_t *tlv = NULL; |
104 | 0 | ogs_pkbuf_t *pkbuf = NULL; |
105 | |
|
106 | 0 | memset(&errmsg, 0, sizeof(ogs_gtp2_message_t)); |
107 | 0 | errmsg.h.teid = teid; |
108 | 0 | errmsg.h.type = type; |
109 | |
|
110 | 0 | switch (type) { |
111 | 0 | case OGS_GTP2_CREATE_SESSION_RESPONSE_TYPE: |
112 | 0 | tlv = &errmsg.create_session_response.cause; |
113 | 0 | break; |
114 | 0 | case OGS_GTP2_MODIFY_BEARER_RESPONSE_TYPE: |
115 | 0 | tlv = &errmsg.modify_bearer_response.cause; |
116 | 0 | break; |
117 | 0 | case OGS_GTP2_DELETE_SESSION_RESPONSE_TYPE: |
118 | 0 | tlv = &errmsg.delete_session_response.cause; |
119 | 0 | break; |
120 | 0 | case OGS_GTP2_RELEASE_ACCESS_BEARERS_RESPONSE_TYPE: |
121 | 0 | tlv = &errmsg.release_access_bearers_response.cause; |
122 | 0 | break; |
123 | 0 | case OGS_GTP2_DOWNLINK_DATA_NOTIFICATION_ACKNOWLEDGE_TYPE: |
124 | 0 | tlv = &errmsg.downlink_data_notification_acknowledge.cause; |
125 | 0 | break; |
126 | 0 | case OGS_GTP2_CREATE_BEARER_RESPONSE_TYPE: |
127 | 0 | tlv = &errmsg.create_bearer_response.cause; |
128 | 0 | break; |
129 | 0 | case OGS_GTP2_UPDATE_BEARER_RESPONSE_TYPE: |
130 | 0 | tlv = &errmsg.update_bearer_response.cause; |
131 | 0 | break; |
132 | 0 | case OGS_GTP2_DELETE_BEARER_RESPONSE_TYPE: |
133 | 0 | tlv = &errmsg.delete_bearer_response.cause; |
134 | 0 | break; |
135 | 0 | case OGS_GTP2_CREATE_INDIRECT_DATA_FORWARDING_TUNNEL_RESPONSE_TYPE: |
136 | 0 | tlv = &errmsg.create_indirect_data_forwarding_tunnel_response.cause; |
137 | 0 | break; |
138 | 0 | case OGS_GTP2_DELETE_INDIRECT_DATA_FORWARDING_TUNNEL_RESPONSE_TYPE: |
139 | 0 | tlv = &errmsg.delete_indirect_data_forwarding_tunnel_response.cause; |
140 | 0 | break; |
141 | 0 | case OGS_GTP2_MODIFY_BEARER_FAILURE_INDICATION_TYPE: |
142 | 0 | tlv = &errmsg.modify_bearer_failure_indication.cause; |
143 | 0 | break; |
144 | 0 | case OGS_GTP2_DELETE_BEARER_FAILURE_INDICATION_TYPE: |
145 | 0 | tlv = &errmsg.delete_bearer_failure_indication.cause; |
146 | 0 | break; |
147 | 0 | case OGS_GTP2_BEARER_RESOURCE_FAILURE_INDICATION_TYPE: |
148 | 0 | tlv = &errmsg.bearer_resource_failure_indication.cause; |
149 | 0 | break; |
150 | 0 | default: |
151 | 0 | ogs_fatal("Invalid message[%d]", type); |
152 | 0 | ogs_assert_if_reached(); |
153 | 0 | return; |
154 | 0 | } |
155 | | |
156 | 0 | ogs_assert(tlv); |
157 | | |
158 | 0 | memset(&cause, 0, sizeof cause); |
159 | 0 | cause.value = cause_value; |
160 | 0 | tlv->presence = 1; |
161 | 0 | tlv->len = sizeof(cause); |
162 | 0 | tlv->data = &cause; |
163 | |
|
164 | 0 | pkbuf = ogs_gtp2_build_msg(&errmsg); |
165 | 0 | if (!pkbuf) { |
166 | 0 | ogs_error("ogs_gtp2_build_msg() failed"); |
167 | 0 | return; |
168 | 0 | } |
169 | | |
170 | 0 | rv = ogs_gtp_xact_update_tx(xact, &errmsg.h, pkbuf); |
171 | 0 | if (rv != OGS_OK) { |
172 | 0 | ogs_error("ogs_gtp_xact_update_tx() failed"); |
173 | 0 | return; |
174 | 0 | } |
175 | | |
176 | 0 | rv = ogs_gtp_xact_commit(xact); |
177 | 0 | ogs_expect(rv == OGS_OK); |
178 | 0 | } |
179 | | |
180 | | void ogs_gtp2_send_echo_request( |
181 | | ogs_gtp_node_t *gnode, uint8_t recovery, uint8_t features) |
182 | 0 | { |
183 | 0 | int rv; |
184 | 0 | ogs_pkbuf_t *pkbuf = NULL; |
185 | 0 | ogs_gtp2_header_t h; |
186 | 0 | ogs_gtp_xact_t *xact = NULL; |
187 | |
|
188 | 0 | ogs_assert(gnode); |
189 | | |
190 | 0 | ogs_debug("[GTP] Sending Echo Request"); |
191 | |
|
192 | 0 | memset(&h, 0, sizeof(ogs_gtp2_header_t)); |
193 | 0 | h.type = OGS_GTP2_ECHO_REQUEST_TYPE; |
194 | 0 | h.teid = 0; |
195 | |
|
196 | 0 | pkbuf = ogs_gtp2_build_echo_request(h.type, recovery, features); |
197 | 0 | if (!pkbuf) { |
198 | 0 | ogs_error("ogs_gtp2_build_echo_request() failed"); |
199 | 0 | return; |
200 | 0 | } |
201 | | |
202 | 0 | xact = ogs_gtp_xact_local_create(gnode, &h, pkbuf, NULL, NULL); |
203 | |
|
204 | 0 | rv = ogs_gtp_xact_commit(xact); |
205 | 0 | ogs_expect(rv == OGS_OK); |
206 | 0 | } |
207 | | |
208 | | void ogs_gtp2_send_echo_response(ogs_gtp_xact_t *xact, |
209 | | uint8_t recovery, uint8_t features) |
210 | 0 | { |
211 | 0 | int rv; |
212 | 0 | ogs_pkbuf_t *pkbuf = NULL; |
213 | 0 | ogs_gtp2_header_t h; |
214 | |
|
215 | 0 | ogs_assert(xact); |
216 | | |
217 | 0 | ogs_debug("[GTP] Sending Echo Response"); |
218 | |
|
219 | 0 | memset(&h, 0, sizeof(ogs_gtp2_header_t)); |
220 | 0 | h.type = OGS_GTP2_ECHO_RESPONSE_TYPE; |
221 | 0 | h.teid = 0; |
222 | |
|
223 | 0 | pkbuf = ogs_gtp2_build_echo_response(h.type, recovery, features); |
224 | 0 | if (!pkbuf) { |
225 | 0 | ogs_error("ogs_gtp2_build_echo_response() failed"); |
226 | 0 | return; |
227 | 0 | } |
228 | | |
229 | 0 | rv = ogs_gtp_xact_update_tx(xact, &h, pkbuf); |
230 | 0 | if (rv != OGS_OK) { |
231 | 0 | ogs_error("ogs_gtp_xact_update_tx() failed"); |
232 | 0 | return; |
233 | 0 | } |
234 | | |
235 | 0 | rv = ogs_gtp_xact_commit(xact); |
236 | 0 | ogs_expect(rv == OGS_OK); |
237 | 0 | } |
238 | | |
239 | | void ogs_gtp1_send_error_indication( |
240 | | ogs_sock_t *sock, uint32_t teid, uint8_t qfi, const ogs_sockaddr_t *to) |
241 | 0 | { |
242 | 0 | ogs_pkbuf_t *pkbuf = NULL; |
243 | |
|
244 | 0 | ogs_gtp2_header_t gtp_hdesc; |
245 | 0 | ogs_gtp2_extension_header_t ext_hdesc; |
246 | 0 | int i; |
247 | |
|
248 | 0 | ogs_assert(sock); |
249 | 0 | ogs_assert(to); |
250 | | |
251 | 0 | pkbuf = ogs_gtp1_build_error_indication(teid, &sock->local_addr); |
252 | 0 | if (!pkbuf) { |
253 | 0 | ogs_error("ogs_gtp1_build_error_indication() failed"); |
254 | 0 | return; |
255 | 0 | } |
256 | | |
257 | 0 | memset(>p_hdesc, 0, sizeof(gtp_hdesc)); |
258 | 0 | memset(&ext_hdesc, 0, sizeof(ext_hdesc)); |
259 | |
|
260 | 0 | gtp_hdesc.type = OGS_GTPU_MSGTYPE_ERR_IND; |
261 | 0 | gtp_hdesc.flags = OGS_GTPU_FLAGS_S|OGS_GTPU_FLAGS_E; |
262 | |
|
263 | 0 | i = 0; |
264 | 0 | if (qfi) { |
265 | 0 | ext_hdesc.array[i].type = |
266 | 0 | OGS_GTP2_EXTENSION_HEADER_TYPE_PDU_SESSION_CONTAINER; |
267 | 0 | ext_hdesc.array[i].len = 1; |
268 | 0 | ext_hdesc.array[i].pdu_type = |
269 | 0 | OGS_GTP2_EXTENSION_HEADER_PDU_TYPE_DL_PDU_SESSION_INFORMATION; |
270 | 0 | ext_hdesc.array[i].qos_flow_identifier = qfi; |
271 | 0 | i++; |
272 | 0 | } |
273 | 0 | ext_hdesc.array[i].type = OGS_GTP2_EXTENSION_HEADER_TYPE_UDP_PORT; |
274 | 0 | ext_hdesc.array[i].len = 1; |
275 | 0 | ext_hdesc.array[i].udp_port = 0; |
276 | |
|
277 | 0 | ogs_gtp2_fill_header(>p_hdesc, &ext_hdesc, pkbuf); |
278 | |
|
279 | 0 | ogs_gtp_send_with_teid(sock, pkbuf, teid, (ogs_sockaddr_t *)to); |
280 | |
|
281 | 0 | ogs_pkbuf_free(pkbuf); |
282 | 0 | } |