blob: a5d496e0cf962ce5b249e282a15dee2504e2ab25 [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>
21
22#include <osmocom/core/msgb.h>
23#include <osmocom/core/logging.h>
24#include <osmocom/core/linuxlist.h>
25#include <osmocom/abis/ipa.h>
26#include <osmocom/abis/ipaccess.h>
Stefan Sperlingf83432c2018-05-03 14:26:59 +020027#include <osmocom/gsm/gsm48_ie.h>
Stefan Sperling93c5b102018-04-10 19:26:14 +020028#include <osmocom/gsm/apn.h>
Neels Hofmeyrad868e22019-11-20 02:36:45 +010029#include <osmocom/gsm/gsm23003.h>
Harald Weltee72cf552016-04-28 07:18:49 +020030
Neels Hofmeyr2f758032019-11-20 00:37:07 +010031#include <osmocom/hlr/gsup_server.h>
32#include <osmocom/hlr/gsup_router.h>
Harald Weltee72cf552016-04-28 07:18:49 +020033
Neels Hofmeyrad868e22019-11-20 02:36:45 +010034#define LOG_GSUP_CONN(conn, level, fmt, args...) \
35 LOGP(DLGSUP, level, "GSUP peer %s: " fmt, \
36 (conn) ? osmo_ipa_name_to_str(&(conn)->peer_name) : "NULL", ##args)
37
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010038struct msgb *osmo_gsup_msgb_alloc(const char *label)
39{
Neels Hofmeyrd017d7b2019-11-25 04:00:10 +010040 struct msgb *msg = msgb_alloc_headroom(1024+512, 512, label);
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010041 OSMO_ASSERT(msg);
42 return msg;
43}
44
Harald Weltee72cf552016-04-28 07:18:49 +020045static void osmo_gsup_server_send(struct osmo_gsup_conn *conn,
46 int proto_ext, struct msgb *msg_tx)
47{
48 ipa_prepend_header_ext(msg_tx, proto_ext);
49 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
50 ipa_server_conn_send(conn->conn, msg_tx);
51}
52
53int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg)
54{
55 if (!conn) {
56 msgb_free(msg);
57 return -ENOTCONN;
58 }
59
60 osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg);
61
62 return 0;
63}
64
Neels Hofmeyrad868e22019-11-20 02:36:45 +010065static void gsup_server_send_req_response(struct osmo_gsup_req *req, struct osmo_gsup_message *response)
66{
67 struct osmo_gsup_server *server = req->cb_data;
Neels Hofmeyraf748922019-11-29 14:19:41 +010068 struct osmo_cni_peer_id *routing;
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010069 struct osmo_gsup_conn *conn = NULL;
Neels Hofmeyrad868e22019-11-20 02:36:45 +010070 struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx");
71 int rc;
72
Neels Hofmeyraf748922019-11-29 14:19:41 +010073 if (response->message_type == OSMO_GSUP_MSGT_ROUTING_ERROR
74 && !osmo_cni_peer_id_is_empty(&req->via_proxy)) {
75 /* If a routing error occured, we need to route back via the immediate sending peer, not via the
76 * intended final recipient -- because one source of routing errors is a duplicate name for a recipient.
77 * If we resolve to req->source_name, we may send to a completely unrelated recipient. */
78 routing = &req->via_proxy;
79 } else {
80 routing = &req->source_name;
81 }
82 switch (routing->type) {
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010083 case OSMO_CNI_PEER_ID_IPA_NAME:
Neels Hofmeyraf748922019-11-29 14:19:41 +010084 conn = gsup_route_find_by_ipa_name(server, &routing->ipa_name);
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010085 break;
86 default:
87 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
Neels Hofmeyraf748922019-11-29 14:19:41 +010088 osmo_cni_peer_id_type_name(routing->type));
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010089 break;
90 }
Neels Hofmeyraf748922019-11-29 14:19:41 +010091
Neels Hofmeyrad868e22019-11-20 02:36:45 +010092 if (!conn) {
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010093 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request not found, cannot respond\n");
Neels Hofmeyrad868e22019-11-20 02:36:45 +010094 msgb_free(msg);
95 return;
96 }
97
98 rc = osmo_gsup_encode(msg, response);
99 if (rc) {
100 LOG_GSUP_REQ(req, LOGL_ERROR, "Unable to encode: {%s}\n",
101 osmo_gsup_message_to_str_c(OTC_SELECT, response));
102 msgb_free(msg);
103 return;
104 }
105
106 rc = osmo_gsup_conn_send(conn, msg);
107 if (rc)
108 LOG_GSUP_CONN(conn, LOGL_ERROR, "Unable to send: %s\n", osmo_gsup_message_to_str_c(OTC_SELECT, response));
109}
110
111struct osmo_gsup_req *osmo_gsup_conn_rx(struct osmo_gsup_conn *conn, struct msgb *msg)
112{
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100113 struct osmo_gsup_req *req;
114 struct osmo_cni_peer_id cpi = {
115 .type = OSMO_CNI_PEER_ID_IPA_NAME,
116 .ipa_name = conn->peer_name,
117 };
118
119 req = osmo_gsup_req_new(conn->server, &cpi, msg, gsup_server_send_req_response, conn->server, NULL);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100120 if (!req)
121 return NULL;
122
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100123 if (!osmo_cni_peer_id_is_empty(&req->via_proxy)) {
124 switch (req->via_proxy.type) {
125 case OSMO_CNI_PEER_ID_IPA_NAME:
126 break;
127 default:
128 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
129 osmo_cni_peer_id_type_name(req->source_name.type));
130 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
131 return NULL;
132 }
133
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100134 /* The source of the GSUP message is not the immediate GSUP peer, but that peer is our proxy for that
135 * source. Add it to the routes for this conn (so we can route responses back). */
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100136 if (gsup_route_add_ipa_name(conn, &req->source_name.ipa_name)) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100137 LOG_GSUP_REQ(req, LOGL_ERROR,
138 "GSUP message received from %s via peer %s, but there already exists a"
139 " different route to this source, message is not routable\n",
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100140 osmo_cni_peer_id_to_str(&req->source_name),
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100141 osmo_ipa_name_to_str(&conn->peer_name));
142 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
143 return NULL;
144 }
145 }
146
147 return req;
148}
149
Harald Weltee72cf552016-04-28 07:18:49 +0200150static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
151 struct msgb *msg_rx)
152{
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100153#if 0
Harald Weltee72cf552016-04-28 07:18:49 +0200154 int rc;
155 struct msgb *msg_tx;
Harald Weltee72cf552016-04-28 07:18:49 +0200156 rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
157 msgb_free(msg_rx);
158 if (rc < 0)
159 return rc;
160
161 if (msg_tx)
162 osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
163#endif
164 return 0;
165}
166
Harald Weltee72cf552016-04-28 07:18:49 +0200167/* Data from a given client has arrived over the socket */
168static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
169 struct msgb *msg)
170{
171 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
172 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
173 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
174 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200175
176 msg->l2h = &hh->data[0];
177
Harald Welte173afdb2016-04-28 08:53:25 +0200178 if (hh->proto == IPAC_PROTO_IPACCESS) {
179 rc = ipa_server_conn_ccm(conn, msg);
180 if (rc < 0) {
181 /* conn is already invalid here! */
Harald Welte173afdb2016-04-28 08:53:25 +0200182 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200183 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200184 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200185 return 0;
186 }
Harald Weltee72cf552016-04-28 07:18:49 +0200187
Harald Weltef2d96da2016-04-28 11:13:15 +0200188 if (hh->proto != IPAC_PROTO_OSMO) {
189 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
190 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200191 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200192 }
Harald Weltee72cf552016-04-28 07:18:49 +0200193
Harald Weltef2d96da2016-04-28 11:13:15 +0200194 if (!he || msgb_l2len(msg) < sizeof(*he)) {
195 LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200196 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200197 }
Harald Weltee72cf552016-04-28 07:18:49 +0200198
199 msg->l2h = &he->data[0];
200
201 if (he->proto == IPAC_PROTO_EXT_GSUP) {
202 OSMO_ASSERT(clnt->server->read_cb != NULL);
203 clnt->server->read_cb(clnt, msg);
204 /* expecting read_cb() to free msg */
205 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
206 return osmo_gsup_conn_oap_handle(clnt, msg);
207 /* osmo_gsup_client_oap_handle frees msg */
Harald Weltef2d96da2016-04-28 11:13:15 +0200208 } else {
209 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
210 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200211 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200212 }
Harald Weltee72cf552016-04-28 07:18:49 +0200213
214 return 0;
215
216invalid:
217 LOGP(DLGSUP, LOGL_NOTICE,
Harald Weltef2d96da2016-04-28 11:13:15 +0200218 "GSUP received an invalid IPA message from %s:%d: %s\n",
219 conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Weltee72cf552016-04-28 07:18:49 +0200220 msgb_free(msg);
221 return -1;
222
223}
224
Harald Weltee687be52016-05-03 18:49:27 +0200225static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
226 int subsys, int level)
227{
228 unsigned int i;
229
230 for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
231 if (!TLVP_PRESENT(tlvp, i))
232 continue;
233
234 LOGP(subsys, level, "%u: %s\n", i,
235 TLVP_VAL(tlvp, i));
236 LOGP(subsys, level, "%u: %s\n", i,
237 osmo_hexdump(TLVP_VAL(tlvp, i),
238 TLVP_LEN(tlvp, i)));
239 }
240}
241
242/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
243static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
244{
245 unsigned int i;
246
247 for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
248 if (!TLVP_PRESENT(in, i)) {
249 if (TLVP_PRESENT(out, i)) {
250 talloc_free((void *) out->lv[i].val);
251 out->lv[i].val = NULL;
252 out->lv[i].len = 0;
253 }
254 continue;
255 }
256 out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
257 out->lv[i].len = in->lv[i].len;
258 }
259}
260
261int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
262 uint8_t tag)
263{
264 if (!TLVP_PRESENT(&clnt->ccm, tag))
265 return -ENODEV;
266 *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
267
268 return TLVP_LEN(&clnt->ccm, tag);
269}
270
Harald Welte173afdb2016-04-28 08:53:25 +0200271static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
272 struct msgb *msg, struct tlv_parsed *tlvp,
273 struct ipaccess_unit *unit)
274{
Harald Weltee687be52016-05-03 18:49:27 +0200275 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100276 uint8_t *addr = NULL;
Harald Weltee687be52016-05-03 18:49:27 +0200277 size_t addr_len;
278
Harald Welte173afdb2016-04-28 08:53:25 +0200279 LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n");
Harald Weltee687be52016-05-03 18:49:27 +0200280
281 /* FIXME: should this be parrt of ipas_server handling, not
282 * GSUP? */
283 tlvp_copy(clnt, &clnt->ccm, tlvp);
284 osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO);
285
286 addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100287 if (addr_len <= 0) {
288 LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and"
289 " cannot be routed\n",
290 conn->addr, conn->port,
291 ipa_ccm_idtag_name(IPAC_IDTAG_SERNR));
292 return -EINVAL;
293 }
Harald Weltee687be52016-05-03 18:49:27 +0200294
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100295 osmo_ipa_name_set(&clnt->peer_name, addr, addr_len);
296 gsup_route_add_ipa_name(clnt, &clnt->peer_name);
Harald Welte173afdb2016-04-28 08:53:25 +0200297 return 0;
298}
299
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100300static void osmo_gsup_conn_free(struct osmo_gsup_conn *conn)
301{
302 gsup_route_del_conn(conn);
303 llist_del(&conn->list);
304 talloc_free(conn);
305}
306
Harald Weltee72cf552016-04-28 07:18:49 +0200307static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
308{
309 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
310
311 LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
312 conn->addr, conn->port);
313
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100314 osmo_gsup_conn_free(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200315 return 0;
316}
317
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100318/* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest
319 * unused integer and the list of clients remains sorted by auc_3g_ind.
320 * Keep this function non-static to allow linking in a unit test. */
321void osmo_gsup_server_add_conn(struct llist_head *clients,
322 struct osmo_gsup_conn *conn)
323{
324 struct osmo_gsup_conn *c;
325 struct osmo_gsup_conn *prev_conn;
326
327 c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list);
328
329 /* Is the first index, 0, unused? */
330 if (!c || c->auc_3g_ind > 0) {
331 conn->auc_3g_ind = 0;
332 llist_add(&conn->list, clients);
333 return;
334 }
335
336 /* Look for a gap later on */
337 prev_conn = NULL;
338 llist_for_each_entry(c, clients, list) {
339 /* skip first item, we know it has auc_3g_ind == 0. */
340 if (!prev_conn) {
341 prev_conn = c;
342 continue;
343 }
344 if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1)
345 break;
346 prev_conn = c;
347 }
348
349 OSMO_ASSERT(prev_conn);
350
351 conn->auc_3g_ind = prev_conn->auc_3g_ind + 1;
352 llist_add(&conn->list, &prev_conn->list);
353}
354
Harald Weltee72cf552016-04-28 07:18:49 +0200355/* a client has connected to the server socket and we have accept()ed it */
356static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
357{
358 struct osmo_gsup_conn *conn;
359 struct osmo_gsup_server *gsups =
360 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200361 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200362
Harald Weltea7617e92016-04-28 12:57:10 +0200363 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200364 OSMO_ASSERT(conn);
365
Harald Weltea7617e92016-04-28 12:57:10 +0200366 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200367 osmo_gsup_server_read_cb,
368 osmo_gsup_server_closed_cb, conn);
369 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100370 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200371
372 /* link data structure with server structure */
373 conn->server = gsups;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100374 osmo_gsup_server_add_conn(&gsups->clients, conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200375
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100376 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n",
377 conn->conn->addr, conn->conn->port, conn->auc_3g_ind);
Harald Welte173afdb2016-04-28 08:53:25 +0200378
379 /* request the identity of the client */
380 rc = ipa_ccm_send_id_req(fd);
381 if (rc < 0)
382 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200383#if 0
384 rc = oap_init(&gsups->oap_config, &conn->oap_state);
385 if (rc != 0)
386 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200387#endif
388 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200389failed:
390 ipa_server_conn_destroy(conn->conn);
391 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200392}
393
394struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100395osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100396 osmo_gsup_read_cb_t read_cb, void *priv)
Harald Weltee72cf552016-04-28 07:18:49 +0200397{
398 struct osmo_gsup_server *gsups;
399 int rc;
400
401 gsups = talloc_zero(ctx, struct osmo_gsup_server);
402 OSMO_ASSERT(gsups);
403
404 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200405 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200406
407 gsups->link = ipa_server_link_create(gsups,
408 /* no e1inp */ NULL,
409 ip_addr, tcp_port,
410 osmo_gsup_server_accept_cb,
411 gsups);
412 if (!gsups->link)
413 goto failed;
414
415 gsups->read_cb = read_cb;
Harald Welte32acace2018-06-16 17:07:28 +0200416 gsups->priv = priv;
Harald Weltee72cf552016-04-28 07:18:49 +0200417
418 rc = ipa_server_link_open(gsups->link);
419 if (rc < 0)
420 goto failed;
421
Harald Weltee72cf552016-04-28 07:18:49 +0200422 return gsups;
423
424failed:
425 osmo_gsup_server_destroy(gsups);
426 return NULL;
427}
428
429void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
430{
431 if (gsups->link) {
432 ipa_server_link_close(gsups->link);
433 ipa_server_link_destroy(gsups->link);
434 gsups->link = NULL;
435 }
436 talloc_free(gsups);
437}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200438
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200439/* Set GSUP message's pdp_infos[0] to a wildcard APN.
440 * Use the provided apn_buf to store the produced APN data. This must remain valid until
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200441 * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too
442 * small. */
443int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup,
444 uint8_t *apn_buf, size_t apn_buf_size)
Stefan Sperling93c5b102018-04-10 19:26:14 +0200445{
446 int l;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200447
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200448 l = osmo_apn_from_str(apn_buf, apn_buf_size, "*");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200449 if (l <= 0)
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200450 return -ENOMEM;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200451
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200452 gsup->pdp_infos[0].apn_enc = apn_buf;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200453 gsup->pdp_infos[0].apn_enc_len = l;
454 gsup->pdp_infos[0].have_info = 1;
455 gsup->num_pdp_infos = 1;
456 /* FIXME: use real value: */
457 gsup->pdp_infos[0].context_id = 1;
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200458
459 return 0;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200460}
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200461
462/**
463 * Populate a gsup message structure with an Insert Subscriber Data Message.
464 * All required memory buffers for data pointed to by pointers in struct omso_gsup_message
465 * must be allocated by the caller and should have the same lifetime as the gsup parameter.
466 *
467 * \param[out] gsup The gsup message to populate.
468 * \param[in] imsi The subscriber's IMSI.
469 * \param[in] msisdn The subscriber's MSISDN.
470 * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form.
471 * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN).
472 * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS).
473 * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN).
474 * \param[in] cn_domain The CN Domain of the subscriber connection.
475 * \returns 0 on success, and negative on error.
476 */
477int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn,
478 uint8_t *msisdn_enc, size_t msisdn_enc_size,
479 uint8_t *apn_buf, size_t apn_buf_size,
480 enum osmo_gsup_cn_domain cn_domain)
481{
482 int len;
483
484 OSMO_ASSERT(gsup);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100485 *gsup = (struct osmo_gsup_message){
486 .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST,
487 };
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200488
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200489 osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi));
490
491 if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN)
492 return -ENOSPC;
493
494 OSMO_ASSERT(msisdn_enc);
495 len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn);
496 if (len < 1) {
497 LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn);
498 return -ENOSPC;
499 }
500 gsup->msisdn_enc = msisdn_enc;
501 gsup->msisdn_enc_len = len;
502
503 #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc"
504
505 gsup->cn_domain = cn_domain;
506 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
507 OSMO_ASSERT(apn_buf_size >= APN_MAXLEN);
508 OSMO_ASSERT(apn_buf);
509 /* FIXME: PDP infos - use more fine-grained access control
510 instead of wildcard APN */
511 osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size);
512 }
513
514 return 0;
515}
Neels Hofmeyr76328bd2019-11-20 03:35:37 +0100516
517int osmo_gsup_forward_to_local_peer(struct osmo_gsup_server *server, const struct osmo_cni_peer_id *to_peer,
518 struct osmo_gsup_req *req, struct osmo_gsup_message *modified_gsup)
519{
520 int rc;
521 /* To forward to a remote entity (HLR, SMSC,...), we need to indicate the original source name in the Source
522 * Name IE to make sure the reply can be routed back. Store the sender in gsup->source_name -- the remote entity
523 * is required to return this as gsup->destination_name so that the reply gets routed to the original sender. */
524 struct osmo_gsup_message forward = *(modified_gsup? : &req->gsup);
525
526 if (req->source_name.type != OSMO_CNI_PEER_ID_IPA_NAME) {
527 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
528 osmo_cni_peer_id_type_name(req->source_name.type));
529 rc = -ENOTSUP;
530 goto routing_error;
531 }
532 forward.source_name = req->source_name.ipa_name.val;
533 forward.source_name_len = req->source_name.ipa_name.len;
534
535 if (to_peer->type != OSMO_CNI_PEER_ID_IPA_NAME) {
536 osmo_gsup_req_respond_err(req, GMM_CAUSE_NET_FAIL, "Unsupported GSUP peer id type: %s",
537 osmo_cni_peer_id_type_name(to_peer->type));
538 rc = -ENOTSUP;
539 goto routing_error;
540 }
541 LOG_GSUP_REQ(req, LOGL_INFO, "Forwarding to %s\n", osmo_cni_peer_id_to_str(to_peer));
542 rc = osmo_gsup_enc_send_to_ipa_name(server, &to_peer->ipa_name, &forward);
543 if (rc)
544 goto routing_error;
545 osmo_gsup_req_free(req);
546 return 0;
547
548routing_error:
549 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
550 return rc;
551}