Harald Welte | 936f672 | 2016-05-03 18:51:18 +0200 | [diff] [blame] | 1 | /* (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 Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 20 | #include <signal.h> |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 21 | #include <errno.h> |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 22 | #include <stdbool.h> |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 23 | #include <getopt.h> |
| 24 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 25 | #include <osmocom/core/msgb.h> |
| 26 | #include <osmocom/core/logging.h> |
| 27 | #include <osmocom/core/application.h> |
| 28 | #include <osmocom/gsm/gsup.h> |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 29 | #include <osmocom/vty/vty.h> |
| 30 | #include <osmocom/vty/command.h> |
| 31 | #include <osmocom/vty/telnet_interface.h> |
| 32 | #include <osmocom/vty/ports.h> |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 33 | #include <osmocom/ctrl/control_vty.h> |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 34 | #include <osmocom/gsm/apn.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 35 | |
| 36 | #include "db.h" |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 37 | #include "hlr.h" |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 38 | #include "ctrl.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 39 | #include "logging.h" |
| 40 | #include "gsup_server.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 41 | #include "gsup_router.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 42 | #include "rand.h" |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 43 | #include "luop.h" |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 44 | #include "hlr_vty.h" |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 45 | #include "hlr_ussd.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 46 | |
Harald Welte | d5807b8 | 2018-07-29 12:27:41 +0200 | [diff] [blame] | 47 | struct hlr *g_hlr; |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 48 | static int quit = 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 49 | |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 50 | /* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients. |
| 51 | * |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 52 | * \param[in] subscr A subscriber we have new data to send for. |
| 53 | */ |
| 54 | void |
| 55 | osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr) |
| 56 | { |
Harald Welte | b85f604 | 2018-06-15 18:01:14 +0200 | [diff] [blame] | 57 | /* FIXME: the below code can only be re-enabled after we make sure that an ISD |
| 58 | * is only sent tot the currently serving VLR and/or SGSN (if there are any). |
| 59 | * We cannot blindly flood the entire PLMN with this, as it would create subscriber |
| 60 | * state in every VLR/SGSN out there, even those that have never seen the subscriber. |
| 61 | * See https://osmocom.org/issues/3154 for details. */ |
| 62 | #if 0 |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 63 | struct osmo_gsup_conn *co; |
| 64 | |
| 65 | if (g_hlr->gs == NULL) |
| 66 | return; |
| 67 | |
| 68 | llist_for_each_entry(co, &g_hlr->gs->clients, list) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 69 | 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 Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 73 | uint8_t *peer; |
| 74 | int peer_len; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 75 | enum osmo_gsup_cn_domain cn_domain; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 76 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 77 | if (co->supports_ps) |
| 78 | cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
| 79 | else if (co->supports_cs) |
| 80 | cn_domain = OSMO_GSUP_CN_DOMAIN_CS; |
| 81 | else { |
| 82 | /* We have not yet received a location update from this subscriber .*/ |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc, |
| 87 | sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) { |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 88 | LOGP(DMAIN, LOGL_ERROR, |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 89 | "IMSI='%s': Cannot notify GSUP client; could not create gsup message " |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 90 | "for %s:%u\n", subscr->imsi, |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 91 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 92 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 93 | continue; |
| 94 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 95 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 96 | /* Send ISD to MSC/SGSN */ |
| 97 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE"); |
| 98 | if (msg_out == NULL) { |
| 99 | LOGP(DMAIN, LOGL_ERROR, |
| 100 | "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer " |
| 101 | "for %s:%u\n", subscr->imsi, |
| 102 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 103 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 104 | continue; |
| 105 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 106 | osmo_gsup_encode(msg_out, &gsup); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 107 | |
| 108 | peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR); |
| 109 | if (peer_len < 0) { |
| 110 | LOGP(DMAIN, LOGL_ERROR, |
| 111 | "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi, |
| 112 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 113 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 114 | continue; |
| 115 | } |
| 116 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 117 | if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) { |
| 118 | LOGP(DMAIN, LOGL_ERROR, |
| 119 | "IMSI='%s': Cannot notify GSUP client; send operation failed " |
| 120 | "for %s:%u\n", subscr->imsi, |
| 121 | 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 Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 125 | } |
Harald Welte | b85f604 | 2018-06-15 18:01:14 +0200 | [diff] [blame] | 126 | #endif |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 129 | /*********************************************************************** |
| 130 | * Send Auth Info handling |
| 131 | ***********************************************************************/ |
| 132 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 133 | /* process an incoming SAI request */ |
| 134 | static int rx_send_auth_info(struct osmo_gsup_conn *conn, |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 135 | const struct osmo_gsup_message *gsup, |
| 136 | struct db_context *dbc) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 137 | { |
| 138 | struct osmo_gsup_message gsup_out; |
| 139 | struct msgb *msg_out; |
| 140 | int rc; |
| 141 | |
| 142 | /* initialize return message structure */ |
| 143 | memset(&gsup_out, 0, sizeof(gsup_out)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 144 | memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi)); |
| 145 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 146 | rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind, |
| 147 | gsup_out.auth_vectors, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 148 | ARRAY_SIZE(gsup_out.auth_vectors), |
Harald Welte | 9be0d2f | 2016-06-10 17:34:02 +0200 | [diff] [blame] | 149 | gsup->rand, gsup->auts); |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 150 | if (rc <= 0) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 151 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 152 | switch (rc) { |
| 153 | case 0: |
Neels Hofmeyr | bd1dca0 | 2017-11-23 15:25:30 +0100 | [diff] [blame] | 154 | /* 0 means "0 tuples generated", which shouldn't happen. |
| 155 | * Treat the same as "no auth data". */ |
| 156 | case -ENOKEY: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 157 | LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;" |
| 158 | " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n", |
| 159 | gsup->imsi); |
| 160 | gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 161 | break; |
Neels Hofmeyr | 33cbde9 | 2017-11-22 20:39:59 +0100 | [diff] [blame] | 162 | case -ENOENT: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 163 | LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi); |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 164 | gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 165 | break; |
| 166 | default: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 167 | LOGP(DAUC, LOGL_ERROR, "%s: failure to look up IMSI in db\n", gsup->imsi); |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 168 | gsup_out.cause = GMM_CAUSE_NET_FAIL; |
| 169 | break; |
| 170 | } |
Harald Welte | 15db826 | 2016-05-05 16:50:39 +0200 | [diff] [blame] | 171 | } else { |
| 172 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT; |
| 173 | gsup_out.num_auth_vectors = rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 176 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 177 | osmo_gsup_encode(msg_out, &gsup_out); |
| 178 | return osmo_gsup_conn_send(conn, msg_out); |
| 179 | } |
| 180 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 181 | /*********************************************************************** |
| 182 | * LU Operation State / Structure |
| 183 | ***********************************************************************/ |
| 184 | |
| 185 | static LLIST_HEAD(g_lu_ops); |
| 186 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 187 | /*! Receive Cancel Location Result from old VLR/SGSN */ |
| 188 | void lu_op_rx_cancel_old_ack(struct lu_operation *luop, |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 189 | const struct osmo_gsup_message *gsup) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 190 | { |
| 191 | OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT); |
| 192 | /* FIXME: Check for spoofing */ |
| 193 | |
| 194 | osmo_timer_del(&luop->timer); |
| 195 | |
| 196 | /* FIXME */ |
| 197 | |
| 198 | lu_op_tx_insert_subscr_data(luop); |
| 199 | } |
| 200 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 201 | /*! Receive Insert Subscriber Data Result from new VLR/SGSN */ |
| 202 | static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop, |
| 203 | const struct osmo_gsup_message *gsup) |
| 204 | { |
| 205 | OSMO_ASSERT(luop->state == LU_S_ISD_SENT); |
| 206 | /* FIXME: Check for spoofing */ |
| 207 | |
| 208 | osmo_timer_del(&luop->timer); |
| 209 | |
| 210 | /* Subscriber_Present_HLR */ |
| 211 | /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */ |
| 212 | |
| 213 | /* Send final ACK towards inquiring VLR/SGSN */ |
| 214 | lu_op_tx_ack(luop); |
| 215 | } |
| 216 | |
| 217 | /*! Receive GSUP message for given \ref lu_operation */ |
| 218 | void lu_op_rx_gsup(struct lu_operation *luop, |
| 219 | const struct osmo_gsup_message *gsup) |
| 220 | { |
| 221 | switch (gsup->message_type) { |
| 222 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
| 223 | /* FIXME */ |
| 224 | break; |
| 225 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
| 226 | lu_op_rx_insert_subscr_data_ack(luop, gsup); |
| 227 | break; |
| 228 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 229 | /* FIXME */ |
| 230 | break; |
| 231 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 232 | lu_op_rx_cancel_old_ack(luop, gsup); |
| 233 | break; |
| 234 | default: |
| 235 | LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n", |
| 236 | gsup->message_type); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 241 | /*! Receive Update Location Request, creates new \ref lu_operation */ |
| 242 | static int rx_upd_loc_req(struct osmo_gsup_conn *conn, |
| 243 | const struct osmo_gsup_message *gsup) |
| 244 | { |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame^] | 245 | struct hlr_subscriber *subscr; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 246 | struct lu_operation *luop = lu_op_alloc_conn(conn); |
| 247 | if (!luop) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 248 | LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n"); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 249 | return -EINVAL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame^] | 252 | subscr = &luop->subscr; |
| 253 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 254 | lu_op_statechg(luop, LU_S_LU_RECEIVED); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 255 | |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 256 | switch (gsup->cn_domain) { |
| 257 | case OSMO_GSUP_CN_DOMAIN_CS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 258 | conn->supports_cs = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 259 | break; |
| 260 | default: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 261 | /* The client didn't send a CN_DOMAIN IE; assume packet-switched in |
| 262 | * accordance with the GSUP spec in osmo-hlr's user manual (section |
| 263 | * 11.6.15 "CN Domain" says "if no CN Domain IE is present within |
| 264 | * a request, the PS Domain is assumed." */ |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 265 | case OSMO_GSUP_CN_DOMAIN_PS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 266 | conn->supports_ps = true; |
Stefan Sperling | 1cb4892 | 2018-05-03 14:05:56 +0200 | [diff] [blame] | 267 | luop->is_ps = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 268 | break; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 269 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 270 | llist_add(&luop->list, &g_lu_ops); |
| 271 | |
| 272 | /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */ |
| 273 | |
| 274 | /* check if subscriber is known at all */ |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 275 | if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 276 | /* Send Error back: Subscriber Unknown in HLR */ |
Harald Welte | bd0d5bf | 2017-11-06 03:55:02 +0900 | [diff] [blame] | 277 | osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi)); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 278 | lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN); |
| 279 | return 0; |
| 280 | } |
| 281 | |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 282 | /* Check if subscriber is generally permitted on CS or PS |
| 283 | * service (as requested) */ |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 284 | if (!luop->is_ps && !luop->subscr.nam_cs) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 285 | lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED); |
| 286 | return 0; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 287 | } else if (luop->is_ps && !luop->subscr.nam_ps) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 288 | lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /* TODO: Set subscriber tracing = deactive in VLR/SGSN */ |
| 293 | |
| 294 | #if 0 |
| 295 | /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */ |
| 296 | if (luop->is_ps == false && |
| 297 | strcmp(subscr->vlr_number, vlr_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 298 | lu_op_tx_cancel_old(luop); |
| 299 | } else if (luop->is_ps == true && |
| 300 | strcmp(subscr->sgsn_number, sgsn_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 301 | lu_op_tx_cancel_old(luop); |
| 302 | } else |
| 303 | #endif |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame^] | 304 | |
| 305 | /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */ |
| 306 | LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing %s = %s\n", |
| 307 | subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number", |
| 308 | osmo_quote_str((const char*)luop->peer, -1)); |
| 309 | if (db_subscr_lu(g_hlr->dbc, subscr->id, (const char *)luop->peer, luop->is_ps)) |
| 310 | LOGP(DAUC, LOGL_ERROR, "IMSI='%s': Cannot update %s in the database\n", |
| 311 | subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number"); |
| 312 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 313 | { |
| 314 | /* TODO: Subscriber allowed to roam in PLMN? */ |
| 315 | /* TODO: Update RoutingInfo */ |
| 316 | /* TODO: Reset Flag MS Purged (cs/ps) */ |
| 317 | /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */ |
| 318 | lu_op_tx_insert_subscr_data(luop); |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 323 | static int rx_purge_ms_req(struct osmo_gsup_conn *conn, |
| 324 | const struct osmo_gsup_message *gsup) |
| 325 | { |
| 326 | struct osmo_gsup_message gsup_reply = {0}; |
| 327 | struct msgb *msg_out; |
| 328 | bool is_ps = false; |
| 329 | int rc; |
| 330 | |
| 331 | LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi, |
| 332 | is_ps ? "PS" : "CS"); |
| 333 | |
| 334 | memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi)); |
| 335 | |
| 336 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) |
| 337 | is_ps = true; |
| 338 | |
| 339 | /* FIXME: check if the VLR that sends the purge is the same that |
| 340 | * we have on record. Only update if yes */ |
| 341 | |
| 342 | /* Perform the actual update of the DB */ |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 343 | rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 344 | |
Harald Welte | 3f2a9a2 | 2018-03-01 23:35:35 +0100 | [diff] [blame] | 345 | if (rc == 0) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 346 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT; |
Harald Welte | 3f2a9a2 | 2018-03-01 23:35:35 +0100 | [diff] [blame] | 347 | else if (rc == -ENOENT) { |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 348 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 349 | gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 350 | } else { |
| 351 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 352 | gsup_reply.cause = GMM_CAUSE_NET_FAIL; |
| 353 | } |
| 354 | |
| 355 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
| 356 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 357 | return osmo_gsup_conn_send(conn, msg_out); |
| 358 | } |
| 359 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 360 | static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi, |
| 361 | enum osmo_gsup_message_type type_in, uint8_t err_cause) |
| 362 | { |
| 363 | int type_err = osmo_gsup_get_err_msg_type(type_in); |
| 364 | struct osmo_gsup_message gsup_reply = {0}; |
| 365 | struct msgb *msg_out; |
| 366 | |
| 367 | if (type_err < 0) { |
| 368 | LOGP(DMAIN, LOGL_ERROR, "unable to determine error response for %s\n", |
| 369 | osmo_gsup_message_type_name(type_in)); |
| 370 | return type_err; |
| 371 | } |
| 372 | |
| 373 | OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi); |
| 374 | gsup_reply.message_type = type_err; |
| 375 | gsup_reply.cause = err_cause; |
| 376 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response"); |
| 377 | OSMO_ASSERT(msg_out); |
| 378 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 379 | LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err)); |
| 380 | return osmo_gsup_conn_send(conn, msg_out); |
| 381 | } |
| 382 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 383 | static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 384 | { |
| 385 | static struct osmo_gsup_message gsup; |
| 386 | int rc; |
| 387 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 388 | rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 389 | if (rc < 0) { |
| 390 | LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc); |
| 391 | return rc; |
| 392 | } |
| 393 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 394 | /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5 |
| 395 | * digits is impossible. Even 5 digits is a highly theoretical case */ |
| 396 | if (strlen(gsup.imsi) < 5) |
| 397 | return gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO); |
| 398 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 399 | switch (gsup.message_type) { |
| 400 | /* requests sent to us */ |
| 401 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 402 | rx_send_auth_info(conn, &gsup, g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 403 | break; |
| 404 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 405 | rx_upd_loc_req(conn, &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 406 | break; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 407 | case OSMO_GSUP_MSGT_PURGE_MS_REQUEST: |
| 408 | rx_purge_ms_req(conn, &gsup); |
| 409 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 410 | /* responses to requests sent by us */ |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 411 | case OSMO_GSUP_MSGT_DELETE_DATA_ERROR: |
| 412 | LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data " |
| 413 | "for IMSI %s\n", gsup.imsi); |
| 414 | break; |
| 415 | case OSMO_GSUP_MSGT_DELETE_DATA_RESULT: |
| 416 | LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n", |
| 417 | gsup.imsi); |
| 418 | break; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 419 | case OSMO_GSUP_MSGT_PROC_SS_REQUEST: |
| 420 | case OSMO_GSUP_MSGT_PROC_SS_RESULT: |
| 421 | rx_proc_ss_req(conn, &gsup); |
| 422 | break; |
| 423 | case OSMO_GSUP_MSGT_PROC_SS_ERROR: |
| 424 | rx_proc_ss_error(conn, &gsup); |
| 425 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 426 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 427 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 428 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 429 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 430 | { |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 431 | struct lu_operation *luop = lu_op_by_imsi(gsup.imsi, |
| 432 | &g_lu_ops); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 433 | if (!luop) { |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 434 | LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for " |
| 435 | "unknown IMSI %s\n", |
| 436 | osmo_gsup_message_type_name(gsup.message_type), |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 437 | gsup.imsi); |
| 438 | break; |
| 439 | } |
| 440 | lu_op_rx_gsup(luop, &gsup); |
| 441 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 442 | break; |
| 443 | default: |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 444 | LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n", |
| 445 | osmo_gsup_message_type_name(gsup.message_type)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 446 | break; |
| 447 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 448 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 452 | static void print_usage() |
| 453 | { |
| 454 | printf("Usage: osmo-hlr\n"); |
| 455 | } |
| 456 | |
| 457 | static void print_help() |
| 458 | { |
| 459 | printf(" -h --help This text.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 460 | printf(" -c --config-file filename The config file to use.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 461 | printf(" -l --database db-name The database to use.\n"); |
| 462 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n"); |
| 463 | printf(" -D --daemonize Fork the process into a background daemon.\n"); |
| 464 | printf(" -s --disable-color Do not print ANSI colors in the log\n"); |
| 465 | printf(" -T --timestamp Prefix every log line with a timestamp.\n"); |
| 466 | printf(" -e --log-level number Set a global loglevel.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 467 | printf(" -V --version Print the version of OsmoHLR.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static struct { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 471 | const char *config_file; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 472 | const char *db_file; |
| 473 | bool daemonize; |
| 474 | } cmdline_opts = { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 475 | .config_file = "osmo-hlr.cfg", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 476 | .db_file = "hlr.db", |
| 477 | .daemonize = false, |
| 478 | }; |
| 479 | |
| 480 | static void handle_options(int argc, char **argv) |
| 481 | { |
| 482 | while (1) { |
| 483 | int option_index = 0, c; |
| 484 | static struct option long_options[] = { |
| 485 | {"help", 0, 0, 'h'}, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 486 | {"config-file", 1, 0, 'c'}, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 487 | {"database", 1, 0, 'l'}, |
| 488 | {"debug", 1, 0, 'd'}, |
| 489 | {"daemonize", 0, 0, 'D'}, |
| 490 | {"disable-color", 0, 0, 's'}, |
| 491 | {"log-level", 1, 0, 'e'}, |
| 492 | {"timestamp", 0, 0, 'T'}, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 493 | {"version", 0, 0, 'V' }, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 494 | {0, 0, 0, 0} |
| 495 | }; |
| 496 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 497 | c = getopt_long(argc, argv, "hc:l:d:Dse:TV", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 498 | long_options, &option_index); |
| 499 | if (c == -1) |
| 500 | break; |
| 501 | |
| 502 | switch (c) { |
| 503 | case 'h': |
| 504 | print_usage(); |
| 505 | print_help(); |
| 506 | exit(0); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 507 | case 'c': |
| 508 | cmdline_opts.config_file = optarg; |
| 509 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 510 | case 'l': |
| 511 | cmdline_opts.db_file = optarg; |
| 512 | break; |
| 513 | case 'd': |
| 514 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 515 | break; |
| 516 | case 'D': |
| 517 | cmdline_opts.daemonize = 1; |
| 518 | break; |
| 519 | case 's': |
| 520 | log_set_use_color(osmo_stderr_target, 0); |
| 521 | break; |
| 522 | case 'e': |
| 523 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 524 | break; |
| 525 | case 'T': |
| 526 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 527 | break; |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 528 | case 'V': |
| 529 | print_version(1); |
| 530 | exit(0); |
| 531 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 532 | default: |
| 533 | /* catch unknown options *as well as* missing arguments. */ |
| 534 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 535 | exit(-1); |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 541 | static void *hlr_ctx = NULL; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 542 | |
| 543 | static void signal_hdlr(int signal) |
| 544 | { |
| 545 | switch (signal) { |
| 546 | case SIGINT: |
| 547 | LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n"); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 548 | quit++; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 549 | break; |
| 550 | case SIGUSR1: |
| 551 | LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n"); |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 552 | talloc_report_full(hlr_ctx, stderr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 553 | break; |
| 554 | } |
| 555 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 556 | |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 557 | static const char vlr_copyright[] = |
| 558 | "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n" |
| 559 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 560 | "This is free software: you are free to change and redistribute it.\r\n" |
| 561 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 562 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 563 | static struct vty_app_info vty_info = { |
| 564 | .name = "OsmoHLR", |
| 565 | .version = PACKAGE_VERSION, |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 566 | .copyright = vlr_copyright, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 567 | .is_config_node = hlr_vty_is_config_node, |
Pau Espin Pedrol | ce9bc40 | 2017-05-31 13:19:22 +0200 | [diff] [blame] | 568 | .go_parent_cb = hlr_vty_go_parent, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 569 | }; |
| 570 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 571 | int main(int argc, char **argv) |
| 572 | { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 573 | int rc; |
| 574 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 575 | /* Track the use of talloc NULL memory contexts */ |
| 576 | talloc_enable_null_tracking(); |
| 577 | |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 578 | hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR"); |
| 579 | msgb_talloc_ctx_init(hlr_ctx, 0); |
Harald Welte | 7ee6e55 | 2018-02-14 00:52:05 +0100 | [diff] [blame] | 580 | vty_info.tall_ctx = hlr_ctx; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 581 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 582 | g_hlr = talloc_zero(hlr_ctx, struct hlr); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 583 | INIT_LLIST_HEAD(&g_hlr->euse_list); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 584 | INIT_LLIST_HEAD(&g_hlr->iuse_list); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 585 | INIT_LLIST_HEAD(&g_hlr->ss_sessions); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 586 | INIT_LLIST_HEAD(&g_hlr->ussd_routes); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 587 | |
Pau Espin Pedrol | 5153031 | 2018-04-17 15:07:06 +0200 | [diff] [blame] | 588 | rc = osmo_init_logging2(hlr_ctx, &hlr_log_info); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 589 | if (rc < 0) { |
| 590 | fprintf(stderr, "Error initializing logging\n"); |
| 591 | exit(1); |
| 592 | } |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 593 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 594 | vty_init(&vty_info); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 595 | ctrl_vty_init(hlr_ctx); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 596 | handle_options(argc, argv); |
Harald Welte | d5807b8 | 2018-07-29 12:27:41 +0200 | [diff] [blame] | 597 | hlr_vty_init(&hlr_log_info); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 598 | |
| 599 | rc = vty_read_config_file(cmdline_opts.config_file, NULL); |
| 600 | if (rc < 0) { |
| 601 | LOGP(DMAIN, LOGL_FATAL, |
| 602 | "Failed to parse the config file: '%s'\n", |
| 603 | cmdline_opts.config_file); |
| 604 | return rc; |
| 605 | } |
| 606 | |
| 607 | /* start telnet after reading config for vty_get_bind_addr() */ |
| 608 | rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(), |
| 609 | OSMO_VTY_PORT_HLR); |
| 610 | if (rc < 0) |
| 611 | return rc; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 612 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 613 | LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n"); |
| 614 | |
| 615 | rc = rand_init(); |
| 616 | if (rc < 0) { |
| 617 | LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n"); |
| 618 | exit(1); |
| 619 | } |
| 620 | |
Neels Hofmeyr | d3814b9 | 2017-11-21 12:28:07 +0100 | [diff] [blame] | 621 | g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file, true); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 622 | if (!g_hlr->dbc) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 623 | LOGP(DMAIN, LOGL_FATAL, "Error opening database\n"); |
| 624 | exit(1); |
| 625 | } |
| 626 | |
Neels Hofmeyr | 84201d3 | 2017-07-21 16:00:32 +0200 | [diff] [blame] | 627 | g_hlr->gs = osmo_gsup_server_create(hlr_ctx, g_hlr->gsup_bind_addr, OSMO_GSUP_PORT, |
Harald Welte | 32acace | 2018-06-16 17:07:28 +0200 | [diff] [blame] | 628 | read_cb, &g_lu_ops, g_hlr); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 629 | if (!g_hlr->gs) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 630 | LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n"); |
| 631 | exit(1); |
| 632 | } |
| 633 | |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 634 | g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr(); |
Neels Hofmeyr | 234f9cb | 2017-10-24 17:23:04 +0200 | [diff] [blame] | 635 | g_hlr->ctrl = hlr_controlif_setup(g_hlr); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 636 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 637 | osmo_init_ignore_signals(); |
| 638 | signal(SIGINT, &signal_hdlr); |
| 639 | signal(SIGUSR1, &signal_hdlr); |
| 640 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 641 | if (cmdline_opts.daemonize) { |
| 642 | rc = osmo_daemonize(); |
| 643 | if (rc < 0) { |
| 644 | perror("Error during daemonize"); |
| 645 | exit(1); |
| 646 | } |
| 647 | } |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 648 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 649 | while (!quit) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 650 | osmo_select_main(0); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 651 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 652 | osmo_gsup_server_destroy(g_hlr->gs); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 653 | db_close(g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 654 | log_fini(); |
| 655 | |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 656 | /** |
| 657 | * Report the heap state of root context, then free, |
| 658 | * so both ASAN and Valgrind are happy... |
| 659 | */ |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 660 | talloc_report_full(hlr_ctx, stderr); |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 661 | talloc_free(hlr_ctx); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 662 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 663 | /* FIXME: VTY code still uses NULL-context */ |
| 664 | talloc_free(tall_vty_ctx); |
| 665 | |
| 666 | /** |
| 667 | * Report the heap state of NULL context, then free, |
| 668 | * so both ASAN and Valgrind are happy... |
| 669 | */ |
| 670 | talloc_report_full(NULL, stderr); |
| 671 | talloc_disable_null_tracking(); |
| 672 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 673 | return 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 674 | } |