blob: ed1b28529b18cd838bb93e6b175edf5e705f5c80 [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>
Harald Weltee72cf552016-04-28 07:18:49 +020029
Neels Hofmeyr2f758032019-11-20 00:37:07 +010030#include <osmocom/hlr/gsup_server.h>
31#include <osmocom/hlr/gsup_router.h>
Harald Weltee72cf552016-04-28 07:18:49 +020032
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010033struct msgb *osmo_gsup_msgb_alloc(const char *label)
34{
35 struct msgb *msg = msgb_alloc_headroom(1024+16, 16, label);
36 OSMO_ASSERT(msg);
37 return msg;
38}
39
Harald Weltee72cf552016-04-28 07:18:49 +020040static void osmo_gsup_server_send(struct osmo_gsup_conn *conn,
41 int proto_ext, struct msgb *msg_tx)
42{
43 ipa_prepend_header_ext(msg_tx, proto_ext);
44 ipa_msg_push_header(msg_tx, IPAC_PROTO_OSMO);
45 ipa_server_conn_send(conn->conn, msg_tx);
46}
47
48int osmo_gsup_conn_send(struct osmo_gsup_conn *conn, struct msgb *msg)
49{
50 if (!conn) {
51 msgb_free(msg);
52 return -ENOTCONN;
53 }
54
55 osmo_gsup_server_send(conn, IPAC_PROTO_EXT_GSUP, msg);
56
57 return 0;
58}
59
60static int osmo_gsup_conn_oap_handle(struct osmo_gsup_conn *conn,
61 struct msgb *msg_rx)
62{
Neels Hofmeyrec1b9592016-12-11 01:22:23 +010063#if 0
Harald Weltee72cf552016-04-28 07:18:49 +020064 int rc;
65 struct msgb *msg_tx;
Harald Weltee72cf552016-04-28 07:18:49 +020066 rc = oap_handle(&conn->oap_state, msg_rx, &msg_tx);
67 msgb_free(msg_rx);
68 if (rc < 0)
69 return rc;
70
71 if (msg_tx)
72 osmo_gsup_conn_send(conn, IPAC_PROTO_EXT_OAP, msg_tx);
73#endif
74 return 0;
75}
76
Harald Weltee72cf552016-04-28 07:18:49 +020077/* Data from a given client has arrived over the socket */
78static int osmo_gsup_server_read_cb(struct ipa_server_conn *conn,
79 struct msgb *msg)
80{
81 struct ipaccess_head *hh = (struct ipaccess_head *) msg->data;
82 struct ipaccess_head_ext *he = (struct ipaccess_head_ext *) msgb_l2(msg);
83 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
84 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +020085
86 msg->l2h = &hh->data[0];
87
Harald Welte173afdb2016-04-28 08:53:25 +020088 if (hh->proto == IPAC_PROTO_IPACCESS) {
89 rc = ipa_server_conn_ccm(conn, msg);
90 if (rc < 0) {
91 /* conn is already invalid here! */
Harald Welte173afdb2016-04-28 08:53:25 +020092 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +020093 }
Harald Welte5341b5d2016-04-28 12:48:39 +020094 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +020095 return 0;
96 }
Harald Weltee72cf552016-04-28 07:18:49 +020097
Harald Weltef2d96da2016-04-28 11:13:15 +020098 if (hh->proto != IPAC_PROTO_OSMO) {
99 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA stream ID 0x%02x\n",
100 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200101 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200102 }
Harald Weltee72cf552016-04-28 07:18:49 +0200103
Harald Weltef2d96da2016-04-28 11:13:15 +0200104 if (!he || msgb_l2len(msg) < sizeof(*he)) {
105 LOGP(DLGSUP, LOGL_NOTICE, "short IPA message\n");
Harald Weltee72cf552016-04-28 07:18:49 +0200106 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200107 }
Harald Weltee72cf552016-04-28 07:18:49 +0200108
109 msg->l2h = &he->data[0];
110
111 if (he->proto == IPAC_PROTO_EXT_GSUP) {
112 OSMO_ASSERT(clnt->server->read_cb != NULL);
113 clnt->server->read_cb(clnt, msg);
114 /* expecting read_cb() to free msg */
115 } else if (he->proto == IPAC_PROTO_EXT_OAP) {
116 return osmo_gsup_conn_oap_handle(clnt, msg);
117 /* osmo_gsup_client_oap_handle frees msg */
Harald Weltef2d96da2016-04-28 11:13:15 +0200118 } else {
119 LOGP(DLGSUP, LOGL_NOTICE, "Unsupported IPA Osmo Proto 0x%02x\n",
120 hh->proto);
Harald Weltee72cf552016-04-28 07:18:49 +0200121 goto invalid;
Harald Weltef2d96da2016-04-28 11:13:15 +0200122 }
Harald Weltee72cf552016-04-28 07:18:49 +0200123
124 return 0;
125
126invalid:
127 LOGP(DLGSUP, LOGL_NOTICE,
Harald Weltef2d96da2016-04-28 11:13:15 +0200128 "GSUP received an invalid IPA message from %s:%d: %s\n",
129 conn->addr, conn->port, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Harald Weltee72cf552016-04-28 07:18:49 +0200130 msgb_free(msg);
131 return -1;
132
133}
134
Harald Weltee687be52016-05-03 18:49:27 +0200135static void osmo_tlvp_dump(const struct tlv_parsed *tlvp,
136 int subsys, int level)
137{
138 unsigned int i;
139
140 for (i = 0; i < ARRAY_SIZE(tlvp->lv); i++) {
141 if (!TLVP_PRESENT(tlvp, i))
142 continue;
143
144 LOGP(subsys, level, "%u: %s\n", i,
145 TLVP_VAL(tlvp, i));
146 LOGP(subsys, level, "%u: %s\n", i,
147 osmo_hexdump(TLVP_VAL(tlvp, i),
148 TLVP_LEN(tlvp, i)));
149 }
150}
151
152/* FIXME: should this be parrt of ipas_server handling, not GSUP? */
153static void tlvp_copy(void *ctx, struct tlv_parsed *out, const struct tlv_parsed *in)
154{
155 unsigned int i;
156
157 for (i = 0; i < ARRAY_SIZE(out->lv); i++) {
158 if (!TLVP_PRESENT(in, i)) {
159 if (TLVP_PRESENT(out, i)) {
160 talloc_free((void *) out->lv[i].val);
161 out->lv[i].val = NULL;
162 out->lv[i].len = 0;
163 }
164 continue;
165 }
166 out->lv[i].val = talloc_memdup(ctx, in->lv[i].val, in->lv[i].len);
167 out->lv[i].len = in->lv[i].len;
168 }
169}
170
171int osmo_gsup_conn_ccm_get(const struct osmo_gsup_conn *clnt, uint8_t **addr,
172 uint8_t tag)
173{
174 if (!TLVP_PRESENT(&clnt->ccm, tag))
175 return -ENODEV;
176 *addr = (uint8_t *) TLVP_VAL(&clnt->ccm, tag);
177
178 return TLVP_LEN(&clnt->ccm, tag);
179}
180
Harald Welte173afdb2016-04-28 08:53:25 +0200181static int osmo_gsup_server_ccm_cb(struct ipa_server_conn *conn,
182 struct msgb *msg, struct tlv_parsed *tlvp,
183 struct ipaccess_unit *unit)
184{
Harald Weltee687be52016-05-03 18:49:27 +0200185 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
Neels Hofmeyrec1b9592016-12-11 01:22:23 +0100186 uint8_t *addr = NULL;
Harald Weltee687be52016-05-03 18:49:27 +0200187 size_t addr_len;
188
Harald Welte173afdb2016-04-28 08:53:25 +0200189 LOGP(DLGSUP, LOGL_INFO, "CCM Callback\n");
Harald Weltee687be52016-05-03 18:49:27 +0200190
191 /* FIXME: should this be parrt of ipas_server handling, not
192 * GSUP? */
193 tlvp_copy(clnt, &clnt->ccm, tlvp);
194 osmo_tlvp_dump(tlvp, DLGSUP, LOGL_INFO);
195
196 addr_len = osmo_gsup_conn_ccm_get(clnt, &addr, IPAC_IDTAG_SERNR);
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100197 if (addr_len <= 0) {
198 LOGP(DLGSUP, LOGL_ERROR, "GSUP client %s:%u has no %s IE and"
199 " cannot be routed\n",
200 conn->addr, conn->port,
201 ipa_ccm_idtag_name(IPAC_IDTAG_SERNR));
202 return -EINVAL;
203 }
Harald Weltee687be52016-05-03 18:49:27 +0200204
Neels Hofmeyr5ecdc562017-02-24 06:38:04 +0100205 gsup_route_add(clnt, addr, addr_len);
Harald Welte173afdb2016-04-28 08:53:25 +0200206 return 0;
207}
208
Harald Weltee72cf552016-04-28 07:18:49 +0200209static int osmo_gsup_server_closed_cb(struct ipa_server_conn *conn)
210{
211 struct osmo_gsup_conn *clnt = (struct osmo_gsup_conn *)conn->data;
212
213 LOGP(DLGSUP, LOGL_INFO, "Lost GSUP client %s:%d\n",
214 conn->addr, conn->port);
215
Harald Weltee687be52016-05-03 18:49:27 +0200216 gsup_route_del_conn(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200217 llist_del(&clnt->list);
Harald Weltea7617e92016-04-28 12:57:10 +0200218 talloc_free(clnt);
Harald Weltee72cf552016-04-28 07:18:49 +0200219
220 return 0;
221}
222
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100223/* Add conn to the clients list in a way that conn->auc_3g_ind takes the lowest
224 * unused integer and the list of clients remains sorted by auc_3g_ind.
225 * Keep this function non-static to allow linking in a unit test. */
226void osmo_gsup_server_add_conn(struct llist_head *clients,
227 struct osmo_gsup_conn *conn)
228{
229 struct osmo_gsup_conn *c;
230 struct osmo_gsup_conn *prev_conn;
231
232 c = llist_first_entry_or_null(clients, struct osmo_gsup_conn, list);
233
234 /* Is the first index, 0, unused? */
235 if (!c || c->auc_3g_ind > 0) {
236 conn->auc_3g_ind = 0;
237 llist_add(&conn->list, clients);
238 return;
239 }
240
241 /* Look for a gap later on */
242 prev_conn = NULL;
243 llist_for_each_entry(c, clients, list) {
244 /* skip first item, we know it has auc_3g_ind == 0. */
245 if (!prev_conn) {
246 prev_conn = c;
247 continue;
248 }
249 if (c->auc_3g_ind > prev_conn->auc_3g_ind + 1)
250 break;
251 prev_conn = c;
252 }
253
254 OSMO_ASSERT(prev_conn);
255
256 conn->auc_3g_ind = prev_conn->auc_3g_ind + 1;
257 llist_add(&conn->list, &prev_conn->list);
258}
259
Harald Weltee72cf552016-04-28 07:18:49 +0200260/* a client has connected to the server socket and we have accept()ed it */
261static int osmo_gsup_server_accept_cb(struct ipa_server_link *link, int fd)
262{
263 struct osmo_gsup_conn *conn;
264 struct osmo_gsup_server *gsups =
265 (struct osmo_gsup_server *) link->data;
Harald Welte173afdb2016-04-28 08:53:25 +0200266 int rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200267
Harald Weltea7617e92016-04-28 12:57:10 +0200268 conn = talloc_zero(gsups, struct osmo_gsup_conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200269 OSMO_ASSERT(conn);
270
Harald Weltea7617e92016-04-28 12:57:10 +0200271 conn->conn = ipa_server_conn_create(gsups, link, fd,
Harald Weltee72cf552016-04-28 07:18:49 +0200272 osmo_gsup_server_read_cb,
273 osmo_gsup_server_closed_cb, conn);
274 OSMO_ASSERT(conn->conn);
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100275 conn->conn->ccm_cb = osmo_gsup_server_ccm_cb;
Harald Weltee72cf552016-04-28 07:18:49 +0200276
277 /* link data structure with server structure */
278 conn->server = gsups;
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100279 osmo_gsup_server_add_conn(&gsups->clients, conn);
Harald Weltee72cf552016-04-28 07:18:49 +0200280
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100281 LOGP(DLGSUP, LOGL_INFO, "New GSUP client %s:%d (IND=%u)\n",
282 conn->conn->addr, conn->conn->port, conn->auc_3g_ind);
Harald Welte173afdb2016-04-28 08:53:25 +0200283
284 /* request the identity of the client */
285 rc = ipa_ccm_send_id_req(fd);
286 if (rc < 0)
287 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200288#if 0
289 rc = oap_init(&gsups->oap_config, &conn->oap_state);
290 if (rc != 0)
291 goto failed;
Harald Weltee72cf552016-04-28 07:18:49 +0200292#endif
293 return 0;
Harald Welte173afdb2016-04-28 08:53:25 +0200294failed:
295 ipa_server_conn_destroy(conn->conn);
296 return -1;
Harald Weltee72cf552016-04-28 07:18:49 +0200297}
298
299struct osmo_gsup_server *
Max9cacb6f2017-02-20 17:22:56 +0100300osmo_gsup_server_create(void *ctx, const char *ip_addr, uint16_t tcp_port,
301 osmo_gsup_read_cb_t read_cb,
Harald Welte32acace2018-06-16 17:07:28 +0200302 struct llist_head *lu_op_lst, void *priv)
Harald Weltee72cf552016-04-28 07:18:49 +0200303{
304 struct osmo_gsup_server *gsups;
305 int rc;
306
307 gsups = talloc_zero(ctx, struct osmo_gsup_server);
308 OSMO_ASSERT(gsups);
309
310 INIT_LLIST_HEAD(&gsups->clients);
Harald Weltee687be52016-05-03 18:49:27 +0200311 INIT_LLIST_HEAD(&gsups->routes);
Harald Weltee72cf552016-04-28 07:18:49 +0200312
313 gsups->link = ipa_server_link_create(gsups,
314 /* no e1inp */ NULL,
315 ip_addr, tcp_port,
316 osmo_gsup_server_accept_cb,
317 gsups);
318 if (!gsups->link)
319 goto failed;
320
321 gsups->read_cb = read_cb;
Harald Welte32acace2018-06-16 17:07:28 +0200322 gsups->priv = priv;
Harald Weltee72cf552016-04-28 07:18:49 +0200323
324 rc = ipa_server_link_open(gsups->link);
325 if (rc < 0)
326 goto failed;
327
Max9cacb6f2017-02-20 17:22:56 +0100328 gsups->luop = lu_op_lst;
329
Harald Weltee72cf552016-04-28 07:18:49 +0200330 return gsups;
331
332failed:
333 osmo_gsup_server_destroy(gsups);
334 return NULL;
335}
336
337void osmo_gsup_server_destroy(struct osmo_gsup_server *gsups)
338{
339 if (gsups->link) {
340 ipa_server_link_close(gsups->link);
341 ipa_server_link_destroy(gsups->link);
342 gsups->link = NULL;
343 }
344 talloc_free(gsups);
345}
Stefan Sperling93c5b102018-04-10 19:26:14 +0200346
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200347/* Set GSUP message's pdp_infos[0] to a wildcard APN.
348 * Use the provided apn_buf to store the produced APN data. This must remain valid until
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200349 * osmo_gsup_encode() is done. Return 0 if an entry was added, -ENOMEM if the provided buffer is too
350 * small. */
351int osmo_gsup_configure_wildcard_apn(struct osmo_gsup_message *gsup,
352 uint8_t *apn_buf, size_t apn_buf_size)
Stefan Sperling93c5b102018-04-10 19:26:14 +0200353{
354 int l;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200355
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200356 l = osmo_apn_from_str(apn_buf, apn_buf_size, "*");
Stefan Sperling93c5b102018-04-10 19:26:14 +0200357 if (l <= 0)
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200358 return -ENOMEM;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200359
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200360 gsup->pdp_infos[0].apn_enc = apn_buf;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200361 gsup->pdp_infos[0].apn_enc_len = l;
362 gsup->pdp_infos[0].have_info = 1;
363 gsup->num_pdp_infos = 1;
364 /* FIXME: use real value: */
365 gsup->pdp_infos[0].context_id = 1;
Neels Hofmeyr1b8a1dc2018-05-04 16:46:47 +0200366
367 return 0;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200368}
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200369
370/**
371 * Populate a gsup message structure with an Insert Subscriber Data Message.
372 * All required memory buffers for data pointed to by pointers in struct omso_gsup_message
373 * must be allocated by the caller and should have the same lifetime as the gsup parameter.
374 *
375 * \param[out] gsup The gsup message to populate.
376 * \param[in] imsi The subscriber's IMSI.
377 * \param[in] msisdn The subscriber's MSISDN.
378 * \param[out] msisdn_enc A buffer large enough to store the MSISDN in encoded form.
379 * \param[in] msisdn_enc_size Size of the buffer (must be >= OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN).
380 * \param[out] apn_buf A buffer large enough to store an APN (required if cn_domain is OSMO_GSUP_CN_DOMAIN_PS).
381 * \param[in] apn_buf_size Size of APN buffer (must be >= APN_MAXLEN).
382 * \param[in] cn_domain The CN Domain of the subscriber connection.
383 * \returns 0 on success, and negative on error.
384 */
385int osmo_gsup_create_insert_subscriber_data_msg(struct osmo_gsup_message *gsup, const char *imsi, const char *msisdn,
386 uint8_t *msisdn_enc, size_t msisdn_enc_size,
387 uint8_t *apn_buf, size_t apn_buf_size,
388 enum osmo_gsup_cn_domain cn_domain)
389{
390 int len;
391
392 OSMO_ASSERT(gsup);
393
394 gsup->message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST;
395 osmo_strlcpy(gsup->imsi, imsi, sizeof(gsup->imsi));
396
397 if (msisdn_enc_size < OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN)
398 return -ENOSPC;
399
400 OSMO_ASSERT(msisdn_enc);
401 len = gsm48_encode_bcd_number(msisdn_enc, msisdn_enc_size, 0, msisdn);
402 if (len < 1) {
403 LOGP(DLGSUP, LOGL_ERROR, "%s: Error: cannot encode MSISDN '%s'\n", imsi, msisdn);
404 return -ENOSPC;
405 }
406 gsup->msisdn_enc = msisdn_enc;
407 gsup->msisdn_enc_len = len;
408
409 #pragma message "FIXME: deal with encoding the following data: gsup.hlr_enc"
410
411 gsup->cn_domain = cn_domain;
412 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) {
413 OSMO_ASSERT(apn_buf_size >= APN_MAXLEN);
414 OSMO_ASSERT(apn_buf);
415 /* FIXME: PDP infos - use more fine-grained access control
416 instead of wildcard APN */
417 osmo_gsup_configure_wildcard_apn(gsup, apn_buf, apn_buf_size);
418 }
419
420 return 0;
421}