blob: 0098a322b9a25b564e9e31e08bf78e4c9b9fd68a [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 Welteaabae9e2016-04-28 12:48:14 +020020#include <signal.h>
Harald Weltee687be52016-05-03 18:49:27 +020021#include <errno.h>
Maxea8b0d42017-02-14 16:53:04 +010022#include <stdbool.h>
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +010023#include <getopt.h>
24
Harald Weltee72cf552016-04-28 07:18:49 +020025#include <osmocom/core/msgb.h>
26#include <osmocom/core/logging.h>
27#include <osmocom/core/application.h>
28#include <osmocom/gsm/gsup.h>
Neels Hofmeyr7685a782017-01-30 23:30:26 +010029#include <osmocom/vty/vty.h>
30#include <osmocom/vty/command.h>
31#include <osmocom/vty/telnet_interface.h>
32#include <osmocom/vty/ports.h>
Max372868b2017-03-02 12:12:00 +010033#include <osmocom/ctrl/control_vty.h>
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +020034#include <osmocom/gsm/apn.h>
Oliver Smith783ac812018-12-17 11:34:51 +010035#include <osmocom/gsm/gsm48_ie.h>
Harald Weltee72cf552016-04-28 07:18:49 +020036
37#include "db.h"
Maxd4bebbd2017-03-02 12:00:19 +010038#include "hlr.h"
Max372868b2017-03-02 12:12:00 +010039#include "ctrl.h"
Harald Weltee72cf552016-04-28 07:18:49 +020040#include "logging.h"
41#include "gsup_server.h"
Harald Weltee687be52016-05-03 18:49:27 +020042#include "gsup_router.h"
Harald Weltee72cf552016-04-28 07:18:49 +020043#include "rand.h"
Maxea8b0d42017-02-14 16:53:04 +010044#include "luop.h"
Neels Hofmeyr7685a782017-01-30 23:30:26 +010045#include "hlr_vty.h"
Harald Weltebb779392018-06-16 20:21:10 +020046#include "hlr_ussd.h"
Harald Weltee72cf552016-04-28 07:18:49 +020047
Harald Welted5807b82018-07-29 12:27:41 +020048struct hlr *g_hlr;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070049static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020050
Stefan Sperlingf1622522018-04-09 11:39:16 +020051/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
52 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020053 * \param[in] subscr A subscriber we have new data to send for.
54 */
55void
56osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
57{
58 struct osmo_gsup_conn *co;
59
Neels Hofmeyre66e5252018-09-28 02:59:19 +020060 if (g_hlr->gs == NULL) {
61 LOGP(DLGSUP, LOGL_DEBUG,
62 "IMSI %s: NOT Notifying peers of subscriber data change,"
63 " there is no GSUP server\n",
64 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020065 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020066 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020067
68 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020069 struct osmo_gsup_message gsup = { };
70 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
71 uint8_t apn[APN_MAXLEN];
72 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020073 uint8_t *peer;
74 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020075 size_t peer_strlen;
76 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020077 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020078
Neels Hofmeyre66e5252018-09-28 02:59:19 +020079 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020080 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020081 peer_compare = subscr->sgsn_number;
82 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020083 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020084 peer_compare = subscr->vlr_number;
85 } else {
86 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +020087 continue;
88 }
89
Neels Hofmeyre66e5252018-09-28 02:59:19 +020090 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
91 if (peer_len < 0) {
92 LOGP(DLGSUP, LOGL_ERROR,
93 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
94 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
95 co && co->conn && co->conn->server? co->conn->server->port : 0);
96 continue;
97 }
98
99 peer_strlen = strnlen((const char*)peer, peer_len);
100 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
101 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
102 /* I hope peer is always nul terminated... */
103 if (peer_strlen < peer_len)
104 LOGP(DLGSUP, LOGL_DEBUG,
105 "IMSI %s: subscriber change: skipping %s peer %s\n",
106 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
107 osmo_quote_str((char*)peer, -1));
108 continue;
109 }
110
111 LOGP(DLGSUP, LOGL_DEBUG,
112 "IMSI %s: subscriber change: notifying %s peer %s\n",
113 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
114 osmo_quote_str(peer_compare, -1));
115
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200116 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
117 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200118 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200119 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200120 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200121 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
122 co && co->conn && co->conn->server? co->conn->server->port : 0);
123 continue;
124 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200125
Stefan Sperling93c5b102018-04-10 19:26:14 +0200126 /* Send ISD to MSC/SGSN */
127 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE");
128 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200129 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200130 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
131 "for %s:%u\n", subscr->imsi,
132 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
133 co && co->conn && co->conn->server? co->conn->server->port : 0);
134 continue;
135 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200136 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200137
Stefan Sperling93c5b102018-04-10 19:26:14 +0200138 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
139 LOGP(DMAIN, LOGL_ERROR,
140 "IMSI='%s': Cannot notify GSUP client; send operation failed "
141 "for %s:%u\n", subscr->imsi,
142 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
143 co && co->conn && co->conn->server? co->conn->server->port : 0);
144 continue;
145 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200146 }
147}
148
Harald Weltee687be52016-05-03 18:49:27 +0200149/***********************************************************************
150 * Send Auth Info handling
151 ***********************************************************************/
152
Harald Weltee72cf552016-04-28 07:18:49 +0200153/* process an incoming SAI request */
154static int rx_send_auth_info(struct osmo_gsup_conn *conn,
Maxd4bebbd2017-03-02 12:00:19 +0100155 const struct osmo_gsup_message *gsup,
156 struct db_context *dbc)
Harald Weltee72cf552016-04-28 07:18:49 +0200157{
158 struct osmo_gsup_message gsup_out;
159 struct msgb *msg_out;
160 int rc;
161
162 /* initialize return message structure */
163 memset(&gsup_out, 0, sizeof(gsup_out));
Harald Weltee72cf552016-04-28 07:18:49 +0200164 memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
165
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100166 rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
167 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200168 ARRAY_SIZE(gsup_out.auth_vectors),
Harald Welte9be0d2f2016-06-10 17:34:02 +0200169 gsup->rand, gsup->auts);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100170 if (rc <= 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200171 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100172 switch (rc) {
173 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100174 /* 0 means "0 tuples generated", which shouldn't happen.
175 * Treat the same as "no auth data". */
176 case -ENOKEY:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100177 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
178 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
179 gsup->imsi);
180 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
181 break;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100182 case -ENOENT:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100183 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100184 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
185 break;
186 default:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100187 LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100188 gsup_out.cause = GMM_CAUSE_NET_FAIL;
189 break;
190 }
Harald Welte15db8262016-05-05 16:50:39 +0200191 } else {
192 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
193 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200194 }
195
Harald Weltee687be52016-05-03 18:49:27 +0200196 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
Harald Weltee72cf552016-04-28 07:18:49 +0200197 osmo_gsup_encode(msg_out, &gsup_out);
198 return osmo_gsup_conn_send(conn, msg_out);
199}
200
Harald Weltee687be52016-05-03 18:49:27 +0200201/***********************************************************************
202 * LU Operation State / Structure
203 ***********************************************************************/
204
205static LLIST_HEAD(g_lu_ops);
206
Harald Weltee687be52016-05-03 18:49:27 +0200207/*! Receive Cancel Location Result from old VLR/SGSN */
208void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
Maxea8b0d42017-02-14 16:53:04 +0100209 const struct osmo_gsup_message *gsup)
Harald Weltee687be52016-05-03 18:49:27 +0200210{
211 OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
212 /* FIXME: Check for spoofing */
213
214 osmo_timer_del(&luop->timer);
215
216 /* FIXME */
217
218 lu_op_tx_insert_subscr_data(luop);
219}
220
Harald Weltee687be52016-05-03 18:49:27 +0200221/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
222static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
223 const struct osmo_gsup_message *gsup)
224{
225 OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
226 /* FIXME: Check for spoofing */
227
228 osmo_timer_del(&luop->timer);
229
230 /* Subscriber_Present_HLR */
231 /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
232
233 /* Send final ACK towards inquiring VLR/SGSN */
234 lu_op_tx_ack(luop);
235}
236
237/*! Receive GSUP message for given \ref lu_operation */
238void lu_op_rx_gsup(struct lu_operation *luop,
239 const struct osmo_gsup_message *gsup)
240{
241 switch (gsup->message_type) {
242 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
243 /* FIXME */
244 break;
245 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
246 lu_op_rx_insert_subscr_data_ack(luop, gsup);
247 break;
248 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
249 /* FIXME */
250 break;
251 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
252 lu_op_rx_cancel_old_ack(luop, gsup);
253 break;
254 default:
255 LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
256 gsup->message_type);
257 break;
258 }
259}
260
Harald Weltee687be52016-05-03 18:49:27 +0200261/*! Receive Update Location Request, creates new \ref lu_operation */
262static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
263 const struct osmo_gsup_message *gsup)
264{
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200265 struct hlr_subscriber *subscr;
Maxea8b0d42017-02-14 16:53:04 +0100266 struct lu_operation *luop = lu_op_alloc_conn(conn);
267 if (!luop) {
Harald Weltee687be52016-05-03 18:49:27 +0200268 LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
Maxea8b0d42017-02-14 16:53:04 +0100269 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +0200270 }
271
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200272 subscr = &luop->subscr;
273
Harald Weltee687be52016-05-03 18:49:27 +0200274 lu_op_statechg(luop, LU_S_LU_RECEIVED);
Maxea8b0d42017-02-14 16:53:04 +0100275
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200276 switch (gsup->cn_domain) {
277 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200278 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200279 break;
280 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200281 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
282 * accordance with the GSUP spec in osmo-hlr's user manual (section
283 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
284 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200285 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200286 conn->supports_ps = true;
Stefan Sperling1cb48922018-05-03 14:05:56 +0200287 luop->is_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200288 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200289 }
Harald Weltee687be52016-05-03 18:49:27 +0200290 llist_add(&luop->list, &g_lu_ops);
291
292 /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
293
294 /* check if subscriber is known at all */
Maxd4bebbd2017-03-02 12:00:19 +0100295 if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) {
Harald Weltee687be52016-05-03 18:49:27 +0200296 /* Send Error back: Subscriber Unknown in HLR */
Harald Weltebd0d5bf2017-11-06 03:55:02 +0900297 osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi));
Harald Weltee687be52016-05-03 18:49:27 +0200298 lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
299 return 0;
300 }
301
Harald Welte99909272016-05-05 18:24:15 +0200302 /* Check if subscriber is generally permitted on CS or PS
303 * service (as requested) */
Maxea8b0d42017-02-14 16:53:04 +0100304 if (!luop->is_ps && !luop->subscr.nam_cs) {
Harald Weltee687be52016-05-03 18:49:27 +0200305 lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
306 return 0;
Maxea8b0d42017-02-14 16:53:04 +0100307 } else if (luop->is_ps && !luop->subscr.nam_ps) {
Harald Weltee687be52016-05-03 18:49:27 +0200308 lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED);
309 return 0;
310 }
311
312 /* TODO: Set subscriber tracing = deactive in VLR/SGSN */
313
314#if 0
315 /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
316 if (luop->is_ps == false &&
317 strcmp(subscr->vlr_number, vlr_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200318 lu_op_tx_cancel_old(luop);
319 } else if (luop->is_ps == true &&
320 strcmp(subscr->sgsn_number, sgsn_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200321 lu_op_tx_cancel_old(luop);
322 } else
323#endif
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200324
325 /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */
326 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing %s = %s\n",
327 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number",
328 osmo_quote_str((const char*)luop->peer, -1));
329 if (db_subscr_lu(g_hlr->dbc, subscr->id, (const char *)luop->peer, luop->is_ps))
330 LOGP(DAUC, LOGL_ERROR, "IMSI='%s': Cannot update %s in the database\n",
331 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number");
332
Oliver Smith66106c02019-01-09 12:05:15 +0100333 /* TODO: Subscriber allowed to roam in PLMN? */
334 /* TODO: Update RoutingInfo */
335 /* TODO: Reset Flag MS Purged (cs/ps) */
336 /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
337 lu_op_tx_insert_subscr_data(luop);
338
Harald Weltee687be52016-05-03 18:49:27 +0200339 return 0;
340}
341
Harald Welteb18f0e02016-05-05 21:03:03 +0200342static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
343 const struct osmo_gsup_message *gsup)
344{
345 struct osmo_gsup_message gsup_reply = {0};
346 struct msgb *msg_out;
347 bool is_ps = false;
348 int rc;
349
350 LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
351 is_ps ? "PS" : "CS");
352
353 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
354
355 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
356 is_ps = true;
357
358 /* FIXME: check if the VLR that sends the purge is the same that
359 * we have on record. Only update if yes */
360
361 /* Perform the actual update of the DB */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200362 rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200363
Harald Welte3f2a9a22018-03-01 23:35:35 +0100364 if (rc == 0)
Harald Welteb18f0e02016-05-05 21:03:03 +0200365 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
Harald Welte3f2a9a22018-03-01 23:35:35 +0100366 else if (rc == -ENOENT) {
Harald Welteb18f0e02016-05-05 21:03:03 +0200367 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
368 gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
369 } else {
370 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
371 gsup_reply.cause = GMM_CAUSE_NET_FAIL;
372 }
373
374 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
375 osmo_gsup_encode(msg_out, &gsup_reply);
376 return osmo_gsup_conn_send(conn, msg_out);
377}
378
Harald Weltea1d3b042018-06-11 20:28:35 +0200379static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
380 enum osmo_gsup_message_type type_in, uint8_t err_cause)
381{
382 int type_err = osmo_gsup_get_err_msg_type(type_in);
383 struct osmo_gsup_message gsup_reply = {0};
384 struct msgb *msg_out;
385
386 if (type_err < 0) {
387 LOGP(DMAIN, LOGL_ERROR, "unable to determine error response for %s\n",
388 osmo_gsup_message_type_name(type_in));
389 return type_err;
390 }
391
392 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
393 gsup_reply.message_type = type_err;
394 gsup_reply.cause = err_cause;
395 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response");
396 OSMO_ASSERT(msg_out);
397 osmo_gsup_encode(msg_out, &gsup_reply);
398 LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
399 return osmo_gsup_conn_send(conn, msg_out);
400}
401
Oliver Smith783ac812018-12-17 11:34:51 +0100402static int rx_check_imei_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
403{
404 struct osmo_gsup_message gsup_reply = {0};
405 struct msgb *msg_out;
406 char imei[GSM23003_IMEI_NUM_DIGITS+1] = {0};
407
408 /* Encoded IMEI length check */
409 if (!gsup->imei_enc || gsup->imei_enc_len < 1 || gsup->imei_enc[0] >= sizeof(imei)) {
410 LOGP(DMAIN, LOGL_ERROR, "%s: wrong encoded IMEI length\n", gsup->imsi);
411 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
412 return -1;
413 }
414
415 /* Decode IMEI */
416 if (gsm48_decode_bcd_number(imei, sizeof(imei), gsup->imei_enc, 0) < 0) {
417 LOGP(DMAIN, LOGL_ERROR, "%s: failed to decode IMEI\n", gsup->imsi);
418 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
419 return -1;
420 }
421
Oliver Smith851814a2019-01-11 15:30:21 +0100422 /* Save in DB if desired */
423 if (g_hlr->store_imei) {
424 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
425 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
426 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
427 return -1;
428 }
429 } else {
430 /* Check if subscriber exists and print IMEI */
431 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei);
432 struct hlr_subscriber subscr;
433 if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) {
434 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
435 return -1;
436 }
437 }
Oliver Smith783ac812018-12-17 11:34:51 +0100438
439 /* Accept all IMEIs */
440 gsup_reply.imei_result = OSMO_GSUP_IMEI_RESULT_ACK;
441 gsup_reply.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT;
442 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP Check_IMEI response");
443 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
444 osmo_gsup_encode(msg_out, &gsup_reply);
445 return osmo_gsup_conn_send(conn, msg_out);
446}
447
Harald Weltee72cf552016-04-28 07:18:49 +0200448static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
449{
450 static struct osmo_gsup_message gsup;
451 int rc;
452
Harald Weltee687be52016-05-03 18:49:27 +0200453 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200454 if (rc < 0) {
455 LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
456 return rc;
457 }
458
Harald Weltea1d3b042018-06-11 20:28:35 +0200459 /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
460 * digits is impossible. Even 5 digits is a highly theoretical case */
461 if (strlen(gsup.imsi) < 5)
462 return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
463
Harald Weltee72cf552016-04-28 07:18:49 +0200464 switch (gsup.message_type) {
465 /* requests sent to us */
466 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Maxd4bebbd2017-03-02 12:00:19 +0100467 rx_send_auth_info(conn, &gsup, g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200468 break;
469 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Harald Weltee687be52016-05-03 18:49:27 +0200470 rx_upd_loc_req(conn, &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200471 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200472 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
473 rx_purge_ms_req(conn, &gsup);
474 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200475 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100476 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
477 LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
478 "for IMSI %s\n", gsup.imsi);
479 break;
480 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
481 LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
482 gsup.imsi);
483 break;
Harald Weltebb779392018-06-16 20:21:10 +0200484 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
485 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
486 rx_proc_ss_req(conn, &gsup);
487 break;
488 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
489 rx_proc_ss_error(conn, &gsup);
490 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200491 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200492 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200493 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
494 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
495 {
Maxea8b0d42017-02-14 16:53:04 +0100496 struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
497 &g_lu_ops);
Harald Weltee687be52016-05-03 18:49:27 +0200498 if (!luop) {
Maxaa0fefd2017-02-16 12:25:22 +0100499 LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
500 "unknown IMSI %s\n",
501 osmo_gsup_message_type_name(gsup.message_type),
Harald Weltee687be52016-05-03 18:49:27 +0200502 gsup.imsi);
503 break;
504 }
505 lu_op_rx_gsup(luop, &gsup);
506 }
Harald Weltee72cf552016-04-28 07:18:49 +0200507 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100508 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
509 rx_check_imei_req(conn, &gsup);
510 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200511 default:
Maxaa0fefd2017-02-16 12:25:22 +0100512 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
513 osmo_gsup_message_type_name(gsup.message_type));
Harald Weltee72cf552016-04-28 07:18:49 +0200514 break;
515 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200516 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200517 return 0;
518}
519
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100520static void print_usage()
521{
522 printf("Usage: osmo-hlr\n");
523}
524
525static void print_help()
526{
527 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100528 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100529 printf(" -l --database db-name The database to use.\n");
530 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
531 printf(" -D --daemonize Fork the process into a background daemon.\n");
532 printf(" -s --disable-color Do not print ANSI colors in the log\n");
533 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
534 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100535 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100536 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100537}
538
539static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100540 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100541 const char *db_file;
542 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100543 bool db_upgrade;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100544} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100545 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100546 .db_file = "hlr.db",
547 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100548 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100549};
550
551static void handle_options(int argc, char **argv)
552{
553 while (1) {
554 int option_index = 0, c;
555 static struct option long_options[] = {
556 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100557 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100558 {"database", 1, 0, 'l'},
559 {"debug", 1, 0, 'd'},
560 {"daemonize", 0, 0, 'D'},
561 {"disable-color", 0, 0, 's'},
562 {"log-level", 1, 0, 'e'},
563 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100564 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100565 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100566 {0, 0, 0, 0}
567 };
568
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100569 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100570 long_options, &option_index);
571 if (c == -1)
572 break;
573
574 switch (c) {
575 case 'h':
576 print_usage();
577 print_help();
578 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100579 case 'c':
580 cmdline_opts.config_file = optarg;
581 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100582 case 'l':
583 cmdline_opts.db_file = optarg;
584 break;
585 case 'd':
586 log_parse_category_mask(osmo_stderr_target, optarg);
587 break;
588 case 'D':
589 cmdline_opts.daemonize = 1;
590 break;
591 case 's':
592 log_set_use_color(osmo_stderr_target, 0);
593 break;
594 case 'e':
595 log_set_log_level(osmo_stderr_target, atoi(optarg));
596 break;
597 case 'T':
598 log_set_print_timestamp(osmo_stderr_target, 1);
599 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100600 case 'U':
601 cmdline_opts.db_upgrade = true;
602 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100603 case 'V':
604 print_version(1);
605 exit(0);
606 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100607 default:
608 /* catch unknown options *as well as* missing arguments. */
609 fprintf(stderr, "Error in command line options. Exiting.\n");
610 exit(-1);
611 break;
612 }
613 }
614}
615
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100616static void *hlr_ctx = NULL;
Harald Welteaabae9e2016-04-28 12:48:14 +0200617
618static void signal_hdlr(int signal)
619{
620 switch (signal) {
621 case SIGINT:
622 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700623 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200624 break;
625 case SIGUSR1:
626 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100627 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200628 break;
629 }
630}
Harald Weltee72cf552016-04-28 07:18:49 +0200631
Max372868b2017-03-02 12:12:00 +0100632static const char vlr_copyright[] =
633 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
634 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
635 "This is free software: you are free to change and redistribute it.\r\n"
636 "There is NO WARRANTY, to the extent permitted by law.\r\n";
637
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100638static struct vty_app_info vty_info = {
639 .name = "OsmoHLR",
640 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100641 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100642 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200643 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100644};
645
Harald Weltee72cf552016-04-28 07:18:49 +0200646int main(int argc, char **argv)
647{
Harald Weltee72cf552016-04-28 07:18:49 +0200648 int rc;
649
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700650 /* Track the use of talloc NULL memory contexts */
651 talloc_enable_null_tracking();
652
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100653 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
654 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100655 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200656
Maxd4bebbd2017-03-02 12:00:19 +0100657 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200658 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200659 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200660 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200661 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Maxd4bebbd2017-03-02 12:00:19 +0100662
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700663 /* Init default (call independent) SS session guard timeout value */
664 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
665
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200666 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200667 if (rc < 0) {
668 fprintf(stderr, "Error initializing logging\n");
669 exit(1);
670 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100671
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100672 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100673 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100674 handle_options(argc, argv);
Harald Welted5807b82018-07-29 12:27:41 +0200675 hlr_vty_init(&hlr_log_info);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100676
677 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
678 if (rc < 0) {
679 LOGP(DMAIN, LOGL_FATAL,
680 "Failed to parse the config file: '%s'\n",
681 cmdline_opts.config_file);
682 return rc;
683 }
684
685 /* start telnet after reading config for vty_get_bind_addr() */
686 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
687 OSMO_VTY_PORT_HLR);
688 if (rc < 0)
689 return rc;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100690
Harald Weltee72cf552016-04-28 07:18:49 +0200691 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
692
693 rc = rand_init();
694 if (rc < 0) {
695 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
696 exit(1);
697 }
698
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100699 g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100700 if (!g_hlr->dbc) {
Harald Weltee72cf552016-04-28 07:18:49 +0200701 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
702 exit(1);
703 }
704
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200705 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Harald Welte32acace2018-06-16 17:07:28 +0200706 read_cb, &g_lu_ops, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100707 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200708 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
709 exit(1);
710 }
711
Max372868b2017-03-02 12:12:00 +0100712 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200713 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100714
Harald Welteaabae9e2016-04-28 12:48:14 +0200715 osmo_init_ignore_signals();
716 signal(SIGINT, &signal_hdlr);
717 signal(SIGUSR1, &signal_hdlr);
718
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100719 if (cmdline_opts.daemonize) {
720 rc = osmo_daemonize();
721 if (rc < 0) {
722 perror("Error during daemonize");
723 exit(1);
724 }
725 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200726
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700727 while (!quit)
Harald Weltee72cf552016-04-28 07:18:49 +0200728 osmo_select_main(0);
Harald Weltee72cf552016-04-28 07:18:49 +0200729
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700730 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100731 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200732 log_fini();
733
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700734 /**
735 * Report the heap state of root context, then free,
736 * so both ASAN and Valgrind are happy...
737 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700738 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700739 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700740
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700741 /* FIXME: VTY code still uses NULL-context */
742 talloc_free(tall_vty_ctx);
743
744 /**
745 * Report the heap state of NULL context, then free,
746 * so both ASAN and Valgrind are happy...
747 */
748 talloc_report_full(NULL, stderr);
749 talloc_disable_null_tracking();
750
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700751 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200752}