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