blob: f0d8101ff1534d9622906f6948f3e5ff97462146 [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>
Harald Weltee72cf552016-04-28 07:18:49 +020035
Neels Hofmeyrad868e22019-11-20 02:36:45 +010036#define LOG_GSUP_CONN(conn, level, fmt, args...) \
37 LOGP(DLGSUP, level, "GSUP peer %s: " fmt, \
38 (conn) ? osmo_ipa_name_to_str(&(conn)->peer_name) : "NULL", ##args)
39
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010040struct msgb *osmo_gsup_msgb_alloc(const char *label)
41{
Neels Hofmeyrd017d7b2019-11-25 04:00:10 +010042 struct msgb *msg = msgb_alloc_headroom(1024+512, 512, label);
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010043 OSMO_ASSERT(msg);
44 return msg;
45}
46
Harald Weltee72cf552016-04-28 07:18:49 +020047static void osmo_gsup_server_send(struct osmo_gsup_conn *conn,
48 int proto_ext, struct msgb *msg_tx)
49{
50 ipa_prepend_header_ext(msg_tx, proto_ext);
51 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
52 ipa_server_conn_send(conn->conn, msg_tx);
53}
54
55int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg)
56{
57 if (!conn) {
58 msgb_free(msg);
59 return -ENOTCONN;
60 }
61
62 osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg);
63
64 return 0;
65}
66
Neels Hofmeyrad868e22019-11-20 02:36:45 +010067static void gsup_server_send_req_response(struct osmo_gsup_req *req, struct osmo_gsup_message *response)
68{
69 struct osmo_gsup_server *server = req->cb_data;
Neels Hofmeyraf748922019-11-29 14:19:41 +010070 struct osmo_cni_peer_id *routing;
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010071 struct osmo_gsup_conn *conn = NULL;
Neels Hofmeyrad868e22019-11-20 02:36:45 +010072 struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx");
73 int rc;
74
Neels Hofmeyraf748922019-11-29 14:19:41 +010075 if (response->message_type == OSMO_GSUP_MSGT_ROUTING_ERROR
76 && !osmo_cni_peer_id_is_empty(&req->via_proxy)) {
77 /* If a routing error occured, we need to route back via the immediate sending peer, not via the
78 * intended final recipient -- because one source of routing errors is a duplicate name for a recipient.
79 * If we resolve to req->source_name, we may send to a completely unrelated recipient. */
80 routing = &req->via_proxy;
81 } else {
82 routing = &req->source_name;
83 }
84 switch (routing->type) {
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010085 case OSMO_CNI_PEER_ID_IPA_NAME:
Neels Hofmeyraf748922019-11-29 14:19:41 +010086 conn = gsup_route_find_by_ipa_name(server, &routing->ipa_name);
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010087 break;
88 default:
89 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
Neels Hofmeyraf748922019-11-29 14:19:41 +010090 osmo_cni_peer_id_type_name(routing->type));
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010091 break;
92 }
Neels Hofmeyraf748922019-11-29 14:19:41 +010093
Neels Hofmeyrad868e22019-11-20 02:36:45 +010094 if (!conn) {
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010095 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request not found, cannot respond\n");
Neels Hofmeyrad868e22019-11-20 02:36:45 +010096 msgb_free(msg);
97 return;
98 }
99
100 rc = osmo_gsup_encode(msg, response);
101 if (rc) {
102 LOG_GSUP_REQ(req, LOGL_ERROR, "Unable to encode: {%s}\n",
103 osmo_gsup_message_to_str_c(OTC_SELECT, response));
104 msgb_free(msg);
105 return;
106 }
107
108 rc = osmo_gsup_conn_send(conn, msg);
109 if (rc)
110 LOG_GSUP_CONN(conn, LOGL_ERROR, "Unable to send: %s\n", osmo_gsup_message_to_str_c(OTC_SELECT, response));
111}
112
113struct osmo_gsup_req *osmo_gsup_conn_rx(struct osmo_gsup_conn *conn, struct msgb *msg)
114{
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100115 struct osmo_gsup_req *req;
116 struct osmo_cni_peer_id cpi = {
117 .type = OSMO_CNI_PEER_ID_IPA_NAME,
118 .ipa_name = conn->peer_name,
119 };
120
121 req = osmo_gsup_req_new(conn->server, &cpi, msg, gsup_server_send_req_response, conn->server, NULL);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100122 if (!req)
123 return NULL;
124
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100125 if (!osmo_cni_peer_id_is_empty(&req->via_proxy)) {
126 switch (req->via_proxy.type) {
127 case OSMO_CNI_PEER_ID_IPA_NAME:
128 break;
129 default:
130 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
131 osmo_cni_peer_id_type_name(req->source_name.type));
132 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
133 return NULL;
134 }
135
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100136 /* The source of the GSUP message is not the immediate GSUP peer, but that peer is our proxy for that
137 * source. Add it to the routes for this conn (so we can route responses back). */
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100138 if (gsup_route_add_ipa_name(conn, &req->source_name.ipa_name)) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100139 LOG_GSUP_REQ(req, LOGL_ERROR,
140 "GSUP message received from %s via peer %s, but there already exists a"
141 " different route to this source, message is not routable\n",
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100142 osmo_cni_peer_id_to_str(&req->source_name),
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100143 osmo_ipa_name_to_str(&conn->peer_name));
144 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
145 return NULL;
146 }
147 }
148
149 return req;
150}
151
Harald Weltee72cf552016-04-28 07:18:49 +0200152static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
153 struct msgb *msg_rx)
154{
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100155#if 0
Harald Weltee72cf552016-04-28 07:18:49 +0200156 int rc;
157 struct msgb *msg_tx;
Harald Weltee72cf552016-04-28 07:18:49 +0200158 rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
159 msgb_free(msg_rx);
160 if (rc < 0)
161 return rc;
162
163 if (msg_tx)
164 osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
165#endif
166 return 0;
167}
168
Harald Weltee72cf552016-04-28 07:18:49 +0200169/* Data from a given client has arrived over the socket */
170static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
171 struct msgb *msg)
172{
173 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
174 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
175 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
176 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200177
178 msg->l2h = &hh->data[0];
179
Harald Welte173afdb2016-04-28 08:53:25 +0200180 if (hh->proto == IPAC_PROTO_IPACCESS) {
181 rc = ipa_server_conn_ccm(conn, msg);
182 if (rc < 0) {
183 /* conn is already invalid here! */
Harald Welte173afdb2016-04-28 08:53:25 +0200184 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200185 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200186 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200187 return 0;
188 }
Harald Weltee72cf552016-04-28 07:18:49 +0200189
Harald Weltef2d96da2016-04-28 11:13:15 +0200190 if (hh->proto != IPAC_PROTO_OSMO) {
191 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
192 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200193 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200194 }
Harald Weltee72cf552016-04-28 07:18:49 +0200195
Harald Weltef2d96da2016-04-28 11:13:15 +0200196 if (!he || msgb_l2len(msg) < sizeof(*he)) {
197 LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200198 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200199 }
Harald Weltee72cf552016-04-28 07:18:49 +0200200
201 msg->l2h = &he->data[0];
202
203 if (he->proto == IPAC_PROTO_EXT_GSUP) {
204 OSMO_ASSERT(clnt->server->read_cb != NULL);
205 clnt->server->read_cb(clnt, msg);
206 /* expecting read_cb() to free msg */
207 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
208 return osmo_gsup_conn_oap_handle(clnt, msg);
209 /* osmo_gsup_client_oap_handle frees msg */
Harald Weltef2d96da2016-04-28 11:13:15 +0200210 } else {
211 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
212 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200213 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200214 }
Harald Weltee72cf552016-04-28 07:18:49 +0200215
216 return 0;
217
218invalid:
219 LOGP(DLGSUP, LOGL_NOTICE,
Harald Weltef2d96da2016-04-28 11:13:15 +0200220 "GSUP received an invalid IPA message from %s:%d: %s\n",
221 conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Weltee72cf552016-04-28 07:18:49 +0200222 msgb_free(msg);
223 return -1;
224
225}
226
Harald Weltee687be52016-05-03 18:49:27 +0200227static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
228 int subsys, int level)
229{
230 unsigned int i;
231
232 for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
233 if (!TLVP_PRESENT(tlvp, i))
234 continue;
235
236 LOGP(subsys, level, "%u: %s\n", i,
237 TLVP_VAL(tlvp, i));
238 LOGP(subsys, level, "%u: %s\n", i,
239 osmo_hexdump(TLVP_VAL(tlvp, i),
240 TLVP_LEN(tlvp, i)));
241 }
242}
243
244/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
245static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
246{
247 unsigned int i;
248
249 for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
250 if (!TLVP_PRESENT(in, i)) {
251 if (TLVP_PRESENT(out, i)) {
252 talloc_free((void *) out->lv[i].val);
253 out->lv[i].val = NULL;
254 out->lv[i].len = 0;
255 }
256 continue;
257 }
258 out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
259 out->lv[i].len = in->lv[i].len;
260 }
261}
262
263int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
264 uint8_t tag)
265{
266 if (!TLVP_PRESENT(&clnt->ccm, tag))
267 return -ENODEV;
268 *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
269
270 return TLVP_LEN(&clnt->ccm, tag);
271}
272
Harald Welte173afdb2016-04-28 08:53:25 +0200273static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
274 struct msgb *msg, struct tlv_parsed *tlvp,
275 struct ipaccess_unit *unit)
276{
Harald Weltee687be52016-05-03 18:49:27 +0200277 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100278 uint8_t *addr = NULL;
Neels Hofmeyr1bd3ec42020-05-04 17:53:50 +0200279 int addr_len;
Harald Weltee687be52016-05-03 18:49:27 +0200280
Harald Welte173afdb2016-04-28 08:53:25 +0200281 LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n");
Harald Weltee687be52016-05-03 18:49:27 +0200282
283 /* FIXME: should this be parrt of ipas_server handling, not
284 * GSUP? */
285 tlvp_copy(clnt, &clnt->ccm, tlvp);
286 osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO);
287
288 addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100289 if (addr_len <= 0) {
290 LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and"
291 " cannot be routed\n",
292 conn->addr, conn->port,
293 ipa_ccm_idtag_name(IPAC_IDTAG_SERNR));
294 return -EINVAL;
295 }
Harald Weltee687be52016-05-03 18:49:27 +0200296
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100297 osmo_ipa_name_set(&clnt->peer_name, addr, addr_len);
298 gsup_route_add_ipa_name(clnt, &clnt->peer_name);
Harald Welte173afdb2016-04-28 08:53:25 +0200299 return 0;
300}
301
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100302static void osmo_gsup_conn_free(struct osmo_gsup_conn *conn)
303{
304 gsup_route_del_conn(conn);
305 llist_del(&conn->list);
306 talloc_free(conn);
307}
308
Harald Weltee72cf552016-04-28 07:18:49 +0200309static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
310{
311 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
312
313 LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
314 conn->addr, conn->port);
315
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100316 osmo_gsup_conn_free(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200317 return 0;
318}
319
Pau Espin Pedrol23ac5862020-10-12 16:39:38 +0200320static void update_fd_settings(int fd)
321{
322 int ret;
323 int val;
324
325 /*TODO: Set keepalive settings here. See OS#4312 */
326
327 val = 1;
328 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
329 if (ret < 0)
330 LOGP(DLGSUP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
331}
332
Harald Weltee72cf552016-04-28 07:18:49 +0200333/* a client has connected to the server socket and we have accept()ed it */
334static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
335{
336 struct osmo_gsup_conn *conn;
337 struct osmo_gsup_server *gsups =
338 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200339 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200340
Harald Weltea7617e92016-04-28 12:57:10 +0200341 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200342 OSMO_ASSERT(conn);
343
Harald Weltea7617e92016-04-28 12:57:10 +0200344 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200345 osmo_gsup_server_read_cb,
346 osmo_gsup_server_closed_cb, conn);
347 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100348 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200349
350 /* link data structure with server structure */
351 conn->server = gsups;
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100352 llist_add_tail(&conn->list, &gsups->clients);
Harald Weltee72cf552016-04-28 07:18:49 +0200353
Neels Hofmeyr3f9d1972019-12-12 04:04:53 +0100354 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d\n", conn->conn->addr, conn->conn->port);
Harald Welte173afdb2016-04-28 08:53:25 +0200355
Pau Espin Pedrol23ac5862020-10-12 16:39:38 +0200356 update_fd_settings(fd);
357
Harald Welte173afdb2016-04-28 08:53:25 +0200358 /* request the identity of the client */
359 rc = ipa_ccm_send_id_req(fd);
360 if (rc < 0)
361 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200362#if 0
363 rc = oap_init(&gsups->oap_config, &conn->oap_state);
364 if (rc != 0)
365 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200366#endif
367 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200368failed:
369 ipa_server_conn_destroy(conn->conn);
370 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200371}
372
373struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100374osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100375 osmo_gsup_read_cb_t read_cb, void *priv)
Harald Weltee72cf552016-04-28 07:18:49 +0200376{
377 struct osmo_gsup_server *gsups;
378 int rc;
379
380 gsups = talloc_zero(ctx, struct osmo_gsup_server);
381 OSMO_ASSERT(gsups);
382
383 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200384 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200385
386 gsups->link = ipa_server_link_create(gsups,
387 /* no e1inp */ NULL,
388 ip_addr, tcp_port,
389 osmo_gsup_server_accept_cb,
390 gsups);
391 if (!gsups->link)
392 goto failed;
393
394 gsups->read_cb = read_cb;
Harald Welte32acace2018-06-16 17:07:28 +0200395 gsups->priv = priv;
Harald Weltee72cf552016-04-28 07:18:49 +0200396
397 rc = ipa_server_link_open(gsups->link);
398 if (rc < 0)
399 goto failed;
400
Harald Weltee72cf552016-04-28 07:18:49 +0200401 return gsups;
402
403failed:
404 osmo_gsup_server_destroy(gsups);
405 return NULL;
406}
407
408void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
409{
410 if (gsups->link) {
411 ipa_server_link_close(gsups->link);
412 ipa_server_link_destroy(gsups->link);
413 gsups->link = NULL;
414 }
415 talloc_free(gsups);
416}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200417
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200418/* Set GSUP message's pdp_infos[0] to a wildcard APN.
419 * Use the provided apn_buf to store the produced APN data. This must remain valid until
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200420 * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too
421 * small. */
422int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup,
423 uint8_t *apn_buf, size_t apn_buf_size)
Stefan Sperling93c5b102018-04-10 19:26:14 +0200424{
425 int l;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200426
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200427 l = osmo_apn_from_str(apn_buf, apn_buf_size, "*");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200428 if (l <= 0)
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200429 return -ENOMEM;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200430
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200431 gsup->pdp_infos[0].apn_enc = apn_buf;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200432 gsup->pdp_infos[0].apn_enc_len = l;
433 gsup->pdp_infos[0].have_info = 1;
434 gsup->num_pdp_infos = 1;
435 /* FIXME: use real value: */
436 gsup->pdp_infos[0].context_id = 1;
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200437
438 return 0;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200439}
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200440
441/**
442 * Populate a gsup message structure with an Insert Subscriber Data Message.
Vadim Yanitskiyfa207022020-05-31 03:52:52 +0700443 * All required memory buffers for data pointed to by pointers in struct osmo_gsup_message
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200444 * must be allocated by the caller and should have the same lifetime as the gsup parameter.
445 *
446 * \param[out] gsup The gsup message to populate.
447 * \param[in] imsi The subscriber's IMSI.
448 * \param[in] msisdn The subscriber's MSISDN.
449 * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form.
450 * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN).
451 * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS).
452 * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN).
453 * \param[in] cn_domain The CN Domain of the subscriber connection.
454 * \returns 0 on success, and negative on error.
455 */
456int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn,
457 uint8_t *msisdn_enc, size_t msisdn_enc_size,
458 uint8_t *apn_buf, size_t apn_buf_size,
459 enum osmo_gsup_cn_domain cn_domain)
460{
461 int len;
462
463 OSMO_ASSERT(gsup);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100464 *gsup = (struct osmo_gsup_message){
465 .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST,
466 };
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200467
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200468 osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi));
469
470 if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN)
471 return -ENOSPC;
472
473 OSMO_ASSERT(msisdn_enc);
474 len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn);
475 if (len < 1) {
476 LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn);
477 return -ENOSPC;
478 }
479 gsup->msisdn_enc = msisdn_enc;
480 gsup->msisdn_enc_len = len;
481
482 #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc"
483
484 gsup->cn_domain = cn_domain;
485 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
486 OSMO_ASSERT(apn_buf_size >= APN_MAXLEN);
487 OSMO_ASSERT(apn_buf);
488 /* FIXME: PDP infos - use more fine-grained access control
489 instead of wildcard APN */
490 osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size);
491 }
492
493 return 0;
494}
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100495
496int osmo_gsup_forward_to_local_peer(struct osmo_gsup_server *server, const struct osmo_cni_peer_id *to_peer,
497 struct osmo_gsup_req *req, struct osmo_gsup_message *modified_gsup)
498{
499 int rc;
500 /* To forward to a remote entity (HLR, SMSC,...), we need to indicate the original source name in the Source
501 * Name IE to make sure the reply can be routed back. Store the sender in gsup->source_name -- the remote entity
502 * is required to return this as gsup->destination_name so that the reply gets routed to the original sender. */
503 struct osmo_gsup_message forward = *(modified_gsup? : &req->gsup);
504
505 if (req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) {
506 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
507 osmo_cni_peer_id_type_name(req->source_name.type));
508 rc = -ENOTSUP;
509 goto routing_error;
510 }
511 forward.source_name = req->source_name.ipa_name.val;
512 forward.source_name_len = req->source_name.ipa_name.len;
513
514 if (to_peer->type != OSMO_CNI_PEER_ID_IPA_NAME) {
515 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
516 osmo_cni_peer_id_type_name(to_peer->type));
517 rc = -ENOTSUP;
518 goto routing_error;
519 }
520 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_cni_peer_id_to_str(to_peer));
521 rc = osmo_gsup_enc_send_to_ipa_name(server, &to_peer->ipa_name, &forward);
522 if (rc)
523 goto routing_error;
524 osmo_gsup_req_free(req);
525 return 0;
526
527routing_error:
528 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
529 return rc;
530}