blob: 756473a395de91b3357c18fc042659a0663b94de [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
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100320/* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest
321 * unused integer and the list of clients remains sorted by auc_3g_ind.
322 * Keep this function non-static to allow linking in a unit test. */
323void osmo_gsup_server_add_conn(struct llist_head *clients,
324 struct osmo_gsup_conn *conn)
325{
326 struct osmo_gsup_conn *c;
327 struct osmo_gsup_conn *prev_conn;
328
329 c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list);
330
331 /* Is the first index, 0, unused? */
332 if (!c || c->auc_3g_ind > 0) {
333 conn->auc_3g_ind = 0;
334 llist_add(&conn->list, clients);
335 return;
336 }
337
338 /* Look for a gap later on */
339 prev_conn = NULL;
340 llist_for_each_entry(c, clients, list) {
341 /* skip first item, we know it has auc_3g_ind == 0. */
342 if (!prev_conn) {
343 prev_conn = c;
344 continue;
345 }
346 if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1)
347 break;
348 prev_conn = c;
349 }
350
351 OSMO_ASSERT(prev_conn);
352
353 conn->auc_3g_ind = prev_conn->auc_3g_ind + 1;
354 llist_add(&conn->list, &prev_conn->list);
355}
356
Pau Espin Pedrol23ac5862020-10-12 16:39:38 +0200357static void update_fd_settings(int fd)
358{
359 int ret;
360 int val;
361
362 /*TODO: Set keepalive settings here. See OS#4312 */
363
364 val = 1;
365 ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
366 if (ret < 0)
367 LOGP(DLGSUP, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno));
368}
369
Harald Weltee72cf552016-04-28 07:18:49 +0200370/* a client has connected to the server socket and we have accept()ed it */
371static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
372{
373 struct osmo_gsup_conn *conn;
374 struct osmo_gsup_server *gsups =
375 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200376 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200377
Harald Weltea7617e92016-04-28 12:57:10 +0200378 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200379 OSMO_ASSERT(conn);
380
Harald Weltea7617e92016-04-28 12:57:10 +0200381 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200382 osmo_gsup_server_read_cb,
383 osmo_gsup_server_closed_cb, conn);
384 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100385 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200386
387 /* link data structure with server structure */
388 conn->server = gsups;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100389 osmo_gsup_server_add_conn(&gsups->clients, conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200390
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100391 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n",
392 conn->conn->addr, conn->conn->port, conn->auc_3g_ind);
Harald Welte173afdb2016-04-28 08:53:25 +0200393
Pau Espin Pedrol23ac5862020-10-12 16:39:38 +0200394 update_fd_settings(fd);
395
Harald Welte173afdb2016-04-28 08:53:25 +0200396 /* request the identity of the client */
397 rc = ipa_ccm_send_id_req(fd);
398 if (rc < 0)
399 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200400#if 0
401 rc = oap_init(&gsups->oap_config, &conn->oap_state);
402 if (rc != 0)
403 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200404#endif
405 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200406failed:
407 ipa_server_conn_destroy(conn->conn);
408 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200409}
410
411struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100412osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100413 osmo_gsup_read_cb_t read_cb, void *priv)
Harald Weltee72cf552016-04-28 07:18:49 +0200414{
415 struct osmo_gsup_server *gsups;
416 int rc;
417
418 gsups = talloc_zero(ctx, struct osmo_gsup_server);
419 OSMO_ASSERT(gsups);
420
421 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200422 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200423
424 gsups->link = ipa_server_link_create(gsups,
425 /* no e1inp */ NULL,
426 ip_addr, tcp_port,
427 osmo_gsup_server_accept_cb,
428 gsups);
429 if (!gsups->link)
430 goto failed;
431
432 gsups->read_cb = read_cb;
Harald Welte32acace2018-06-16 17:07:28 +0200433 gsups->priv = priv;
Harald Weltee72cf552016-04-28 07:18:49 +0200434
435 rc = ipa_server_link_open(gsups->link);
436 if (rc < 0)
437 goto failed;
438
Harald Weltee72cf552016-04-28 07:18:49 +0200439 return gsups;
440
441failed:
442 osmo_gsup_server_destroy(gsups);
443 return NULL;
444}
445
446void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
447{
448 if (gsups->link) {
449 ipa_server_link_close(gsups->link);
450 ipa_server_link_destroy(gsups->link);
451 gsups->link = NULL;
452 }
453 talloc_free(gsups);
454}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200455
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200456/* Set GSUP message's pdp_infos[0] to a wildcard APN.
457 * Use the provided apn_buf to store the produced APN data. This must remain valid until
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200458 * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too
459 * small. */
460int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup,
461 uint8_t *apn_buf, size_t apn_buf_size)
Stefan Sperling93c5b102018-04-10 19:26:14 +0200462{
463 int l;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200464
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200465 l = osmo_apn_from_str(apn_buf, apn_buf_size, "*");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200466 if (l <= 0)
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200467 return -ENOMEM;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200468
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200469 gsup->pdp_infos[0].apn_enc = apn_buf;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200470 gsup->pdp_infos[0].apn_enc_len = l;
471 gsup->pdp_infos[0].have_info = 1;
472 gsup->num_pdp_infos = 1;
473 /* FIXME: use real value: */
474 gsup->pdp_infos[0].context_id = 1;
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200475
476 return 0;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200477}
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200478
479/**
480 * Populate a gsup message structure with an Insert Subscriber Data Message.
Vadim Yanitskiyfa207022020-05-31 03:52:52 +0700481 * All required memory buffers for data pointed to by pointers in struct osmo_gsup_message
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200482 * must be allocated by the caller and should have the same lifetime as the gsup parameter.
483 *
484 * \param[out] gsup The gsup message to populate.
485 * \param[in] imsi The subscriber's IMSI.
486 * \param[in] msisdn The subscriber's MSISDN.
487 * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form.
488 * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN).
489 * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS).
490 * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN).
491 * \param[in] cn_domain The CN Domain of the subscriber connection.
492 * \returns 0 on success, and negative on error.
493 */
494int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn,
495 uint8_t *msisdn_enc, size_t msisdn_enc_size,
496 uint8_t *apn_buf, size_t apn_buf_size,
497 enum osmo_gsup_cn_domain cn_domain)
498{
499 int len;
500
501 OSMO_ASSERT(gsup);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100502 *gsup = (struct osmo_gsup_message){
503 .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST,
504 };
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200505
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200506 osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi));
507
508 if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN)
509 return -ENOSPC;
510
511 OSMO_ASSERT(msisdn_enc);
512 len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn);
513 if (len < 1) {
514 LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn);
515 return -ENOSPC;
516 }
517 gsup->msisdn_enc = msisdn_enc;
518 gsup->msisdn_enc_len = len;
519
520 #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc"
521
522 gsup->cn_domain = cn_domain;
523 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
524 OSMO_ASSERT(apn_buf_size >= APN_MAXLEN);
525 OSMO_ASSERT(apn_buf);
526 /* FIXME: PDP infos - use more fine-grained access control
527 instead of wildcard APN */
528 osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size);
529 }
530
531 return 0;
532}
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100533
534int osmo_gsup_forward_to_local_peer(struct osmo_gsup_server *server, const struct osmo_cni_peer_id *to_peer,
535 struct osmo_gsup_req *req, struct osmo_gsup_message *modified_gsup)
536{
537 int rc;
538 /* To forward to a remote entity (HLR, SMSC,...), we need to indicate the original source name in the Source
539 * Name IE to make sure the reply can be routed back. Store the sender in gsup->source_name -- the remote entity
540 * is required to return this as gsup->destination_name so that the reply gets routed to the original sender. */
541 struct osmo_gsup_message forward = *(modified_gsup? : &req->gsup);
542
543 if (req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) {
544 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
545 osmo_cni_peer_id_type_name(req->source_name.type));
546 rc = -ENOTSUP;
547 goto routing_error;
548 }
549 forward.source_name = req->source_name.ipa_name.val;
550 forward.source_name_len = req->source_name.ipa_name.len;
551
552 if (to_peer->type != OSMO_CNI_PEER_ID_IPA_NAME) {
553 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
554 osmo_cni_peer_id_type_name(to_peer->type));
555 rc = -ENOTSUP;
556 goto routing_error;
557 }
558 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_cni_peer_id_to_str(to_peer));
559 rc = osmo_gsup_enc_send_to_ipa_name(server, &to_peer->ipa_name, &forward);
560 if (rc)
561 goto routing_error;
562 osmo_gsup_req_free(req);
563 return 0;
564
565routing_error:
566 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
567 return rc;
568}