blob: 20ea162507d6c833e56222cb459c2ce0555c252d [file] [log] [blame]
Harald Welte936f6722016-05-03 18:51:18 +02001/* (C) 2016 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
Harald Weltee72cf552016-04-28 07:18:49 +020020#include <errno.h>
Pau Espin Pedrol23ac5862020-10-12 16:39:38 +020021#include <netinet/tcp.h>
22#include <netinet/in.h>
Harald Weltee72cf552016-04-28 07:18:49 +020023
24#include <osmocom/core/msgb.h>
25#include <osmocom/core/logging.h>
26#include <osmocom/core/linuxlist.h>
27#include <osmocom/abis/ipa.h>
28#include <osmocom/abis/ipaccess.h>
Stefan Sperlingf83432c2018-05-03 14:26:59 +020029#include <osmocom/gsm/gsm48_ie.h>
Stefan Sperling93c5b102018-04-10 19:26:14 +020030#include <osmocom/gsm/apn.h>
Neels Hofmeyrad868e22019-11-20 02:36:45 +010031#include <osmocom/gsm/gsm23003.h>
Harald Weltee72cf552016-04-28 07:18:49 +020032
Neels Hofmeyr2f758032019-11-20 00:37:07 +010033#include <osmocom/hlr/gsup_server.h>
34#include <osmocom/hlr/gsup_router.h>
Alexander Couzens37f0b3a2023-04-11 19:28:36 +020035#include <osmocom/hlr/hlr.h>
Harald Weltee72cf552016-04-28 07:18:49 +020036
Neels Hofmeyrad868e22019-11-20 02:36:45 +010037#define LOG_GSUP_CONN(conn, level, fmt, args...) \
38 LOGP(DLGSUP, level, "GSUP peer %s: " fmt, \
39 (conn) ? osmo_ipa_name_to_str(&(conn)->peer_name) : "NULL", ##args)
40
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010041struct msgb *osmo_gsup_msgb_alloc(const char *label)
42{
Neels Hofmeyrd017d7b2019-11-25 04:00:10 +010043 struct msgb *msg = msgb_alloc_headroom(1024+512, 512, label);
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010044 OSMO_ASSERT(msg);
45 return msg;
46}
47
Harald Weltee72cf552016-04-28 07:18:49 +020048static void osmo_gsup_server_send(struct osmo_gsup_conn *conn,
49 int proto_ext, struct msgb *msg_tx)
50{
51 ipa_prepend_header_ext(msg_tx, proto_ext);
52 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
53 ipa_server_conn_send(conn->conn, msg_tx);
54}
55
56int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg)
57{
58 if (!conn) {
59 msgb_free(msg);
60 return -ENOTCONN;
61 }
62
63 osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg);
64
65 return 0;
66}
67
Neels Hofmeyrad868e22019-11-20 02:36:45 +010068static void gsup_server_send_req_response(struct osmo_gsup_req *req, struct osmo_gsup_message *response)
69{
70 struct osmo_gsup_server *server = req->cb_data;
Neels Hofmeyraf748922019-11-29 14:19:41 +010071 struct osmo_cni_peer_id *routing;
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010072 struct osmo_gsup_conn *conn = NULL;
Neels Hofmeyrad868e22019-11-20 02:36:45 +010073 struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx");
74 int rc;
75
Neels Hofmeyraf748922019-11-29 14:19:41 +010076 if (response->message_type == OSMO_GSUP_MSGT_ROUTING_ERROR
77 && !osmo_cni_peer_id_is_empty(&req->via_proxy)) {
78 /* If a routing error occured, we need to route back via the immediate sending peer, not via the
79 * intended final recipient -- because one source of routing errors is a duplicate name for a recipient.
80 * If we resolve to req->source_name, we may send to a completely unrelated recipient. */
81 routing = &req->via_proxy;
82 } else {
83 routing = &req->source_name;
84 }
85 switch (routing->type) {
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010086 case OSMO_CNI_PEER_ID_IPA_NAME:
Neels Hofmeyraf748922019-11-29 14:19:41 +010087 conn = gsup_route_find_by_ipa_name(server, &routing->ipa_name);
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010088 break;
89 default:
90 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
Neels Hofmeyraf748922019-11-29 14:19:41 +010091 osmo_cni_peer_id_type_name(routing->type));
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010092 break;
93 }
Neels Hofmeyraf748922019-11-29 14:19:41 +010094
Neels Hofmeyrad868e22019-11-20 02:36:45 +010095 if (!conn) {
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010096 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request not found, cannot respond\n");
Neels Hofmeyrad868e22019-11-20 02:36:45 +010097 msgb_free(msg);
98 return;
99 }
100
101 rc = osmo_gsup_encode(msg, response);
102 if (rc) {
103 LOG_GSUP_REQ(req, LOGL_ERROR, "Unable to encode: {%s}\n",
104 osmo_gsup_message_to_str_c(OTC_SELECT, response));
105 msgb_free(msg);
106 return;
107 }
108
109 rc = osmo_gsup_conn_send(conn, msg);
110 if (rc)
111 LOG_GSUP_CONN(conn, LOGL_ERROR, "Unable to send: %s\n", osmo_gsup_message_to_str_c(OTC_SELECT, response));
112}
113
114struct osmo_gsup_req *osmo_gsup_conn_rx(struct osmo_gsup_conn *conn, struct msgb *msg)
115{
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100116 struct osmo_gsup_req *req;
117 struct osmo_cni_peer_id cpi = {
118 .type = OSMO_CNI_PEER_ID_IPA_NAME,
119 .ipa_name = conn->peer_name,
120 };
121
122 req = osmo_gsup_req_new(conn->server, &cpi, msg, gsup_server_send_req_response, conn->server, NULL);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100123 if (!req)
124 return NULL;
125
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100126 if (!osmo_cni_peer_id_is_empty(&req->via_proxy)) {
127 switch (req->via_proxy.type) {
128 case OSMO_CNI_PEER_ID_IPA_NAME:
129 break;
130 default:
131 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
132 osmo_cni_peer_id_type_name(req->source_name.type));
133 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
134 return NULL;
135 }
136
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100137 /* The source of the GSUP message is not the immediate GSUP peer, but that peer is our proxy for that
138 * source. Add it to the routes for this conn (so we can route responses back). */
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100139 if (gsup_route_add_ipa_name(conn, &req->source_name.ipa_name)) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100140 LOG_GSUP_REQ(req, LOGL_ERROR,
141 "GSUP message received from %s via peer %s, but there already exists a"
142 " different route to this source, message is not routable\n",
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100143 osmo_cni_peer_id_to_str(&req->source_name),
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100144 osmo_ipa_name_to_str(&conn->peer_name));
145 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
146 return NULL;
147 }
148 }
149
150 return req;
151}
152
Harald Weltee72cf552016-04-28 07:18:49 +0200153static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
154 struct msgb *msg_rx)
155{
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100156#if 0
Harald Weltee72cf552016-04-28 07:18:49 +0200157 int rc;
158 struct msgb *msg_tx;
Harald Weltee72cf552016-04-28 07:18:49 +0200159 rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
160 msgb_free(msg_rx);
161 if (rc < 0)
162 return rc;
163
164 if (msg_tx)
165 osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
166#endif
167 return 0;
168}
169
Harald Weltee72cf552016-04-28 07:18:49 +0200170/* Data from a given client has arrived over the socket */
171static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
172 struct msgb *msg)
173{
174 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
175 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
176 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
177 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200178
179 msg->l2h = &hh->data[0];
180
Harald Welte173afdb2016-04-28 08:53:25 +0200181 if (hh->proto == IPAC_PROTO_IPACCESS) {
182 rc = ipa_server_conn_ccm(conn, msg);
Harald Welte5341b5d2016-04-28 12:48:39 +0200183 msgb_free(msg);
Vadim Yanitskiyf8b73f02023-07-05 01:06:20 +0700184 if (rc < 0) /* conn is already invalid here! */
185 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200186 return 0;
187 }
Harald Weltee72cf552016-04-28 07:18:49 +0200188
Harald Weltef2d96da2016-04-28 11:13:15 +0200189 if (hh->proto != IPAC_PROTO_OSMO) {
190 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
191 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200192 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200193 }
Harald Weltee72cf552016-04-28 07:18:49 +0200194
Harald Weltef2d96da2016-04-28 11:13:15 +0200195 if (!he || msgb_l2len(msg) < sizeof(*he)) {
196 LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200197 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200198 }
Harald Weltee72cf552016-04-28 07:18:49 +0200199
200 msg->l2h = &he->data[0];
201
202 if (he->proto == IPAC_PROTO_EXT_GSUP) {
203 OSMO_ASSERT(clnt->server->read_cb != NULL);
204 clnt->server->read_cb(clnt, msg);
205 /* expecting read_cb() to free msg */
206 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
207 return osmo_gsup_conn_oap_handle(clnt, msg);
208 /* osmo_gsup_client_oap_handle frees msg */
Harald Weltef2d96da2016-04-28 11:13:15 +0200209 } else {
210 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
211 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200212 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200213 }
Harald Weltee72cf552016-04-28 07:18:49 +0200214
215 return 0;
216
217invalid:
218 LOGP(DLGSUP, LOGL_NOTICE,
Harald Weltef2d96da2016-04-28 11:13:15 +0200219 "GSUP received an invalid IPA message from %s:%d: %s\n",
220 conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Weltee72cf552016-04-28 07:18:49 +0200221 msgb_free(msg);
222 return -1;
223
224}
225
Harald Weltee687be52016-05-03 18:49:27 +0200226static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
227 int subsys, int level)
228{
229 unsigned int i;
230
231 for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
232 if (!TLVP_PRESENT(tlvp, i))
233 continue;
234
235 LOGP(subsys, level, "%u: %s\n", i,
236 TLVP_VAL(tlvp, i));
237 LOGP(subsys, level, "%u: %s\n", i,
238 osmo_hexdump(TLVP_VAL(tlvp, i),
239 TLVP_LEN(tlvp, i)));
240 }
241}
242
243/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
244static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
245{
246 unsigned int i;
247
248 for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
249 if (!TLVP_PRESENT(in, i)) {
250 if (TLVP_PRESENT(out, i)) {
251 talloc_free((void *) out->lv[i].val);
252 out->lv[i].val = NULL;
253 out->lv[i].len = 0;
254 }
255 continue;
256 }
257 out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
258 out->lv[i].len = in->lv[i].len;
259 }
260}
261
262int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
263 uint8_t tag)
264{
265 if (!TLVP_PRESENT(&clnt->ccm, tag))
266 return -ENODEV;
267 *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
268
269 return TLVP_LEN(&clnt->ccm, tag);
270}
271
Harald Welte173afdb2016-04-28 08:53:25 +0200272static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
273 struct msgb *msg, struct tlv_parsed *tlvp,
274 struct ipaccess_unit *unit)
275{
Harald Weltee687be52016-05-03 18:49:27 +0200276 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100277 uint8_t *addr = NULL;
Neels Hofmeyr1bd3ec42020-05-04 17:53:50 +0200278 int addr_len;
Harald Weltee687be52016-05-03 18:49:27 +0200279
Harald Welte173afdb2016-04-28 08:53:25 +0200280 LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n");
Harald Weltee687be52016-05-03 18:49:27 +0200281
282 /* FIXME: should this be parrt of ipas_server handling, not
283 * GSUP? */
284 tlvp_copy(clnt, &clnt->ccm, tlvp);
285 osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO);
286
287 addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100288 if (addr_len <= 0) {
289 LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and"
290 " cannot be routed\n",
291 conn->addr, conn->port,
292 ipa_ccm_idtag_name(IPAC_IDTAG_SERNR));
293 return -EINVAL;
294 }
Harald Weltee687be52016-05-03 18:49:27 +0200295
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100296 osmo_ipa_name_set(&clnt->peer_name, addr, addr_len);
297 gsup_route_add_ipa_name(clnt, &clnt->peer_name);
Harald Welte173afdb2016-04-28 08:53:25 +0200298 return 0;
299}
300
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100301static void osmo_gsup_conn_free(struct osmo_gsup_conn *conn)
302{
303 gsup_route_del_conn(conn);
304 llist_del(&conn->list);
305 talloc_free(conn);
306}
307
Harald Weltee72cf552016-04-28 07:18:49 +0200308static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
309{
310 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
311
312 LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
313 conn->addr, conn->port);
314
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100315 osmo_gsup_conn_free(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200316 return 0;
317}
318
Pau Espin Pedrol23ac5862020-10-12 16:39:38 +0200319static void update_fd_settings(int fd)
320{
321 int ret;
322 int val;
323
324 /*TODO: Set keepalive settings here. See OS#4312 */
325
326 val = 1;
327 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
328 if (ret < 0)
329 LOGP(DLGSUP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
330}
331
Harald Weltee72cf552016-04-28 07:18:49 +0200332/* a client has connected to the server socket and we have accept()ed it */
333static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
334{
335 struct osmo_gsup_conn *conn;
336 struct osmo_gsup_server *gsups =
337 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200338 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200339
Harald Weltea7617e92016-04-28 12:57:10 +0200340 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200341 OSMO_ASSERT(conn);
342
Harald Weltea7617e92016-04-28 12:57:10 +0200343 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200344 osmo_gsup_server_read_cb,
345 osmo_gsup_server_closed_cb, conn);
346 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100347 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200348
349 /* link data structure with server structure */
350 conn->server = gsups;
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100351 llist_add_tail(&conn->list, &gsups->clients);
Harald Weltee72cf552016-04-28 07:18:49 +0200352
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100353 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d\n", conn->conn->addr, conn->conn->port);
Harald Welte173afdb2016-04-28 08:53:25 +0200354
Pau Espin Pedrol23ac5862020-10-12 16:39:38 +0200355 update_fd_settings(fd);
356
Harald Welte173afdb2016-04-28 08:53:25 +0200357 /* request the identity of the client */
358 rc = ipa_ccm_send_id_req(fd);
359 if (rc < 0)
360 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200361#if 0
362 rc = oap_init(&gsups->oap_config, &conn->oap_state);
363 if (rc != 0)
364 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200365#endif
366 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200367failed:
368 ipa_server_conn_destroy(conn->conn);
369 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200370}
371
372struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100373osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100374 osmo_gsup_read_cb_t read_cb, void *priv)
Harald Weltee72cf552016-04-28 07:18:49 +0200375{
376 struct osmo_gsup_server *gsups;
377 int rc;
378
379 gsups = talloc_zero(ctx, struct osmo_gsup_server);
380 OSMO_ASSERT(gsups);
381
382 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200383 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200384
385 gsups->link = ipa_server_link_create(gsups,
386 /* no e1inp */ NULL,
387 ip_addr, tcp_port,
388 osmo_gsup_server_accept_cb,
389 gsups);
390 if (!gsups->link)
391 goto failed;
392
393 gsups->read_cb = read_cb;
Harald Welte32acace2018-06-16 17:07:28 +0200394 gsups->priv = priv;
Harald Weltee72cf552016-04-28 07:18:49 +0200395
396 rc = ipa_server_link_open(gsups->link);
397 if (rc < 0)
398 goto failed;
399
Harald Weltee72cf552016-04-28 07:18:49 +0200400 return gsups;
401
402failed:
403 osmo_gsup_server_destroy(gsups);
404 return NULL;
405}
406
407void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
408{
409 if (gsups->link) {
410 ipa_server_link_close(gsups->link);
411 ipa_server_link_destroy(gsups->link);
412 gsups->link = NULL;
413 }
414 talloc_free(gsups);
415}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200416
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200417/* Set GSUP message's pdp_infos[0] to a wildcard APN.
418 * Use the provided apn_buf to store the produced APN data. This must remain valid until
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200419 * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too
420 * small. */
421int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup,
422 uint8_t *apn_buf, size_t apn_buf_size)
Stefan Sperling93c5b102018-04-10 19:26:14 +0200423{
424 int l;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200425
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200426 l = osmo_apn_from_str(apn_buf, apn_buf_size, "*");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200427 if (l <= 0)
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200428 return -ENOMEM;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200429
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200430 gsup->pdp_infos[0].apn_enc = apn_buf;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200431 gsup->pdp_infos[0].apn_enc_len = l;
432 gsup->pdp_infos[0].have_info = 1;
433 gsup->num_pdp_infos = 1;
434 /* FIXME: use real value: */
435 gsup->pdp_infos[0].context_id = 1;
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200436
437 return 0;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200438}
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200439
440/**
441 * Populate a gsup message structure with an Insert Subscriber Data Message.
Vadim Yanitskiyfa207022020-05-31 03:52:52 +0700442 * All required memory buffers for data pointed to by pointers in struct osmo_gsup_message
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200443 * must be allocated by the caller and should have the same lifetime as the gsup parameter.
444 *
445 * \param[out] gsup The gsup message to populate.
446 * \param[in] imsi The subscriber's IMSI.
447 * \param[in] msisdn The subscriber's MSISDN.
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200448 * \param[in] cn_domain The CN Domain of the subscriber connection.
Alexander Couzens29898732023-04-11 17:53:33 +0200449 * \param[in] talloc_ctx To allocation memory for dynamic fields (msisdn, apn) in the gsup field
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200450 * \returns 0 on success, and negative on error.
451 */
452int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn,
Alexander Couzens29898732023-04-11 17:53:33 +0200453 enum osmo_gsup_cn_domain cn_domain,
454 void *talloc_ctx)
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200455{
456 int len;
Alexander Couzens29898732023-04-11 17:53:33 +0200457 uint8_t *msisdn_buf = talloc_size(talloc_ctx, OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200458
459 OSMO_ASSERT(gsup);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100460 *gsup = (struct osmo_gsup_message){
461 .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST,
462 };
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200463
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200464 osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi));
465
Alexander Couzens29898732023-04-11 17:53:33 +0200466 len = gsm48_encode_bcd_number(msisdn_buf, OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN, 0, msisdn);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200467 if (len < 1) {
468 LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn);
469 return -ENOSPC;
470 }
Alexander Couzens29898732023-04-11 17:53:33 +0200471 gsup->msisdn_enc = msisdn_buf;
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200472 gsup->msisdn_enc_len = len;
473
474 #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc"
475
476 gsup->cn_domain = cn_domain;
477 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
Alexander Couzens37f0b3a2023-04-11 19:28:36 +0200478 if (g_hlr->ps.pdp_profile.enabled) {
479 OSMO_ASSERT(g_hlr->ps.pdp_profile.num_pdp_infos <= ARRAY_SIZE(g_hlr->ps.pdp_profile.pdp_infos));
480 OSMO_ASSERT(g_hlr->ps.pdp_profile.num_pdp_infos <= ARRAY_SIZE(gsup->pdp_infos));
481 memcpy(gsup->pdp_infos,
482 g_hlr->ps.pdp_profile.pdp_infos,
483 sizeof(struct osmo_gsup_pdp_info) * g_hlr->ps.pdp_profile.num_pdp_infos);
484 gsup->num_pdp_infos = g_hlr->ps.pdp_profile.num_pdp_infos;
485 } else {
486 uint8_t *apn_buf = talloc_size(talloc_ctx, APN_MAXLEN);
487 osmo_gsup_configure_wildcard_apn(gsup, apn_buf, APN_MAXLEN);
488 }
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200489 }
490
491 return 0;
492}
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100493
494int osmo_gsup_forward_to_local_peer(struct osmo_gsup_server *server, const struct osmo_cni_peer_id *to_peer,
495 struct osmo_gsup_req *req, struct osmo_gsup_message *modified_gsup)
496{
497 int rc;
498 /* To forward to a remote entity (HLR, SMSC,...), we need to indicate the original source name in the Source
499 * Name IE to make sure the reply can be routed back. Store the sender in gsup->source_name -- the remote entity
500 * is required to return this as gsup->destination_name so that the reply gets routed to the original sender. */
501 struct osmo_gsup_message forward = *(modified_gsup? : &req->gsup);
502
503 if (req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) {
504 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
505 osmo_cni_peer_id_type_name(req->source_name.type));
506 rc = -ENOTSUP;
507 goto routing_error;
508 }
509 forward.source_name = req->source_name.ipa_name.val;
510 forward.source_name_len = req->source_name.ipa_name.len;
511
512 if (to_peer->type != OSMO_CNI_PEER_ID_IPA_NAME) {
513 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
514 osmo_cni_peer_id_type_name(to_peer->type));
515 rc = -ENOTSUP;
516 goto routing_error;
517 }
518 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_cni_peer_id_to_str(to_peer));
519 rc = osmo_gsup_enc_send_to_ipa_name(server, &to_peer->ipa_name, &forward);
520 if (rc)
521 goto routing_error;
522 osmo_gsup_req_free(req);
523 return 0;
524
525routing_error:
526 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
527 return rc;
528}