blob: 61b9257ce74dd41b2a22c487e6fb6551cb1b1aa4 [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 Hofmeyrc79bcde2019-12-04 01:04:32 +010068 struct osmo_gsup_conn *conn = NULL;
Neels Hofmeyrad868e22019-11-20 02:36:45 +010069 struct msgb *msg = osmo_gsup_msgb_alloc("GSUP Tx");
70 int rc;
71
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010072 switch (req->source_name.type) {
73 case OSMO_CNI_PEER_ID_IPA_NAME:
74 conn = gsup_route_find_by_ipa_name(server, &req->source_name.ipa_name);
75 break;
76 default:
77 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
78 osmo_cni_peer_id_type_name(req->source_name.type));
79 break;
80 }
Neels Hofmeyrad868e22019-11-20 02:36:45 +010081 if (!conn) {
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +010082 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP client that sent this request not found, cannot respond\n");
Neels Hofmeyrad868e22019-11-20 02:36:45 +010083 msgb_free(msg);
84 return;
85 }
86
87 rc = osmo_gsup_encode(msg, response);
88 if (rc) {
89 LOG_GSUP_REQ(req, LOGL_ERROR, "Unable to encode: {%s}\n",
90 osmo_gsup_message_to_str_c(OTC_SELECT, response));
91 msgb_free(msg);
92 return;
93 }
94
95 rc = osmo_gsup_conn_send(conn, msg);
96 if (rc)
97 LOG_GSUP_CONN(conn, LOGL_ERROR, "Unable to send: %s\n", osmo_gsup_message_to_str_c(OTC_SELECT, response));
98}
99
100struct osmo_gsup_req *osmo_gsup_conn_rx(struct osmo_gsup_conn *conn, struct msgb *msg)
101{
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100102 struct osmo_gsup_req *req;
103 struct osmo_cni_peer_id cpi = {
104 .type = OSMO_CNI_PEER_ID_IPA_NAME,
105 .ipa_name = conn->peer_name,
106 };
107
108 req = osmo_gsup_req_new(conn->server, &cpi, msg, gsup_server_send_req_response, conn->server, NULL);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100109 if (!req)
110 return NULL;
111
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100112 if (!osmo_cni_peer_id_is_empty(&req->via_proxy)) {
113 switch (req->via_proxy.type) {
114 case OSMO_CNI_PEER_ID_IPA_NAME:
115 break;
116 default:
117 LOG_GSUP_REQ(req, LOGL_ERROR, "GSUP peer id kind not supported: %s\n",
118 osmo_cni_peer_id_type_name(req->source_name.type));
119 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
120 return NULL;
121 }
122
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100123 /* The source of the GSUP message is not the immediate GSUP peer, but that peer is our proxy for that
124 * source. Add it to the routes for this conn (so we can route responses back). */
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100125 if (gsup_route_add_ipa_name(conn, &req->source_name.ipa_name)) {
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100126 LOG_GSUP_REQ(req, LOGL_ERROR,
127 "GSUP message received from %s via peer %s, but there already exists a"
128 " different route to this source, message is not routable\n",
Neels Hofmeyrc79bcde2019-12-04 01:04:32 +0100129 osmo_cni_peer_id_to_str(&req->source_name),
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100130 osmo_ipa_name_to_str(&conn->peer_name));
131 osmo_gsup_req_respond_msgt(req, OSMO_GSUP_MSGT_ROUTING_ERROR, true);
132 return NULL;
133 }
134 }
135
136 return req;
137}
138
Harald Weltee72cf552016-04-28 07:18:49 +0200139static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
140 struct msgb *msg_rx)
141{
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100142#if 0
Harald Weltee72cf552016-04-28 07:18:49 +0200143 int rc;
144 struct msgb *msg_tx;
Harald Weltee72cf552016-04-28 07:18:49 +0200145 rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
146 msgb_free(msg_rx);
147 if (rc < 0)
148 return rc;
149
150 if (msg_tx)
151 osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
152#endif
153 return 0;
154}
155
Harald Weltee72cf552016-04-28 07:18:49 +0200156/* Data from a given client has arrived over the socket */
157static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
158 struct msgb *msg)
159{
160 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
161 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
162 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
163 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200164
165 msg->l2h = &hh->data[0];
166
Harald Welte173afdb2016-04-28 08:53:25 +0200167 if (hh->proto == IPAC_PROTO_IPACCESS) {
168 rc = ipa_server_conn_ccm(conn, msg);
169 if (rc < 0) {
170 /* conn is already invalid here! */
Harald Welte173afdb2016-04-28 08:53:25 +0200171 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200172 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200173 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200174 return 0;
175 }
Harald Weltee72cf552016-04-28 07:18:49 +0200176
Harald Weltef2d96da2016-04-28 11:13:15 +0200177 if (hh->proto != IPAC_PROTO_OSMO) {
178 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
179 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200180 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200181 }
Harald Weltee72cf552016-04-28 07:18:49 +0200182
Harald Weltef2d96da2016-04-28 11:13:15 +0200183 if (!he || msgb_l2len(msg) < sizeof(*he)) {
184 LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200185 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200186 }
Harald Weltee72cf552016-04-28 07:18:49 +0200187
188 msg->l2h = &he->data[0];
189
190 if (he->proto == IPAC_PROTO_EXT_GSUP) {
191 OSMO_ASSERT(clnt->server->read_cb != NULL);
192 clnt->server->read_cb(clnt, msg);
193 /* expecting read_cb() to free msg */
194 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
195 return osmo_gsup_conn_oap_handle(clnt, msg);
196 /* osmo_gsup_client_oap_handle frees msg */
Harald Weltef2d96da2016-04-28 11:13:15 +0200197 } else {
198 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
199 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200200 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200201 }
Harald Weltee72cf552016-04-28 07:18:49 +0200202
203 return 0;
204
205invalid:
206 LOGP(DLGSUP, LOGL_NOTICE,
Harald Weltef2d96da2016-04-28 11:13:15 +0200207 "GSUP received an invalid IPA message from %s:%d: %s\n",
208 conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Weltee72cf552016-04-28 07:18:49 +0200209 msgb_free(msg);
210 return -1;
211
212}
213
Harald Weltee687be52016-05-03 18:49:27 +0200214static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
215 int subsys, int level)
216{
217 unsigned int i;
218
219 for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
220 if (!TLVP_PRESENT(tlvp, i))
221 continue;
222
223 LOGP(subsys, level, "%u: %s\n", i,
224 TLVP_VAL(tlvp, i));
225 LOGP(subsys, level, "%u: %s\n", i,
226 osmo_hexdump(TLVP_VAL(tlvp, i),
227 TLVP_LEN(tlvp, i)));
228 }
229}
230
231/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
232static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
233{
234 unsigned int i;
235
236 for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
237 if (!TLVP_PRESENT(in, i)) {
238 if (TLVP_PRESENT(out, i)) {
239 talloc_free((void *) out->lv[i].val);
240 out->lv[i].val = NULL;
241 out->lv[i].len = 0;
242 }
243 continue;
244 }
245 out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
246 out->lv[i].len = in->lv[i].len;
247 }
248}
249
250int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
251 uint8_t tag)
252{
253 if (!TLVP_PRESENT(&clnt->ccm, tag))
254 return -ENODEV;
255 *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
256
257 return TLVP_LEN(&clnt->ccm, tag);
258}
259
Harald Welte173afdb2016-04-28 08:53:25 +0200260static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
261 struct msgb *msg, struct tlv_parsed *tlvp,
262 struct ipaccess_unit *unit)
263{
Harald Weltee687be52016-05-03 18:49:27 +0200264 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100265 uint8_t *addr = NULL;
Harald Weltee687be52016-05-03 18:49:27 +0200266 size_t addr_len;
267
Harald Welte173afdb2016-04-28 08:53:25 +0200268 LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n");
Harald Weltee687be52016-05-03 18:49:27 +0200269
270 /* FIXME: should this be parrt of ipas_server handling, not
271 * GSUP? */
272 tlvp_copy(clnt, &clnt->ccm, tlvp);
273 osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO);
274
275 addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100276 if (addr_len <= 0) {
277 LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and"
278 " cannot be routed\n",
279 conn->addr, conn->port,
280 ipa_ccm_idtag_name(IPAC_IDTAG_SERNR));
281 return -EINVAL;
282 }
Harald Weltee687be52016-05-03 18:49:27 +0200283
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100284 osmo_ipa_name_set(&clnt->peer_name, addr, addr_len);
285 gsup_route_add_ipa_name(clnt, &clnt->peer_name);
Harald Welte173afdb2016-04-28 08:53:25 +0200286 return 0;
287}
288
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100289static void osmo_gsup_conn_free(struct osmo_gsup_conn *conn)
290{
291 gsup_route_del_conn(conn);
292 llist_del(&conn->list);
293 talloc_free(conn);
294}
295
Harald Weltee72cf552016-04-28 07:18:49 +0200296static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
297{
298 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
299
300 LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
301 conn->addr, conn->port);
302
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100303 osmo_gsup_conn_free(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200304 return 0;
305}
306
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100307/* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest
308 * unused integer and the list of clients remains sorted by auc_3g_ind.
309 * Keep this function non-static to allow linking in a unit test. */
310void osmo_gsup_server_add_conn(struct llist_head *clients,
311 struct osmo_gsup_conn *conn)
312{
313 struct osmo_gsup_conn *c;
314 struct osmo_gsup_conn *prev_conn;
315
316 c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list);
317
318 /* Is the first index, 0, unused? */
319 if (!c || c->auc_3g_ind > 0) {
320 conn->auc_3g_ind = 0;
321 llist_add(&conn->list, clients);
322 return;
323 }
324
325 /* Look for a gap later on */
326 prev_conn = NULL;
327 llist_for_each_entry(c, clients, list) {
328 /* skip first item, we know it has auc_3g_ind == 0. */
329 if (!prev_conn) {
330 prev_conn = c;
331 continue;
332 }
333 if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1)
334 break;
335 prev_conn = c;
336 }
337
338 OSMO_ASSERT(prev_conn);
339
340 conn->auc_3g_ind = prev_conn->auc_3g_ind + 1;
341 llist_add(&conn->list, &prev_conn->list);
342}
343
Harald Weltee72cf552016-04-28 07:18:49 +0200344/* a client has connected to the server socket and we have accept()ed it */
345static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
346{
347 struct osmo_gsup_conn *conn;
348 struct osmo_gsup_server *gsups =
349 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200350 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200351
Harald Weltea7617e92016-04-28 12:57:10 +0200352 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200353 OSMO_ASSERT(conn);
354
Harald Weltea7617e92016-04-28 12:57:10 +0200355 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200356 osmo_gsup_server_read_cb,
357 osmo_gsup_server_closed_cb, conn);
358 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100359 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200360
361 /* link data structure with server structure */
362 conn->server = gsups;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100363 osmo_gsup_server_add_conn(&gsups->clients, conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200364
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100365 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n",
366 conn->conn->addr, conn->conn->port, conn->auc_3g_ind);
Harald Welte173afdb2016-04-28 08:53:25 +0200367
368 /* request the identity of the client */
369 rc = ipa_ccm_send_id_req(fd);
370 if (rc < 0)
371 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200372#if 0
373 rc = oap_init(&gsups->oap_config, &conn->oap_state);
374 if (rc != 0)
375 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200376#endif
377 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200378failed:
379 ipa_server_conn_destroy(conn->conn);
380 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200381}
382
383struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100384osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100385 osmo_gsup_read_cb_t read_cb, void *priv)
Harald Weltee72cf552016-04-28 07:18:49 +0200386{
387 struct osmo_gsup_server *gsups;
388 int rc;
389
390 gsups = talloc_zero(ctx, struct osmo_gsup_server);
391 OSMO_ASSERT(gsups);
392
393 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200394 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200395
396 gsups->link = ipa_server_link_create(gsups,
397 /* no e1inp */ NULL,
398 ip_addr, tcp_port,
399 osmo_gsup_server_accept_cb,
400 gsups);
401 if (!gsups->link)
402 goto failed;
403
404 gsups->read_cb = read_cb;
Harald Welte32acace2018-06-16 17:07:28 +0200405 gsups->priv = priv;
Harald Weltee72cf552016-04-28 07:18:49 +0200406
407 rc = ipa_server_link_open(gsups->link);
408 if (rc < 0)
409 goto failed;
410
Harald Weltee72cf552016-04-28 07:18:49 +0200411 return gsups;
412
413failed:
414 osmo_gsup_server_destroy(gsups);
415 return NULL;
416}
417
418void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
419{
420 if (gsups->link) {
421 ipa_server_link_close(gsups->link);
422 ipa_server_link_destroy(gsups->link);
423 gsups->link = NULL;
424 }
425 talloc_free(gsups);
426}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200427
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200428/* Set GSUP message's pdp_infos[0] to a wildcard APN.
429 * Use the provided apn_buf to store the produced APN data. This must remain valid until
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200430 * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too
431 * small. */
432int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup,
433 uint8_t *apn_buf, size_t apn_buf_size)
Stefan Sperling93c5b102018-04-10 19:26:14 +0200434{
435 int l;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200436
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200437 l = osmo_apn_from_str(apn_buf, apn_buf_size, "*");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200438 if (l <= 0)
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200439 return -ENOMEM;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200440
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200441 gsup->pdp_infos[0].apn_enc = apn_buf;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200442 gsup->pdp_infos[0].apn_enc_len = l;
443 gsup->pdp_infos[0].have_info = 1;
444 gsup->num_pdp_infos = 1;
445 /* FIXME: use real value: */
446 gsup->pdp_infos[0].context_id = 1;
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200447
448 return 0;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200449}
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200450
451/**
452 * Populate a gsup message structure with an Insert Subscriber Data Message.
453 * All required memory buffers for data pointed to by pointers in struct omso_gsup_message
454 * must be allocated by the caller and should have the same lifetime as the gsup parameter.
455 *
456 * \param[out] gsup The gsup message to populate.
457 * \param[in] imsi The subscriber's IMSI.
458 * \param[in] msisdn The subscriber's MSISDN.
459 * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form.
460 * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN).
461 * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS).
462 * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN).
463 * \param[in] cn_domain The CN Domain of the subscriber connection.
464 * \returns 0 on success, and negative on error.
465 */
466int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn,
467 uint8_t *msisdn_enc, size_t msisdn_enc_size,
468 uint8_t *apn_buf, size_t apn_buf_size,
469 enum osmo_gsup_cn_domain cn_domain)
470{
471 int len;
472
473 OSMO_ASSERT(gsup);
Neels Hofmeyrad868e22019-11-20 02:36:45 +0100474 *gsup = (struct osmo_gsup_message){
475 .message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST,
476 };
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200477
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200478 osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi));
479
480 if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN)
481 return -ENOSPC;
482
483 OSMO_ASSERT(msisdn_enc);
484 len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn);
485 if (len < 1) {
486 LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn);
487 return -ENOSPC;
488 }
489 gsup->msisdn_enc = msisdn_enc;
490 gsup->msisdn_enc_len = len;
491
492 #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc"
493
494 gsup->cn_domain = cn_domain;
495 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
496 OSMO_ASSERT(apn_buf_size >= APN_MAXLEN);
497 OSMO_ASSERT(apn_buf);
498 /* FIXME: PDP infos - use more fine-grained access control
499 instead of wildcard APN */
500 osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size);
501 }
502
503 return 0;
504}