blob: 19cfebbe761778aa8466f7f89310a634d98fb5f6 [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>
Max20ddfdb2019-02-18 13:12:27 +010026#include <osmocom/core/stats.h>
Harald Weltee72cf552016-04-28 07:18:49 +020027#include <osmocom/core/logging.h>
28#include <osmocom/core/application.h>
29#include <osmocom/gsm/gsup.h>
Neels Hofmeyr7685a782017-01-30 23:30:26 +010030#include <osmocom/vty/vty.h>
31#include <osmocom/vty/command.h>
32#include <osmocom/vty/telnet_interface.h>
33#include <osmocom/vty/ports.h>
Max372868b2017-03-02 12:12:00 +010034#include <osmocom/ctrl/control_vty.h>
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +020035#include <osmocom/gsm/apn.h>
Oliver Smith783ac812018-12-17 11:34:51 +010036#include <osmocom/gsm/gsm48_ie.h>
Harald Weltee72cf552016-04-28 07:18:49 +020037
38#include "db.h"
Maxd4bebbd2017-03-02 12:00:19 +010039#include "hlr.h"
Max372868b2017-03-02 12:12:00 +010040#include "ctrl.h"
Harald Weltee72cf552016-04-28 07:18:49 +020041#include "logging.h"
42#include "gsup_server.h"
Harald Weltee687be52016-05-03 18:49:27 +020043#include "gsup_router.h"
Harald Weltee72cf552016-04-28 07:18:49 +020044#include "rand.h"
Maxea8b0d42017-02-14 16:53:04 +010045#include "luop.h"
Neels Hofmeyr7685a782017-01-30 23:30:26 +010046#include "hlr_vty.h"
Harald Weltebb779392018-06-16 20:21:10 +020047#include "hlr_ussd.h"
Harald Weltee72cf552016-04-28 07:18:49 +020048
Harald Welted5807b82018-07-29 12:27:41 +020049struct hlr *g_hlr;
Oliver Smithc5044cf2019-02-26 16:32:57 +010050static void *hlr_ctx = NULL;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070051static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020052
Stefan Sperlingf1622522018-04-09 11:39:16 +020053/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
54 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020055 * \param[in] subscr A subscriber we have new data to send for.
56 */
57void
58osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
59{
60 struct osmo_gsup_conn *co;
61
Neels Hofmeyre66e5252018-09-28 02:59:19 +020062 if (g_hlr->gs == NULL) {
63 LOGP(DLGSUP, LOGL_DEBUG,
64 "IMSI %s: NOT Notifying peers of subscriber data change,"
65 " there is no GSUP server\n",
66 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020067 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020068 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020069
70 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020071 struct osmo_gsup_message gsup = { };
72 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
73 uint8_t apn[APN_MAXLEN];
74 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020075 uint8_t *peer;
76 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020077 size_t peer_strlen;
78 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020079 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020080
Neels Hofmeyre66e5252018-09-28 02:59:19 +020081 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020082 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020083 peer_compare = subscr->sgsn_number;
84 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020085 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020086 peer_compare = subscr->vlr_number;
87 } else {
88 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +020089 continue;
90 }
91
Neels Hofmeyre66e5252018-09-28 02:59:19 +020092 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
93 if (peer_len < 0) {
94 LOGP(DLGSUP, LOGL_ERROR,
95 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
96 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
97 co && co->conn && co->conn->server? co->conn->server->port : 0);
98 continue;
99 }
100
101 peer_strlen = strnlen((const char*)peer, peer_len);
102 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
103 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
104 /* I hope peer is always nul terminated... */
105 if (peer_strlen < peer_len)
106 LOGP(DLGSUP, LOGL_DEBUG,
107 "IMSI %s: subscriber change: skipping %s peer %s\n",
108 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
109 osmo_quote_str((char*)peer, -1));
110 continue;
111 }
112
113 LOGP(DLGSUP, LOGL_DEBUG,
114 "IMSI %s: subscriber change: notifying %s peer %s\n",
115 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
116 osmo_quote_str(peer_compare, -1));
117
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200118 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
119 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200120 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200121 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200122 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200123 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
124 co && co->conn && co->conn->server? co->conn->server->port : 0);
125 continue;
126 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200127
Stefan Sperling93c5b102018-04-10 19:26:14 +0200128 /* Send ISD to MSC/SGSN */
129 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE");
130 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200131 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200132 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
133 "for %s:%u\n", subscr->imsi,
134 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
135 co && co->conn && co->conn->server? co->conn->server->port : 0);
136 continue;
137 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200138 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200139
Stefan Sperling93c5b102018-04-10 19:26:14 +0200140 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
141 LOGP(DMAIN, LOGL_ERROR,
142 "IMSI='%s': Cannot notify GSUP client; send operation failed "
143 "for %s:%u\n", subscr->imsi,
144 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
145 co && co->conn && co->conn->server? co->conn->server->port : 0);
146 continue;
147 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200148 }
149}
150
Harald Weltee687be52016-05-03 18:49:27 +0200151/***********************************************************************
152 * Send Auth Info handling
153 ***********************************************************************/
154
Harald Weltee72cf552016-04-28 07:18:49 +0200155/* process an incoming SAI request */
156static int rx_send_auth_info(struct osmo_gsup_conn *conn,
Maxd4bebbd2017-03-02 12:00:19 +0100157 const struct osmo_gsup_message *gsup,
158 struct db_context *dbc)
Harald Weltee72cf552016-04-28 07:18:49 +0200159{
160 struct osmo_gsup_message gsup_out;
161 struct msgb *msg_out;
162 int rc;
163
164 /* initialize return message structure */
165 memset(&gsup_out, 0, sizeof(gsup_out));
Harald Weltee72cf552016-04-28 07:18:49 +0200166 memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
167
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100168 rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
169 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200170 ARRAY_SIZE(gsup_out.auth_vectors),
Harald Welte9be0d2f2016-06-10 17:34:02 +0200171 gsup->rand, gsup->auts);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100172 if (rc <= 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200173 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100174 switch (rc) {
175 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100176 /* 0 means "0 tuples generated", which shouldn't happen.
177 * Treat the same as "no auth data". */
178 case -ENOKEY:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100179 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
180 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
181 gsup->imsi);
182 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
183 break;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100184 case -ENOENT:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100185 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100186 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
187 break;
188 default:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100189 LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100190 gsup_out.cause = GMM_CAUSE_NET_FAIL;
191 break;
192 }
Harald Welte15db8262016-05-05 16:50:39 +0200193 } else {
194 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
195 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200196 }
197
Harald Weltee687be52016-05-03 18:49:27 +0200198 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
Harald Weltee72cf552016-04-28 07:18:49 +0200199 osmo_gsup_encode(msg_out, &gsup_out);
200 return osmo_gsup_conn_send(conn, msg_out);
201}
202
Harald Weltee687be52016-05-03 18:49:27 +0200203/***********************************************************************
204 * LU Operation State / Structure
205 ***********************************************************************/
206
207static LLIST_HEAD(g_lu_ops);
208
Harald Weltee687be52016-05-03 18:49:27 +0200209/*! Receive Cancel Location Result from old VLR/SGSN */
210void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
Maxea8b0d42017-02-14 16:53:04 +0100211 const struct osmo_gsup_message *gsup)
Harald Weltee687be52016-05-03 18:49:27 +0200212{
213 OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
214 /* FIXME: Check for spoofing */
215
216 osmo_timer_del(&luop->timer);
217
218 /* FIXME */
219
220 lu_op_tx_insert_subscr_data(luop);
221}
222
Harald Weltee687be52016-05-03 18:49:27 +0200223/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
224static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
225 const struct osmo_gsup_message *gsup)
226{
227 OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
228 /* FIXME: Check for spoofing */
229
230 osmo_timer_del(&luop->timer);
231
232 /* Subscriber_Present_HLR */
233 /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
234
235 /* Send final ACK towards inquiring VLR/SGSN */
236 lu_op_tx_ack(luop);
237}
238
239/*! Receive GSUP message for given \ref lu_operation */
240void lu_op_rx_gsup(struct lu_operation *luop,
241 const struct osmo_gsup_message *gsup)
242{
243 switch (gsup->message_type) {
244 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
245 /* FIXME */
246 break;
247 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
248 lu_op_rx_insert_subscr_data_ack(luop, gsup);
249 break;
250 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
251 /* FIXME */
252 break;
253 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
254 lu_op_rx_cancel_old_ack(luop, gsup);
255 break;
256 default:
257 LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
258 gsup->message_type);
259 break;
260 }
261}
262
Harald Weltee687be52016-05-03 18:49:27 +0200263/*! Receive Update Location Request, creates new \ref lu_operation */
264static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
265 const struct osmo_gsup_message *gsup)
266{
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200267 struct hlr_subscriber *subscr;
Maxea8b0d42017-02-14 16:53:04 +0100268 struct lu_operation *luop = lu_op_alloc_conn(conn);
269 if (!luop) {
Harald Weltee687be52016-05-03 18:49:27 +0200270 LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n");
Maxea8b0d42017-02-14 16:53:04 +0100271 return -EINVAL;
Harald Weltee687be52016-05-03 18:49:27 +0200272 }
273
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200274 subscr = &luop->subscr;
275
Harald Weltee687be52016-05-03 18:49:27 +0200276 lu_op_statechg(luop, LU_S_LU_RECEIVED);
Maxea8b0d42017-02-14 16:53:04 +0100277
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200278 switch (gsup->cn_domain) {
279 case OSMO_GSUP_CN_DOMAIN_CS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200280 conn->supports_cs = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200281 break;
282 default:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200283 /* The client didn't send a CN_DOMAIN IE; assume packet-switched in
284 * accordance with the GSUP spec in osmo-hlr's user manual (section
285 * 11.6.15 "CN Domain" says "if no CN Domain IE is present within
286 * a request, the PS Domain is assumed." */
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200287 case OSMO_GSUP_CN_DOMAIN_PS:
Stefan Sperling93c5b102018-04-10 19:26:14 +0200288 conn->supports_ps = true;
Stefan Sperling1cb48922018-05-03 14:05:56 +0200289 luop->is_ps = true;
Neels Hofmeyr6cee7992018-09-28 02:53:22 +0200290 break;
Stefan Sperling93c5b102018-04-10 19:26:14 +0200291 }
Harald Weltee687be52016-05-03 18:49:27 +0200292 llist_add(&luop->list, &g_lu_ops);
293
294 /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */
295
296 /* check if subscriber is known at all */
Maxd4bebbd2017-03-02 12:00:19 +0100297 if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) {
Harald Weltee687be52016-05-03 18:49:27 +0200298 /* Send Error back: Subscriber Unknown in HLR */
Harald Weltebd0d5bf2017-11-06 03:55:02 +0900299 osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi));
Harald Weltee687be52016-05-03 18:49:27 +0200300 lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN);
301 return 0;
302 }
303
Harald Welte99909272016-05-05 18:24:15 +0200304 /* Check if subscriber is generally permitted on CS or PS
305 * service (as requested) */
Maxea8b0d42017-02-14 16:53:04 +0100306 if (!luop->is_ps && !luop->subscr.nam_cs) {
Harald Weltee687be52016-05-03 18:49:27 +0200307 lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED);
308 return 0;
Maxea8b0d42017-02-14 16:53:04 +0100309 } else if (luop->is_ps && !luop->subscr.nam_ps) {
Harald Weltee687be52016-05-03 18:49:27 +0200310 lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED);
311 return 0;
312 }
313
314 /* TODO: Set subscriber tracing = deactive in VLR/SGSN */
315
316#if 0
317 /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
318 if (luop->is_ps == false &&
319 strcmp(subscr->vlr_number, vlr_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200320 lu_op_tx_cancel_old(luop);
321 } else if (luop->is_ps == true &&
322 strcmp(subscr->sgsn_number, sgsn_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200323 lu_op_tx_cancel_old(luop);
324 } else
325#endif
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200326
327 /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */
328 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing %s = %s\n",
329 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number",
330 osmo_quote_str((const char*)luop->peer, -1));
331 if (db_subscr_lu(g_hlr->dbc, subscr->id, (const char *)luop->peer, luop->is_ps))
332 LOGP(DAUC, LOGL_ERROR, "IMSI='%s': Cannot update %s in the database\n",
333 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number");
334
Oliver Smith66106c02019-01-09 12:05:15 +0100335 /* TODO: Subscriber allowed to roam in PLMN? */
336 /* TODO: Update RoutingInfo */
337 /* TODO: Reset Flag MS Purged (cs/ps) */
338 /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
339 lu_op_tx_insert_subscr_data(luop);
340
Harald Weltee687be52016-05-03 18:49:27 +0200341 return 0;
342}
343
Harald Welteb18f0e02016-05-05 21:03:03 +0200344static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
345 const struct osmo_gsup_message *gsup)
346{
347 struct osmo_gsup_message gsup_reply = {0};
348 struct msgb *msg_out;
349 bool is_ps = false;
350 int rc;
351
352 LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
353 is_ps ? "PS" : "CS");
354
355 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
356
357 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
358 is_ps = true;
359
360 /* FIXME: check if the VLR that sends the purge is the same that
361 * we have on record. Only update if yes */
362
363 /* Perform the actual update of the DB */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200364 rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200365
Harald Welte3f2a9a22018-03-01 23:35:35 +0100366 if (rc == 0)
Harald Welteb18f0e02016-05-05 21:03:03 +0200367 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
Harald Welte3f2a9a22018-03-01 23:35:35 +0100368 else if (rc == -ENOENT) {
Harald Welteb18f0e02016-05-05 21:03:03 +0200369 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
370 gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
371 } else {
372 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
373 gsup_reply.cause = GMM_CAUSE_NET_FAIL;
374 }
375
376 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
377 osmo_gsup_encode(msg_out, &gsup_reply);
378 return osmo_gsup_conn_send(conn, msg_out);
379}
380
Harald Weltea1d3b042018-06-11 20:28:35 +0200381static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
382 enum osmo_gsup_message_type type_in, uint8_t err_cause)
383{
Oliver Smithf9cf1802019-02-04 11:20:08 +0100384 int type_err = OSMO_GSUP_TO_MSGT_ERROR(type_in);
Harald Weltea1d3b042018-06-11 20:28:35 +0200385 struct osmo_gsup_message gsup_reply = {0};
386 struct msgb *msg_out;
387
Harald Weltea1d3b042018-06-11 20:28:35 +0200388 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
389 gsup_reply.message_type = type_err;
390 gsup_reply.cause = err_cause;
391 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response");
392 OSMO_ASSERT(msg_out);
393 osmo_gsup_encode(msg_out, &gsup_reply);
394 LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
395 return osmo_gsup_conn_send(conn, msg_out);
396}
397
Oliver Smith783ac812018-12-17 11:34:51 +0100398static int rx_check_imei_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup)
399{
400 struct osmo_gsup_message gsup_reply = {0};
401 struct msgb *msg_out;
402 char imei[GSM23003_IMEI_NUM_DIGITS+1] = {0};
403
404 /* Encoded IMEI length check */
405 if (!gsup->imei_enc || gsup->imei_enc_len < 1 || gsup->imei_enc[0] >= sizeof(imei)) {
406 LOGP(DMAIN, LOGL_ERROR, "%s: wrong encoded IMEI length\n", gsup->imsi);
407 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
408 return -1;
409 }
410
411 /* Decode IMEI */
412 if (gsm48_decode_bcd_number(imei, sizeof(imei), gsup->imei_enc, 0) < 0) {
413 LOGP(DMAIN, LOGL_ERROR, "%s: failed to decode IMEI\n", gsup->imsi);
414 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
415 return -1;
416 }
417
Oliver Smith851814a2019-01-11 15:30:21 +0100418 /* Save in DB if desired */
419 if (g_hlr->store_imei) {
420 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei);
421 if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) {
422 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
423 return -1;
424 }
425 } else {
426 /* Check if subscriber exists and print IMEI */
427 LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei);
428 struct hlr_subscriber subscr;
429 if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) {
430 gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO);
431 return -1;
432 }
433 }
Oliver Smith783ac812018-12-17 11:34:51 +0100434
435 /* Accept all IMEIs */
436 gsup_reply.imei_result = OSMO_GSUP_IMEI_RESULT_ACK;
437 gsup_reply.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT;
438 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP Check_IMEI response");
439 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
440 osmo_gsup_encode(msg_out, &gsup_reply);
441 return osmo_gsup_conn_send(conn, msg_out);
442}
443
Harald Weltee72cf552016-04-28 07:18:49 +0200444static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
445{
446 static struct osmo_gsup_message gsup;
447 int rc;
448
Harald Weltee687be52016-05-03 18:49:27 +0200449 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200450 if (rc < 0) {
451 LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
452 return rc;
453 }
454
Harald Weltea1d3b042018-06-11 20:28:35 +0200455 /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
456 * digits is impossible. Even 5 digits is a highly theoretical case */
Neels Hofmeyr9f6e5582019-04-03 18:16:50 +0200457 if (strlen(gsup.imsi) < 5) {
458 LOGP(DMAIN, LOGL_ERROR, "IMSI too short: %s\n", osmo_quote_str(gsup.imsi, -1));
Harald Weltea1d3b042018-06-11 20:28:35 +0200459 return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
Neels Hofmeyr9f6e5582019-04-03 18:16:50 +0200460 }
Harald Weltea1d3b042018-06-11 20:28:35 +0200461
Harald Weltee72cf552016-04-28 07:18:49 +0200462 switch (gsup.message_type) {
463 /* requests sent to us */
464 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Maxd4bebbd2017-03-02 12:00:19 +0100465 rx_send_auth_info(conn, &gsup, g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200466 break;
467 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Harald Weltee687be52016-05-03 18:49:27 +0200468 rx_upd_loc_req(conn, &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200469 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200470 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
471 rx_purge_ms_req(conn, &gsup);
472 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200473 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100474 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
475 LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
476 "for IMSI %s\n", gsup.imsi);
477 break;
478 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
479 LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
480 gsup.imsi);
481 break;
Harald Weltebb779392018-06-16 20:21:10 +0200482 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
483 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
484 rx_proc_ss_req(conn, &gsup);
485 break;
486 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
487 rx_proc_ss_error(conn, &gsup);
488 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200489 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200490 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200491 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
492 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
493 {
Maxea8b0d42017-02-14 16:53:04 +0100494 struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
495 &g_lu_ops);
Harald Weltee687be52016-05-03 18:49:27 +0200496 if (!luop) {
Maxaa0fefd2017-02-16 12:25:22 +0100497 LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
498 "unknown IMSI %s\n",
499 osmo_gsup_message_type_name(gsup.message_type),
Harald Weltee687be52016-05-03 18:49:27 +0200500 gsup.imsi);
501 break;
502 }
503 lu_op_rx_gsup(luop, &gsup);
504 }
Harald Weltee72cf552016-04-28 07:18:49 +0200505 break;
Oliver Smith783ac812018-12-17 11:34:51 +0100506 case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST:
507 rx_check_imei_req(conn, &gsup);
508 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200509 default:
Maxaa0fefd2017-02-16 12:25:22 +0100510 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
511 osmo_gsup_message_type_name(gsup.message_type));
Harald Weltee72cf552016-04-28 07:18:49 +0200512 break;
513 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200514 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200515 return 0;
516}
517
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100518static void print_usage()
519{
520 printf("Usage: osmo-hlr\n");
521}
522
523static void print_help()
524{
525 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100526 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100527 printf(" -l --database db-name The database to use.\n");
528 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
529 printf(" -D --daemonize Fork the process into a background daemon.\n");
530 printf(" -s --disable-color Do not print ANSI colors in the log\n");
531 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
532 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100533 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100534 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100535}
536
537static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100538 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100539 const char *db_file;
540 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100541 bool db_upgrade;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100542} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100543 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100544 .db_file = "hlr.db",
545 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100546 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100547};
548
549static void handle_options(int argc, char **argv)
550{
551 while (1) {
552 int option_index = 0, c;
553 static struct option long_options[] = {
554 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100555 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100556 {"database", 1, 0, 'l'},
557 {"debug", 1, 0, 'd'},
558 {"daemonize", 0, 0, 'D'},
559 {"disable-color", 0, 0, 's'},
560 {"log-level", 1, 0, 'e'},
561 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100562 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100563 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100564 {0, 0, 0, 0}
565 };
566
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100567 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100568 long_options, &option_index);
569 if (c == -1)
570 break;
571
572 switch (c) {
573 case 'h':
574 print_usage();
575 print_help();
576 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100577 case 'c':
578 cmdline_opts.config_file = optarg;
579 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100580 case 'l':
581 cmdline_opts.db_file = optarg;
582 break;
583 case 'd':
584 log_parse_category_mask(osmo_stderr_target, optarg);
585 break;
586 case 'D':
587 cmdline_opts.daemonize = 1;
588 break;
589 case 's':
590 log_set_use_color(osmo_stderr_target, 0);
591 break;
592 case 'e':
593 log_set_log_level(osmo_stderr_target, atoi(optarg));
594 break;
595 case 'T':
596 log_set_print_timestamp(osmo_stderr_target, 1);
597 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100598 case 'U':
599 cmdline_opts.db_upgrade = true;
600 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100601 case 'V':
602 print_version(1);
603 exit(0);
604 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100605 default:
606 /* catch unknown options *as well as* missing arguments. */
607 fprintf(stderr, "Error in command line options. Exiting.\n");
608 exit(-1);
609 break;
610 }
611 }
612}
613
Harald Welteaabae9e2016-04-28 12:48:14 +0200614static void signal_hdlr(int signal)
615{
616 switch (signal) {
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700617 case SIGTERM:
Harald Welteaabae9e2016-04-28 12:48:14 +0200618 case SIGINT:
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700619 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700620 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200621 break;
622 case SIGUSR1:
623 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100624 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200625 break;
626 }
627}
Harald Weltee72cf552016-04-28 07:18:49 +0200628
Max372868b2017-03-02 12:12:00 +0100629static const char vlr_copyright[] =
630 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
631 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
632 "This is free software: you are free to change and redistribute it.\r\n"
633 "There is NO WARRANTY, to the extent permitted by law.\r\n";
634
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100635static struct vty_app_info vty_info = {
636 .name = "OsmoHLR",
637 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100638 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100639 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200640 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100641};
642
Harald Weltee72cf552016-04-28 07:18:49 +0200643int main(int argc, char **argv)
644{
Harald Weltee72cf552016-04-28 07:18:49 +0200645 int rc;
646
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700647 /* Track the use of talloc NULL memory contexts */
648 talloc_enable_null_tracking();
649
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100650 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
651 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100652 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200653
Maxd4bebbd2017-03-02 12:00:19 +0100654 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200655 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200656 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200657 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200658 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Maxd4bebbd2017-03-02 12:00:19 +0100659
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700660 /* Init default (call independent) SS session guard timeout value */
661 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
662
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200663 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200664 if (rc < 0) {
665 fprintf(stderr, "Error initializing logging\n");
666 exit(1);
667 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100668
Max20ddfdb2019-02-18 13:12:27 +0100669 osmo_stats_init(hlr_ctx);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100670 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100671 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100672 handle_options(argc, argv);
Harald Welted5807b82018-07-29 12:27:41 +0200673 hlr_vty_init(&hlr_log_info);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100674
675 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
676 if (rc < 0) {
677 LOGP(DMAIN, LOGL_FATAL,
678 "Failed to parse the config file: '%s'\n",
679 cmdline_opts.config_file);
680 return rc;
681 }
682
683 /* start telnet after reading config for vty_get_bind_addr() */
684 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
685 OSMO_VTY_PORT_HLR);
686 if (rc < 0)
687 return rc;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100688
Harald Weltee72cf552016-04-28 07:18:49 +0200689 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
690
691 rc = rand_init();
692 if (rc < 0) {
693 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
694 exit(1);
695 }
696
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100697 g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100698 if (!g_hlr->dbc) {
Harald Weltee72cf552016-04-28 07:18:49 +0200699 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
700 exit(1);
701 }
702
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200703 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Harald Welte32acace2018-06-16 17:07:28 +0200704 read_cb, &g_lu_ops, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100705 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200706 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
707 exit(1);
708 }
709
Max372868b2017-03-02 12:12:00 +0100710 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200711 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100712
Harald Welteaabae9e2016-04-28 12:48:14 +0200713 osmo_init_ignore_signals();
714 signal(SIGINT, &signal_hdlr);
Vadim Yanitskiyee7c0cb2019-03-19 18:10:51 +0700715 signal(SIGTERM, &signal_hdlr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200716 signal(SIGUSR1, &signal_hdlr);
717
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100718 if (cmdline_opts.daemonize) {
719 rc = osmo_daemonize();
720 if (rc < 0) {
721 perror("Error during daemonize");
722 exit(1);
723 }
724 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200725
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700726 while (!quit)
Harald Weltee72cf552016-04-28 07:18:49 +0200727 osmo_select_main(0);
Harald Weltee72cf552016-04-28 07:18:49 +0200728
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700729 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100730 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200731 log_fini();
732
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700733 /**
734 * Report the heap state of root context, then free,
735 * so both ASAN and Valgrind are happy...
736 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700737 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700738 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700739
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700740 /* FIXME: VTY code still uses NULL-context */
741 talloc_free(tall_vty_ctx);
742
743 /**
744 * Report the heap state of NULL context, then free,
745 * so both ASAN and Valgrind are happy...
746 */
747 talloc_report_full(NULL, stderr);
748 talloc_disable_null_tracking();
749
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700750 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200751}