blob: 1f5e63d3a7cd290b74840a495d8dcbcfad42bfe4 [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>
Harald Weltee72cf552016-04-28 07:18:49 +020035
36#include "db.h"
Maxd4bebbd2017-03-02 12:00:19 +010037#include "hlr.h"
Max372868b2017-03-02 12:12:00 +010038#include "ctrl.h"
Harald Weltee72cf552016-04-28 07:18:49 +020039#include "logging.h"
40#include "gsup_server.h"
Harald Weltee687be52016-05-03 18:49:27 +020041#include "gsup_router.h"
Harald Weltee72cf552016-04-28 07:18:49 +020042#include "rand.h"
Maxea8b0d42017-02-14 16:53:04 +010043#include "luop.h"
Neels Hofmeyr7685a782017-01-30 23:30:26 +010044#include "hlr_vty.h"
Harald Weltebb779392018-06-16 20:21:10 +020045#include "hlr_ussd.h"
Harald Weltee72cf552016-04-28 07:18:49 +020046
Harald Welted5807b82018-07-29 12:27:41 +020047struct hlr *g_hlr;
Vadim Yanitskiy527d9342018-07-30 02:42:25 +070048static int quit = 0;
Harald Weltee72cf552016-04-28 07:18:49 +020049
Stefan Sperlingf1622522018-04-09 11:39:16 +020050/* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients.
51 *
Stefan Sperlingf1622522018-04-09 11:39:16 +020052 * \param[in] subscr A subscriber we have new data to send for.
53 */
54void
55osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr)
56{
57 struct osmo_gsup_conn *co;
58
Neels Hofmeyre66e5252018-09-28 02:59:19 +020059 if (g_hlr->gs == NULL) {
60 LOGP(DLGSUP, LOGL_DEBUG,
61 "IMSI %s: NOT Notifying peers of subscriber data change,"
62 " there is no GSUP server\n",
63 subscr->imsi);
Stefan Sperlingf1622522018-04-09 11:39:16 +020064 return;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020065 }
Stefan Sperlingf1622522018-04-09 11:39:16 +020066
67 llist_for_each_entry(co, &g_hlr->gs->clients, list) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020068 struct osmo_gsup_message gsup = { };
69 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
70 uint8_t apn[APN_MAXLEN];
71 struct msgb *msg_out;
Stefan Sperling93c5b102018-04-10 19:26:14 +020072 uint8_t *peer;
73 int peer_len;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020074 size_t peer_strlen;
75 const char *peer_compare;
Stefan Sperlingf83432c2018-05-03 14:26:59 +020076 enum osmo_gsup_cn_domain cn_domain;
Stefan Sperling93c5b102018-04-10 19:26:14 +020077
Neels Hofmeyre66e5252018-09-28 02:59:19 +020078 if (co->supports_ps) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020079 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020080 peer_compare = subscr->sgsn_number;
81 } else if (co->supports_cs) {
Stefan Sperlingf83432c2018-05-03 14:26:59 +020082 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Neels Hofmeyre66e5252018-09-28 02:59:19 +020083 peer_compare = subscr->vlr_number;
84 } else {
85 /* We have not yet received a location update from this GSUP client.*/
Stefan Sperlingf83432c2018-05-03 14:26:59 +020086 continue;
87 }
88
Neels Hofmeyre66e5252018-09-28 02:59:19 +020089 peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR);
90 if (peer_len < 0) {
91 LOGP(DLGSUP, LOGL_ERROR,
92 "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi,
93 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
94 co && co->conn && co->conn->server? co->conn->server->port : 0);
95 continue;
96 }
97
98 peer_strlen = strnlen((const char*)peer, peer_len);
99 if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) {
100 /* Mismatch. The subscriber is not subscribed with this GSUP client. */
101 /* I hope peer is always nul terminated... */
102 if (peer_strlen < peer_len)
103 LOGP(DLGSUP, LOGL_DEBUG,
104 "IMSI %s: subscriber change: skipping %s peer %s\n",
105 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
106 osmo_quote_str((char*)peer, -1));
107 continue;
108 }
109
110 LOGP(DLGSUP, LOGL_DEBUG,
111 "IMSI %s: subscriber change: notifying %s peer %s\n",
112 subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS",
113 osmo_quote_str(peer_compare, -1));
114
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200115 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
116 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200117 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200118 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
Stefan Sperling93c5b102018-04-10 19:26:14 +0200119 "for %s:%u\n", subscr->imsi,
Stefan Sperlingf1622522018-04-09 11:39:16 +0200120 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
121 co && co->conn && co->conn->server? co->conn->server->port : 0);
122 continue;
123 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200124
Stefan Sperling93c5b102018-04-10 19:26:14 +0200125 /* Send ISD to MSC/SGSN */
126 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE");
127 if (msg_out == NULL) {
Neels Hofmeyre66e5252018-09-28 02:59:19 +0200128 LOGP(DLGSUP, LOGL_ERROR,
Stefan Sperling93c5b102018-04-10 19:26:14 +0200129 "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer "
130 "for %s:%u\n", subscr->imsi,
131 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
132 co && co->conn && co->conn->server? co->conn->server->port : 0);
133 continue;
134 }
Stefan Sperling93c5b102018-04-10 19:26:14 +0200135 osmo_gsup_encode(msg_out, &gsup);
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200136
Stefan Sperling93c5b102018-04-10 19:26:14 +0200137 if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) {
138 LOGP(DMAIN, LOGL_ERROR,
139 "IMSI='%s': Cannot notify GSUP client; send operation failed "
140 "for %s:%u\n", subscr->imsi,
141 co && co->conn && co->conn->server? co->conn->server->addr : "unset",
142 co && co->conn && co->conn->server? co->conn->server->port : 0);
143 continue;
144 }
Stefan Sperlingf1622522018-04-09 11:39:16 +0200145 }
146}
147
Harald Weltee687be52016-05-03 18:49:27 +0200148/***********************************************************************
149 * Send Auth Info handling
150 ***********************************************************************/
151
Harald Weltee72cf552016-04-28 07:18:49 +0200152/* process an incoming SAI request */
153static int rx_send_auth_info(struct osmo_gsup_conn *conn,
Maxd4bebbd2017-03-02 12:00:19 +0100154 const struct osmo_gsup_message *gsup,
155 struct db_context *dbc)
Harald Weltee72cf552016-04-28 07:18:49 +0200156{
157 struct osmo_gsup_message gsup_out;
158 struct msgb *msg_out;
159 int rc;
160
161 /* initialize return message structure */
162 memset(&gsup_out, 0, sizeof(gsup_out));
Harald Weltee72cf552016-04-28 07:18:49 +0200163 memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi));
164
Neels Hofmeyrcab2fcd2017-03-15 00:07:43 +0100165 rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind,
166 gsup_out.auth_vectors,
Harald Weltee72cf552016-04-28 07:18:49 +0200167 ARRAY_SIZE(gsup_out.auth_vectors),
Harald Welte9be0d2f2016-06-10 17:34:02 +0200168 gsup->rand, gsup->auts);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100169 if (rc <= 0) {
Harald Weltee72cf552016-04-28 07:18:49 +0200170 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR;
Neels Hofmeyr671db902017-11-22 20:38:19 +0100171 switch (rc) {
172 case 0:
Neels Hofmeyrbd1dca02017-11-23 15:25:30 +0100173 /* 0 means "0 tuples generated", which shouldn't happen.
174 * Treat the same as "no auth data". */
175 case -ENOKEY:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100176 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;"
177 " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n",
178 gsup->imsi);
179 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
180 break;
Neels Hofmeyr33cbde92017-11-22 20:39:59 +0100181 case -ENOENT:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100182 LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100183 gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN;
184 break;
185 default:
Neels Hofmeyrab4d5092017-11-23 15:31:12 +0100186 LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi);
Neels Hofmeyr671db902017-11-22 20:38:19 +0100187 gsup_out.cause = GMM_CAUSE_NET_FAIL;
188 break;
189 }
Harald Welte15db8262016-05-05 16:50:39 +0200190 } else {
191 gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT;
192 gsup_out.num_auth_vectors = rc;
Harald Weltee72cf552016-04-28 07:18:49 +0200193 }
194
Harald Weltee687be52016-05-03 18:49:27 +0200195 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
Harald Weltee72cf552016-04-28 07:18:49 +0200196 osmo_gsup_encode(msg_out, &gsup_out);
197 return osmo_gsup_conn_send(conn, msg_out);
198}
199
Harald Weltee687be52016-05-03 18:49:27 +0200200/***********************************************************************
201 * LU Operation State / Structure
202 ***********************************************************************/
203
204static LLIST_HEAD(g_lu_ops);
205
Harald Weltee687be52016-05-03 18:49:27 +0200206/*! Receive Cancel Location Result from old VLR/SGSN */
207void lu_op_rx_cancel_old_ack(struct lu_operation *luop,
Maxea8b0d42017-02-14 16:53:04 +0100208 const struct osmo_gsup_message *gsup)
Harald Weltee687be52016-05-03 18:49:27 +0200209{
210 OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT);
211 /* FIXME: Check for spoofing */
212
213 osmo_timer_del(&luop->timer);
214
215 /* FIXME */
216
217 lu_op_tx_insert_subscr_data(luop);
218}
219
Harald Weltee687be52016-05-03 18:49:27 +0200220/*! Receive Insert Subscriber Data Result from new VLR/SGSN */
221static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop,
222 const struct osmo_gsup_message *gsup)
223{
224 OSMO_ASSERT(luop->state == LU_S_ISD_SENT);
225 /* FIXME: Check for spoofing */
226
227 osmo_timer_del(&luop->timer);
228
229 /* Subscriber_Present_HLR */
230 /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */
231
232 /* Send final ACK towards inquiring VLR/SGSN */
233 lu_op_tx_ack(luop);
234}
235
236/*! Receive GSUP message for given \ref lu_operation */
237void lu_op_rx_gsup(struct lu_operation *luop,
238 const struct osmo_gsup_message *gsup)
239{
240 switch (gsup->message_type) {
241 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
242 /* FIXME */
243 break;
244 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
245 lu_op_rx_insert_subscr_data_ack(luop, gsup);
246 break;
247 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
248 /* FIXME */
249 break;
250 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
251 lu_op_rx_cancel_old_ack(luop, gsup);
252 break;
253 default:
254 LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n",
255 gsup->message_type);
256 break;
257 }
258}
259
Harald Weltee687be52016-05-03 18:49:27 +0200260/*! Receive Update Location Request, creates new \ref lu_operation */
261static int rx_upd_loc_req(struct osmo_gsup_conn *conn,
262 const struct osmo_gsup_message *gsup)
263{
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200264 struct hlr_subscriber *subscr;
Maxea8b0d42017-02-14 16:53:04 +0100265 struct lu_operation *luop = lu_op_alloc_conn(conn);
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +0100266 int i;
267 bool allowed;
268
Maxea8b0d42017-02-14 16:53:04 +0100269 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
Neels Hofmeyr2fb33f12018-12-26 01:49:53 +0100314 /* Check if any available RAT type is allowed. See 3GPP TS 29.010 3.2 'Routeing area updating' and 3.8 'Location
315 * update' for the "No Suitable cells in location area" error code. */
316 allowed = false;
317 LOGP(DAUC, LOGL_DEBUG, "LU: IMSI='%s' on %s sent RAT types: %zu\n", subscr->imsi,
318 gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_CS ? "CS" : "PS", gsup->rat_types_len);
319 for (i = 0; i < gsup->rat_types_len; i++) {
320 enum osmo_rat_type rat = gsup->rat_types[i];
321 if (rat <= 0 || rat >= OSMO_RAT_COUNT) {
322 lu_op_tx_error(luop, GMM_CAUSE_COND_IE_ERR);
323 return 0;
324 }
325 if (luop->subscr.rat_types[rat]) {
326 allowed = true;
327 LOGP(DAUC, LOGL_DEBUG, "LU: IMSI='%s' allowed on %s\n",
328 subscr->imsi, osmo_rat_type_name(rat));
329 } else {
330 LOGP(DAUC, LOGL_DEBUG, "LU: IMSI='%s' not allowed on %s\n",
331 subscr->imsi, osmo_rat_type_name(rat));
332 }
333 }
334 if (!allowed && gsup->rat_types_len > 0) {
335 LOGP(DAUC, LOGL_DEBUG, "ISMI='%s' not allowed on %s%s\n",
336 subscr->imsi, osmo_rat_type_name(gsup->rat_types[0]),
337 gsup->rat_types_len > 1 ? " (nor on the other available RAT types)" : "");
338 lu_op_tx_error(luop, GMM_CAUSE_NO_SUIT_CELL_IN_LA);
339 return 0;
340 }
341
Harald Weltee687be52016-05-03 18:49:27 +0200342 /* TODO: Set subscriber tracing = deactive in VLR/SGSN */
343
344#if 0
345 /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */
346 if (luop->is_ps == false &&
347 strcmp(subscr->vlr_number, vlr_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200348 lu_op_tx_cancel_old(luop);
349 } else if (luop->is_ps == true &&
350 strcmp(subscr->sgsn_number, sgsn_number)) {
Harald Weltee687be52016-05-03 18:49:27 +0200351 lu_op_tx_cancel_old(luop);
352 } else
353#endif
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200354
355 /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */
356 LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing %s = %s\n",
357 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number",
358 osmo_quote_str((const char*)luop->peer, -1));
Neels Hofmeyr0f9daa62018-12-29 03:28:38 +0100359 if (db_subscr_lu(g_hlr->dbc, subscr->id, (const char *)luop->peer, luop->is_ps,
360 gsup->rat_types, gsup->rat_types_len))
Neels Hofmeyrcb364bb2018-09-28 01:22:15 +0200361 LOGP(DAUC, LOGL_ERROR, "IMSI='%s': Cannot update %s in the database\n",
362 subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number");
363
Harald Weltee687be52016-05-03 18:49:27 +0200364 {
365 /* TODO: Subscriber allowed to roam in PLMN? */
366 /* TODO: Update RoutingInfo */
367 /* TODO: Reset Flag MS Purged (cs/ps) */
368 /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */
369 lu_op_tx_insert_subscr_data(luop);
370 }
371 return 0;
372}
373
Harald Welteb18f0e02016-05-05 21:03:03 +0200374static int rx_purge_ms_req(struct osmo_gsup_conn *conn,
375 const struct osmo_gsup_message *gsup)
376{
377 struct osmo_gsup_message gsup_reply = {0};
378 struct msgb *msg_out;
379 bool is_ps = false;
380 int rc;
381
382 LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi,
383 is_ps ? "PS" : "CS");
384
385 memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi));
386
387 if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS)
388 is_ps = true;
389
390 /* FIXME: check if the VLR that sends the purge is the same that
391 * we have on record. Only update if yes */
392
393 /* Perform the actual update of the DB */
Neels Hofmeyre50121e2017-10-09 17:48:51 +0200394 rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps);
Harald Welteb18f0e02016-05-05 21:03:03 +0200395
Harald Welte3f2a9a22018-03-01 23:35:35 +0100396 if (rc == 0)
Harald Welteb18f0e02016-05-05 21:03:03 +0200397 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT;
Harald Welte3f2a9a22018-03-01 23:35:35 +0100398 else if (rc == -ENOENT) {
Harald Welteb18f0e02016-05-05 21:03:03 +0200399 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
400 gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN;
401 } else {
402 gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR;
403 gsup_reply.cause = GMM_CAUSE_NET_FAIL;
404 }
405
406 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response");
407 osmo_gsup_encode(msg_out, &gsup_reply);
408 return osmo_gsup_conn_send(conn, msg_out);
409}
410
Harald Weltea1d3b042018-06-11 20:28:35 +0200411static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi,
412 enum osmo_gsup_message_type type_in, uint8_t err_cause)
413{
414 int type_err = osmo_gsup_get_err_msg_type(type_in);
415 struct osmo_gsup_message gsup_reply = {0};
416 struct msgb *msg_out;
417
418 if (type_err < 0) {
419 LOGP(DMAIN, LOGL_ERROR, "unable to determine error response for %s\n",
420 osmo_gsup_message_type_name(type_in));
421 return type_err;
422 }
423
424 OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi);
425 gsup_reply.message_type = type_err;
426 gsup_reply.cause = err_cause;
427 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response");
428 OSMO_ASSERT(msg_out);
429 osmo_gsup_encode(msg_out, &gsup_reply);
430 LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err));
431 return osmo_gsup_conn_send(conn, msg_out);
432}
433
Harald Weltee72cf552016-04-28 07:18:49 +0200434static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg)
435{
436 static struct osmo_gsup_message gsup;
437 int rc;
438
Harald Weltee687be52016-05-03 18:49:27 +0200439 rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200440 if (rc < 0) {
441 LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc);
442 return rc;
443 }
444
Harald Weltea1d3b042018-06-11 20:28:35 +0200445 /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5
446 * digits is impossible. Even 5 digits is a highly theoretical case */
447 if (strlen(gsup.imsi) < 5)
448 return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO);
449
Harald Weltee72cf552016-04-28 07:18:49 +0200450 switch (gsup.message_type) {
451 /* requests sent to us */
452 case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST:
Maxd4bebbd2017-03-02 12:00:19 +0100453 rx_send_auth_info(conn, &gsup, g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200454 break;
455 case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST:
Harald Weltee687be52016-05-03 18:49:27 +0200456 rx_upd_loc_req(conn, &gsup);
Harald Weltee72cf552016-04-28 07:18:49 +0200457 break;
Harald Welteb18f0e02016-05-05 21:03:03 +0200458 case OSMO_GSUP_MSGT_PURGE_MS_REQUEST:
459 rx_purge_ms_req(conn, &gsup);
460 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200461 /* responses to requests sent by us */
Max9cacb6f2017-02-20 17:22:56 +0100462 case OSMO_GSUP_MSGT_DELETE_DATA_ERROR:
463 LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data "
464 "for IMSI %s\n", gsup.imsi);
465 break;
466 case OSMO_GSUP_MSGT_DELETE_DATA_RESULT:
467 LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n",
468 gsup.imsi);
469 break;
Harald Weltebb779392018-06-16 20:21:10 +0200470 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
471 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
472 rx_proc_ss_req(conn, &gsup);
473 break;
474 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
475 rx_proc_ss_error(conn, &gsup);
476 break;
Harald Weltee72cf552016-04-28 07:18:49 +0200477 case OSMO_GSUP_MSGT_INSERT_DATA_ERROR:
Harald Weltee72cf552016-04-28 07:18:49 +0200478 case OSMO_GSUP_MSGT_INSERT_DATA_RESULT:
Harald Weltee687be52016-05-03 18:49:27 +0200479 case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR:
480 case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT:
481 {
Maxea8b0d42017-02-14 16:53:04 +0100482 struct lu_operation *luop = lu_op_by_imsi(gsup.imsi,
483 &g_lu_ops);
Harald Weltee687be52016-05-03 18:49:27 +0200484 if (!luop) {
Maxaa0fefd2017-02-16 12:25:22 +0100485 LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for "
486 "unknown IMSI %s\n",
487 osmo_gsup_message_type_name(gsup.message_type),
Harald Weltee687be52016-05-03 18:49:27 +0200488 gsup.imsi);
489 break;
490 }
491 lu_op_rx_gsup(luop, &gsup);
492 }
Harald Weltee72cf552016-04-28 07:18:49 +0200493 break;
494 default:
Maxaa0fefd2017-02-16 12:25:22 +0100495 LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n",
496 osmo_gsup_message_type_name(gsup.message_type));
Harald Weltee72cf552016-04-28 07:18:49 +0200497 break;
498 }
Harald Welte5341b5d2016-04-28 12:48:39 +0200499 msgb_free(msg);
Harald Weltee72cf552016-04-28 07:18:49 +0200500 return 0;
501}
502
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100503static void print_usage()
504{
505 printf("Usage: osmo-hlr\n");
506}
507
508static void print_help()
509{
510 printf(" -h --help This text.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100511 printf(" -c --config-file filename The config file to use.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100512 printf(" -l --database db-name The database to use.\n");
513 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
514 printf(" -D --daemonize Fork the process into a background daemon.\n");
515 printf(" -s --disable-color Do not print ANSI colors in the log\n");
516 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
517 printf(" -e --log-level number Set a global loglevel.\n");
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100518 printf(" -U --db-upgrade Allow HLR database schema upgrades.\n");
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100519 printf(" -V --version Print the version of OsmoHLR.\n");
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100520}
521
522static struct {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100523 const char *config_file;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100524 const char *db_file;
525 bool daemonize;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100526 bool db_upgrade;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100527} cmdline_opts = {
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100528 .config_file = "osmo-hlr.cfg",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100529 .db_file = "hlr.db",
530 .daemonize = false,
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100531 .db_upgrade = false,
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100532};
533
534static void handle_options(int argc, char **argv)
535{
536 while (1) {
537 int option_index = 0, c;
538 static struct option long_options[] = {
539 {"help", 0, 0, 'h'},
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100540 {"config-file", 1, 0, 'c'},
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100541 {"database", 1, 0, 'l'},
542 {"debug", 1, 0, 'd'},
543 {"daemonize", 0, 0, 'D'},
544 {"disable-color", 0, 0, 's'},
545 {"log-level", 1, 0, 'e'},
546 {"timestamp", 0, 0, 'T'},
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100547 {"db-upgrade", 0, 0, 'U' },
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100548 {"version", 0, 0, 'V' },
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100549 {0, 0, 0, 0}
550 };
551
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100552 c = getopt_long(argc, argv, "hc:l:d:Dse:TUV",
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100553 long_options, &option_index);
554 if (c == -1)
555 break;
556
557 switch (c) {
558 case 'h':
559 print_usage();
560 print_help();
561 exit(0);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100562 case 'c':
563 cmdline_opts.config_file = optarg;
564 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100565 case 'l':
566 cmdline_opts.db_file = optarg;
567 break;
568 case 'd':
569 log_parse_category_mask(osmo_stderr_target, optarg);
570 break;
571 case 'D':
572 cmdline_opts.daemonize = 1;
573 break;
574 case 's':
575 log_set_use_color(osmo_stderr_target, 0);
576 break;
577 case 'e':
578 log_set_log_level(osmo_stderr_target, atoi(optarg));
579 break;
580 case 'T':
581 log_set_print_timestamp(osmo_stderr_target, 1);
582 break;
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100583 case 'U':
584 cmdline_opts.db_upgrade = true;
585 break;
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100586 case 'V':
587 print_version(1);
588 exit(0);
589 break;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100590 default:
591 /* catch unknown options *as well as* missing arguments. */
592 fprintf(stderr, "Error in command line options. Exiting.\n");
593 exit(-1);
594 break;
595 }
596 }
597}
598
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100599static void *hlr_ctx = NULL;
Harald Welteaabae9e2016-04-28 12:48:14 +0200600
601static void signal_hdlr(int signal)
602{
603 switch (signal) {
604 case SIGINT:
605 LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n");
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700606 quit++;
Harald Welteaabae9e2016-04-28 12:48:14 +0200607 break;
608 case SIGUSR1:
609 LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n");
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100610 talloc_report_full(hlr_ctx, stderr);
Harald Welteaabae9e2016-04-28 12:48:14 +0200611 break;
612 }
613}
Harald Weltee72cf552016-04-28 07:18:49 +0200614
Max372868b2017-03-02 12:12:00 +0100615static const char vlr_copyright[] =
616 "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n"
617 "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
618 "This is free software: you are free to change and redistribute it.\r\n"
619 "There is NO WARRANTY, to the extent permitted by law.\r\n";
620
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100621static struct vty_app_info vty_info = {
622 .name = "OsmoHLR",
623 .version = PACKAGE_VERSION,
Max372868b2017-03-02 12:12:00 +0100624 .copyright = vlr_copyright,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100625 .is_config_node = hlr_vty_is_config_node,
Pau Espin Pedrolce9bc402017-05-31 13:19:22 +0200626 .go_parent_cb = hlr_vty_go_parent,
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100627};
628
Harald Weltee72cf552016-04-28 07:18:49 +0200629int main(int argc, char **argv)
630{
Harald Weltee72cf552016-04-28 07:18:49 +0200631 int rc;
632
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700633 /* Track the use of talloc NULL memory contexts */
634 talloc_enable_null_tracking();
635
Neels Hofmeyrca43e302017-01-30 13:18:23 +0100636 hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR");
637 msgb_talloc_ctx_init(hlr_ctx, 0);
Harald Welte7ee6e552018-02-14 00:52:05 +0100638 vty_info.tall_ctx = hlr_ctx;
Harald Welteaabae9e2016-04-28 12:48:14 +0200639
Maxd4bebbd2017-03-02 12:00:19 +0100640 g_hlr = talloc_zero(hlr_ctx, struct hlr);
Harald Welte4956ae12018-06-15 22:04:28 +0200641 INIT_LLIST_HEAD(&g_hlr->euse_list);
Harald Weltedab544e2018-07-29 16:14:48 +0200642 INIT_LLIST_HEAD(&g_hlr->iuse_list);
Harald Weltebb779392018-06-16 20:21:10 +0200643 INIT_LLIST_HEAD(&g_hlr->ss_sessions);
Harald Weltedab544e2018-07-29 16:14:48 +0200644 INIT_LLIST_HEAD(&g_hlr->ussd_routes);
Maxd4bebbd2017-03-02 12:00:19 +0100645
Vadim Yanitskiyd157a562018-12-01 00:03:39 +0700646 /* Init default (call independent) SS session guard timeout value */
647 g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT;
648
Pau Espin Pedrol51530312018-04-17 15:07:06 +0200649 rc = osmo_init_logging2(hlr_ctx, &hlr_log_info);
Harald Weltee72cf552016-04-28 07:18:49 +0200650 if (rc < 0) {
651 fprintf(stderr, "Error initializing logging\n");
652 exit(1);
653 }
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100654
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100655 vty_init(&vty_info);
Max372868b2017-03-02 12:12:00 +0100656 ctrl_vty_init(hlr_ctx);
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100657 handle_options(argc, argv);
Harald Welted5807b82018-07-29 12:27:41 +0200658 hlr_vty_init(&hlr_log_info);
Neels Hofmeyr7685a782017-01-30 23:30:26 +0100659
660 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
661 if (rc < 0) {
662 LOGP(DMAIN, LOGL_FATAL,
663 "Failed to parse the config file: '%s'\n",
664 cmdline_opts.config_file);
665 return rc;
666 }
667
668 /* start telnet after reading config for vty_get_bind_addr() */
669 rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(),
670 OSMO_VTY_PORT_HLR);
671 if (rc < 0)
672 return rc;
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100673
Harald Weltee72cf552016-04-28 07:18:49 +0200674 LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n");
675
676 rc = rand_init();
677 if (rc < 0) {
678 LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n");
679 exit(1);
680 }
681
Stefan Sperling8f3a7cc2018-11-27 12:10:45 +0100682 g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true, cmdline_opts.db_upgrade);
Maxd4bebbd2017-03-02 12:00:19 +0100683 if (!g_hlr->dbc) {
Harald Weltee72cf552016-04-28 07:18:49 +0200684 LOGP(DMAIN, LOGL_FATAL, "Error opening database\n");
685 exit(1);
686 }
687
Neels Hofmeyr84201d32017-07-21 16:00:32 +0200688 g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT,
Harald Welte32acace2018-06-16 17:07:28 +0200689 read_cb, &g_lu_ops, g_hlr);
Maxd4bebbd2017-03-02 12:00:19 +0100690 if (!g_hlr->gs) {
Harald Weltee72cf552016-04-28 07:18:49 +0200691 LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n");
692 exit(1);
693 }
694
Max372868b2017-03-02 12:12:00 +0100695 g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr();
Neels Hofmeyr234f9cb2017-10-24 17:23:04 +0200696 g_hlr->ctrl = hlr_controlif_setup(g_hlr);
Max372868b2017-03-02 12:12:00 +0100697
Harald Welteaabae9e2016-04-28 12:48:14 +0200698 osmo_init_ignore_signals();
699 signal(SIGINT, &signal_hdlr);
700 signal(SIGUSR1, &signal_hdlr);
701
Neels Hofmeyr7f9491f2017-01-30 13:30:47 +0100702 if (cmdline_opts.daemonize) {
703 rc = osmo_daemonize();
704 if (rc < 0) {
705 perror("Error during daemonize");
706 exit(1);
707 }
708 }
Harald Welteaabae9e2016-04-28 12:48:14 +0200709
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700710 while (!quit)
Harald Weltee72cf552016-04-28 07:18:49 +0200711 osmo_select_main(0);
Harald Weltee72cf552016-04-28 07:18:49 +0200712
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700713 osmo_gsup_server_destroy(g_hlr->gs);
Maxd4bebbd2017-03-02 12:00:19 +0100714 db_close(g_hlr->dbc);
Harald Weltee72cf552016-04-28 07:18:49 +0200715 log_fini();
716
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700717 /**
718 * Report the heap state of root context, then free,
719 * so both ASAN and Valgrind are happy...
720 */
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700721 talloc_report_full(hlr_ctx, stderr);
Vadim Yanitskiy4793a7e2018-07-30 03:04:34 +0700722 talloc_free(hlr_ctx);
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700723
Vadim Yanitskiy9fdb8542018-07-30 03:09:22 +0700724 /* FIXME: VTY code still uses NULL-context */
725 talloc_free(tall_vty_ctx);
726
727 /**
728 * Report the heap state of NULL context, then free,
729 * so both ASAN and Valgrind are happy...
730 */
731 talloc_report_full(NULL, stderr);
732 talloc_disable_null_tracking();
733
Vadim Yanitskiy527d9342018-07-30 02:42:25 +0700734 return 0;
Harald Weltee72cf552016-04-28 07:18:49 +0200735}