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> |
Max | 20ddfdb | 2019-02-18 13:12:27 +0100 | [diff] [blame] | 26 | #include <osmocom/core/stats.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 27 | #include <osmocom/core/logging.h> |
| 28 | #include <osmocom/core/application.h> |
| 29 | #include <osmocom/gsm/gsup.h> |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 30 | #include <osmocom/vty/vty.h> |
| 31 | #include <osmocom/vty/command.h> |
| 32 | #include <osmocom/vty/telnet_interface.h> |
| 33 | #include <osmocom/vty/ports.h> |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 34 | #include <osmocom/ctrl/control_vty.h> |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 35 | #include <osmocom/gsm/apn.h> |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/gsm48_ie.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 37 | |
| 38 | #include "db.h" |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 39 | #include "hlr.h" |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 40 | #include "ctrl.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 41 | #include "logging.h" |
| 42 | #include "gsup_server.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 43 | #include "gsup_router.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 44 | #include "rand.h" |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 45 | #include "luop.h" |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 46 | #include "hlr_vty.h" |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 47 | #include "hlr_ussd.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 48 | |
Harald Welte | d5807b8 | 2018-07-29 12:27:41 +0200 | [diff] [blame] | 49 | struct hlr *g_hlr; |
Oliver Smith | c5044cf | 2019-02-26 16:32:57 +0100 | [diff] [blame] | 50 | static void *hlr_ctx = NULL; |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 51 | static int quit = 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 52 | |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 53 | /* Trigger 'Insert Subscriber Data' messages to all connected GSUP clients. |
| 54 | * |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 55 | * \param[in] subscr A subscriber we have new data to send for. |
| 56 | */ |
| 57 | void |
| 58 | osmo_hlr_subscriber_update_notify(struct hlr_subscriber *subscr) |
| 59 | { |
| 60 | struct osmo_gsup_conn *co; |
| 61 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 62 | if (g_hlr->gs == NULL) { |
| 63 | LOGP(DLGSUP, LOGL_DEBUG, |
| 64 | "IMSI %s: NOT Notifying peers of subscriber data change," |
| 65 | " there is no GSUP server\n", |
| 66 | subscr->imsi); |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 67 | return; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 68 | } |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 69 | |
| 70 | llist_for_each_entry(co, &g_hlr->gs->clients, list) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 71 | struct osmo_gsup_message gsup = { }; |
| 72 | uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN]; |
| 73 | uint8_t apn[APN_MAXLEN]; |
| 74 | struct msgb *msg_out; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 75 | uint8_t *peer; |
| 76 | int peer_len; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 77 | size_t peer_strlen; |
| 78 | const char *peer_compare; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 79 | enum osmo_gsup_cn_domain cn_domain; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 80 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 81 | if (co->supports_ps) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 82 | cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 83 | peer_compare = subscr->sgsn_number; |
| 84 | } else if (co->supports_cs) { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 85 | cn_domain = OSMO_GSUP_CN_DOMAIN_CS; |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 86 | peer_compare = subscr->vlr_number; |
| 87 | } else { |
| 88 | /* We have not yet received a location update from this GSUP client.*/ |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 89 | continue; |
| 90 | } |
| 91 | |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 92 | peer_len = osmo_gsup_conn_ccm_get(co, &peer, IPAC_IDTAG_SERNR); |
| 93 | if (peer_len < 0) { |
| 94 | LOGP(DLGSUP, LOGL_ERROR, |
| 95 | "IMSI='%s': cannot get peer name for connection %s:%u\n", subscr->imsi, |
| 96 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 97 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | peer_strlen = strnlen((const char*)peer, peer_len); |
| 102 | if (strlen(peer_compare) != peer_strlen || strncmp(peer_compare, (const char *)peer, peer_len)) { |
| 103 | /* Mismatch. The subscriber is not subscribed with this GSUP client. */ |
| 104 | /* I hope peer is always nul terminated... */ |
| 105 | if (peer_strlen < peer_len) |
| 106 | LOGP(DLGSUP, LOGL_DEBUG, |
| 107 | "IMSI %s: subscriber change: skipping %s peer %s\n", |
| 108 | subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS", |
| 109 | osmo_quote_str((char*)peer, -1)); |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | LOGP(DLGSUP, LOGL_DEBUG, |
| 114 | "IMSI %s: subscriber change: notifying %s peer %s\n", |
| 115 | subscr->imsi, cn_domain == OSMO_GSUP_CN_DOMAIN_PS ? "PS" : "CS", |
| 116 | osmo_quote_str(peer_compare, -1)); |
| 117 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 118 | if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc, |
| 119 | sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) { |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 120 | LOGP(DLGSUP, LOGL_ERROR, |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 121 | "IMSI='%s': Cannot notify GSUP client; could not create gsup message " |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 122 | "for %s:%u\n", subscr->imsi, |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 123 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 124 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 125 | continue; |
| 126 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 127 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 128 | /* Send ISD to MSC/SGSN */ |
| 129 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ISD UPDATE"); |
| 130 | if (msg_out == NULL) { |
Neels Hofmeyr | e66e525 | 2018-09-28 02:59:19 +0200 | [diff] [blame] | 131 | LOGP(DLGSUP, LOGL_ERROR, |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 132 | "IMSI='%s': Cannot notify GSUP client; could not allocate msg buffer " |
| 133 | "for %s:%u\n", subscr->imsi, |
| 134 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 135 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 136 | continue; |
| 137 | } |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 138 | osmo_gsup_encode(msg_out, &gsup); |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 139 | |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 140 | if (osmo_gsup_addr_send(g_hlr->gs, peer, peer_len, msg_out) < 0) { |
| 141 | LOGP(DMAIN, LOGL_ERROR, |
| 142 | "IMSI='%s': Cannot notify GSUP client; send operation failed " |
| 143 | "for %s:%u\n", subscr->imsi, |
| 144 | co && co->conn && co->conn->server? co->conn->server->addr : "unset", |
| 145 | co && co->conn && co->conn->server? co->conn->server->port : 0); |
| 146 | continue; |
| 147 | } |
Stefan Sperling | f162252 | 2018-04-09 11:39:16 +0200 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 151 | /*********************************************************************** |
| 152 | * Send Auth Info handling |
| 153 | ***********************************************************************/ |
| 154 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 155 | /* process an incoming SAI request */ |
| 156 | static int rx_send_auth_info(struct osmo_gsup_conn *conn, |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 157 | const struct osmo_gsup_message *gsup, |
| 158 | struct db_context *dbc) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 159 | { |
| 160 | struct osmo_gsup_message gsup_out; |
| 161 | struct msgb *msg_out; |
| 162 | int rc; |
| 163 | |
| 164 | /* initialize return message structure */ |
| 165 | memset(&gsup_out, 0, sizeof(gsup_out)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 166 | memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi)); |
| 167 | |
Neels Hofmeyr | cab2fcd | 2017-03-15 00:07:43 +0100 | [diff] [blame] | 168 | rc = db_get_auc(dbc, gsup->imsi, conn->auc_3g_ind, |
| 169 | gsup_out.auth_vectors, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 170 | ARRAY_SIZE(gsup_out.auth_vectors), |
Harald Welte | 9be0d2f | 2016-06-10 17:34:02 +0200 | [diff] [blame] | 171 | gsup->rand, gsup->auts); |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 172 | if (rc <= 0) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 173 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 174 | switch (rc) { |
| 175 | case 0: |
Neels Hofmeyr | bd1dca0 | 2017-11-23 15:25:30 +0100 | [diff] [blame] | 176 | /* 0 means "0 tuples generated", which shouldn't happen. |
| 177 | * Treat the same as "no auth data". */ |
| 178 | case -ENOKEY: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 179 | LOGP(DAUC, LOGL_NOTICE, "%s: IMSI known, but has no auth data;" |
| 180 | " Returning slightly inaccurate cause 'IMSI Unknown' via GSUP\n", |
| 181 | gsup->imsi); |
| 182 | gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 183 | break; |
Neels Hofmeyr | 33cbde9 | 2017-11-22 20:39:59 +0100 | [diff] [blame] | 184 | case -ENOENT: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 185 | LOGP(DAUC, LOGL_NOTICE, "%s: IMSI not known\n", gsup->imsi); |
Neels Hofmeyr | 671db90 | 2017-11-22 20:38:19 +0100 | [diff] [blame] | 186 | gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 187 | break; |
| 188 | default: |
Neels Hofmeyr | ab4d509 | 2017-11-23 15:31:12 +0100 | [diff] [blame] | 189 | 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] | 190 | gsup_out.cause = GMM_CAUSE_NET_FAIL; |
| 191 | break; |
| 192 | } |
Harald Welte | 15db826 | 2016-05-05 16:50:39 +0200 | [diff] [blame] | 193 | } else { |
| 194 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT; |
| 195 | gsup_out.num_auth_vectors = rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 198 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 199 | osmo_gsup_encode(msg_out, &gsup_out); |
| 200 | return osmo_gsup_conn_send(conn, msg_out); |
| 201 | } |
| 202 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 203 | /*********************************************************************** |
| 204 | * LU Operation State / Structure |
| 205 | ***********************************************************************/ |
| 206 | |
| 207 | static LLIST_HEAD(g_lu_ops); |
| 208 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 209 | /*! Receive Cancel Location Result from old VLR/SGSN */ |
| 210 | void lu_op_rx_cancel_old_ack(struct lu_operation *luop, |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 211 | const struct osmo_gsup_message *gsup) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 212 | { |
| 213 | OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT); |
| 214 | /* FIXME: Check for spoofing */ |
| 215 | |
| 216 | osmo_timer_del(&luop->timer); |
| 217 | |
| 218 | /* FIXME */ |
| 219 | |
| 220 | lu_op_tx_insert_subscr_data(luop); |
| 221 | } |
| 222 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 223 | /*! Receive Insert Subscriber Data Result from new VLR/SGSN */ |
| 224 | static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop, |
| 225 | const struct osmo_gsup_message *gsup) |
| 226 | { |
| 227 | OSMO_ASSERT(luop->state == LU_S_ISD_SENT); |
| 228 | /* FIXME: Check for spoofing */ |
| 229 | |
| 230 | osmo_timer_del(&luop->timer); |
| 231 | |
| 232 | /* Subscriber_Present_HLR */ |
| 233 | /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */ |
| 234 | |
| 235 | /* Send final ACK towards inquiring VLR/SGSN */ |
| 236 | lu_op_tx_ack(luop); |
| 237 | } |
| 238 | |
| 239 | /*! Receive GSUP message for given \ref lu_operation */ |
| 240 | void lu_op_rx_gsup(struct lu_operation *luop, |
| 241 | const struct osmo_gsup_message *gsup) |
| 242 | { |
| 243 | switch (gsup->message_type) { |
| 244 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
| 245 | /* FIXME */ |
| 246 | break; |
| 247 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
| 248 | lu_op_rx_insert_subscr_data_ack(luop, gsup); |
| 249 | break; |
| 250 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 251 | /* FIXME */ |
| 252 | break; |
| 253 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 254 | lu_op_rx_cancel_old_ack(luop, gsup); |
| 255 | break; |
| 256 | default: |
| 257 | LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n", |
| 258 | gsup->message_type); |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 263 | /*! Receive Update Location Request, creates new \ref lu_operation */ |
| 264 | static int rx_upd_loc_req(struct osmo_gsup_conn *conn, |
| 265 | const struct osmo_gsup_message *gsup) |
| 266 | { |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 267 | struct hlr_subscriber *subscr; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 268 | struct lu_operation *luop = lu_op_alloc_conn(conn); |
| 269 | if (!luop) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 270 | LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n"); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 271 | return -EINVAL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 274 | subscr = &luop->subscr; |
| 275 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 276 | lu_op_statechg(luop, LU_S_LU_RECEIVED); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 277 | |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 278 | switch (gsup->cn_domain) { |
| 279 | case OSMO_GSUP_CN_DOMAIN_CS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 280 | conn->supports_cs = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 281 | break; |
| 282 | default: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 283 | /* 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 Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 287 | case OSMO_GSUP_CN_DOMAIN_PS: |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 288 | conn->supports_ps = true; |
Stefan Sperling | 1cb4892 | 2018-05-03 14:05:56 +0200 | [diff] [blame] | 289 | luop->is_ps = true; |
Neels Hofmeyr | 6cee799 | 2018-09-28 02:53:22 +0200 | [diff] [blame] | 290 | break; |
Stefan Sperling | 93c5b10 | 2018-04-10 19:26:14 +0200 | [diff] [blame] | 291 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 292 | 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 */ |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 297 | if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 298 | /* Send Error back: Subscriber Unknown in HLR */ |
Harald Welte | bd0d5bf | 2017-11-06 03:55:02 +0900 | [diff] [blame] | 299 | osmo_strlcpy(luop->subscr.imsi, gsup->imsi, sizeof(luop->subscr.imsi)); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 300 | lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 304 | /* Check if subscriber is generally permitted on CS or PS |
| 305 | * service (as requested) */ |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 306 | if (!luop->is_ps && !luop->subscr.nam_cs) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 307 | lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED); |
| 308 | return 0; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 309 | } else if (luop->is_ps && !luop->subscr.nam_ps) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 310 | lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* TODO: Set subscriber tracing = deactive in VLR/SGSN */ |
| 315 | |
| 316 | #if 0 |
| 317 | /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */ |
| 318 | if (luop->is_ps == false && |
| 319 | strcmp(subscr->vlr_number, vlr_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 320 | lu_op_tx_cancel_old(luop); |
| 321 | } else if (luop->is_ps == true && |
| 322 | strcmp(subscr->sgsn_number, sgsn_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 323 | lu_op_tx_cancel_old(luop); |
| 324 | } else |
| 325 | #endif |
Neels Hofmeyr | cb364bb | 2018-09-28 01:22:15 +0200 | [diff] [blame] | 326 | |
| 327 | /* Store the VLR / SGSN number with the subscriber, so we know where it was last seen. */ |
| 328 | LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing %s = %s\n", |
| 329 | subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number", |
| 330 | osmo_quote_str((const char*)luop->peer, -1)); |
| 331 | if (db_subscr_lu(g_hlr->dbc, subscr->id, (const char *)luop->peer, luop->is_ps)) |
| 332 | LOGP(DAUC, LOGL_ERROR, "IMSI='%s': Cannot update %s in the database\n", |
| 333 | subscr->imsi, luop->is_ps ? "SGSN number" : "VLR number"); |
| 334 | |
Oliver Smith | 66106c0 | 2019-01-09 12:05:15 +0100 | [diff] [blame] | 335 | /* TODO: Subscriber allowed to roam in PLMN? */ |
| 336 | /* TODO: Update RoutingInfo */ |
| 337 | /* TODO: Reset Flag MS Purged (cs/ps) */ |
| 338 | /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */ |
| 339 | lu_op_tx_insert_subscr_data(luop); |
| 340 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 344 | static int rx_purge_ms_req(struct osmo_gsup_conn *conn, |
| 345 | const struct osmo_gsup_message *gsup) |
| 346 | { |
| 347 | struct osmo_gsup_message gsup_reply = {0}; |
| 348 | struct msgb *msg_out; |
| 349 | bool is_ps = false; |
| 350 | int rc; |
| 351 | |
| 352 | LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi, |
| 353 | is_ps ? "PS" : "CS"); |
| 354 | |
| 355 | memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi)); |
| 356 | |
| 357 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) |
| 358 | is_ps = true; |
| 359 | |
| 360 | /* FIXME: check if the VLR that sends the purge is the same that |
| 361 | * we have on record. Only update if yes */ |
| 362 | |
| 363 | /* Perform the actual update of the DB */ |
Neels Hofmeyr | e50121e | 2017-10-09 17:48:51 +0200 | [diff] [blame] | 364 | rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, true, is_ps); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 365 | |
Harald Welte | 3f2a9a2 | 2018-03-01 23:35:35 +0100 | [diff] [blame] | 366 | if (rc == 0) |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 367 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT; |
Harald Welte | 3f2a9a2 | 2018-03-01 23:35:35 +0100 | [diff] [blame] | 368 | else if (rc == -ENOENT) { |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 369 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 370 | gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 371 | } else { |
| 372 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 373 | gsup_reply.cause = GMM_CAUSE_NET_FAIL; |
| 374 | } |
| 375 | |
| 376 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
| 377 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 378 | return osmo_gsup_conn_send(conn, msg_out); |
| 379 | } |
| 380 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 381 | static int gsup_send_err_reply(struct osmo_gsup_conn *conn, const char *imsi, |
| 382 | enum osmo_gsup_message_type type_in, uint8_t err_cause) |
| 383 | { |
Oliver Smith | f9cf180 | 2019-02-04 11:20:08 +0100 | [diff] [blame] | 384 | int type_err = OSMO_GSUP_TO_MSGT_ERROR(type_in); |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 385 | struct osmo_gsup_message gsup_reply = {0}; |
| 386 | struct msgb *msg_out; |
| 387 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 388 | OSMO_STRLCPY_ARRAY(gsup_reply.imsi, imsi); |
| 389 | gsup_reply.message_type = type_err; |
| 390 | gsup_reply.cause = err_cause; |
| 391 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP ERR response"); |
| 392 | OSMO_ASSERT(msg_out); |
| 393 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 394 | LOGP(DMAIN, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(type_err)); |
| 395 | return osmo_gsup_conn_send(conn, msg_out); |
| 396 | } |
| 397 | |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 398 | static int rx_check_imei_req(struct osmo_gsup_conn *conn, const struct osmo_gsup_message *gsup) |
| 399 | { |
| 400 | struct osmo_gsup_message gsup_reply = {0}; |
| 401 | struct msgb *msg_out; |
| 402 | char imei[GSM23003_IMEI_NUM_DIGITS+1] = {0}; |
| 403 | |
| 404 | /* Encoded IMEI length check */ |
| 405 | if (!gsup->imei_enc || gsup->imei_enc_len < 1 || gsup->imei_enc[0] >= sizeof(imei)) { |
| 406 | LOGP(DMAIN, LOGL_ERROR, "%s: wrong encoded IMEI length\n", gsup->imsi); |
| 407 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | /* Decode IMEI */ |
| 412 | if (gsm48_decode_bcd_number(imei, sizeof(imei), gsup->imei_enc, 0) < 0) { |
| 413 | LOGP(DMAIN, LOGL_ERROR, "%s: failed to decode IMEI\n", gsup->imsi); |
| 414 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 415 | return -1; |
| 416 | } |
| 417 | |
Oliver Smith | 851814a | 2019-01-11 15:30:21 +0100 | [diff] [blame] | 418 | /* Save in DB if desired */ |
| 419 | if (g_hlr->store_imei) { |
| 420 | LOGP(DAUC, LOGL_DEBUG, "IMSI='%s': storing IMEI = %s\n", gsup->imsi, imei); |
| 421 | if (db_subscr_update_imei_by_imsi(g_hlr->dbc, gsup->imsi, imei) < 0) { |
| 422 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 423 | return -1; |
| 424 | } |
| 425 | } else { |
| 426 | /* Check if subscriber exists and print IMEI */ |
| 427 | LOGP(DMAIN, LOGL_INFO, "IMSI='%s': has IMEI = %s (consider setting 'store-imei')\n", gsup->imsi, imei); |
| 428 | struct hlr_subscriber subscr; |
| 429 | if (db_subscr_get_by_imsi(g_hlr->dbc, gsup->imsi, &subscr) < 0) { |
| 430 | gsup_send_err_reply(conn, gsup->imsi, gsup->message_type, GMM_CAUSE_INV_MAND_INFO); |
| 431 | return -1; |
| 432 | } |
| 433 | } |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 434 | |
| 435 | /* Accept all IMEIs */ |
| 436 | gsup_reply.imei_result = OSMO_GSUP_IMEI_RESULT_ACK; |
| 437 | gsup_reply.message_type = OSMO_GSUP_MSGT_CHECK_IMEI_RESULT; |
| 438 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP Check_IMEI response"); |
| 439 | memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi)); |
| 440 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 441 | return osmo_gsup_conn_send(conn, msg_out); |
| 442 | } |
| 443 | |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 444 | static char namebuf[255]; |
| 445 | #define LOGP_GSUP_FWD(gsup, level, fmt, args ...) \ |
| 446 | LOGP(DMAIN, level, "Forward %s (class=%s, IMSI=%s, %s->%s): " fmt, \ |
| 447 | osmo_gsup_message_type_name(gsup->message_type), \ |
| 448 | osmo_gsup_message_class_name(gsup->message_class), \ |
| 449 | gsup->imsi, \ |
| 450 | osmo_quote_str((const char *)gsup->source_name, gsup->source_name_len), \ |
| 451 | osmo_quote_str_buf2(namebuf, sizeof(namebuf), (const char *)gsup->destination_name, gsup->destination_name_len), \ |
| 452 | ## args) |
| 453 | |
| 454 | static int read_cb_forward(struct osmo_gsup_conn *conn, struct msgb *msg, const struct osmo_gsup_message *gsup) |
| 455 | { |
| 456 | int ret = -EINVAL; |
| 457 | struct osmo_gsup_message *gsup_err; |
| 458 | |
| 459 | /* FIXME: it would be better if the msgb never were deallocated immediately by osmo_gsup_addr_send(), which a |
| 460 | * select-loop volatile talloc context could facilitate. Then we would still be able to access gsup-> members |
| 461 | * (pointing into the msgb) even after sending failed, and we wouldn't need to copy this data before sending: */ |
| 462 | /* Prepare error message (before IEs get deallocated) */ |
| 463 | gsup_err = talloc_zero(hlr_ctx, struct osmo_gsup_message); |
| 464 | OSMO_STRLCPY_ARRAY(gsup_err->imsi, gsup->imsi); |
| 465 | gsup_err->message_class = gsup->message_class; |
| 466 | gsup_err->destination_name = talloc_memdup(gsup_err, gsup->destination_name, gsup->destination_name_len); |
| 467 | gsup_err->destination_name_len = gsup->destination_name_len; |
Vadim Yanitskiy | 8625cda | 2019-05-07 21:09:01 +0700 | [diff] [blame] | 468 | gsup_err->message_type = gsup->message_type; |
Vadim Yanitskiy | 609978d | 2019-05-07 20:35:05 +0700 | [diff] [blame] | 469 | gsup_err->session_state = gsup->session_state; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 470 | gsup_err->session_id = gsup->session_id; |
| 471 | gsup_err->source_name = talloc_memdup(gsup_err, gsup->source_name, gsup->source_name_len); |
| 472 | gsup_err->source_name_len = gsup->source_name_len; |
| 473 | |
| 474 | /* Check for routing IEs */ |
| 475 | if (!gsup->source_name || !gsup->source_name_len || !gsup->destination_name || !gsup->destination_name_len) { |
| 476 | LOGP_GSUP_FWD(gsup, LOGL_ERROR, "missing routing IEs\n"); |
| 477 | goto end; |
| 478 | } |
| 479 | |
| 480 | /* Verify source name (e.g. "MSC-00-00-00-00-00-00") */ |
| 481 | if (gsup_route_find(conn->server, gsup->source_name, gsup->source_name_len) != conn) { |
| 482 | LOGP_GSUP_FWD(gsup, LOGL_ERROR, "mismatching source name\n"); |
| 483 | goto end; |
| 484 | } |
| 485 | |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 486 | /* Forward message without re-encoding (so we don't remove unknown IEs) */ |
| 487 | LOGP_GSUP_FWD(gsup, LOGL_INFO, "checks passed, forwarding\n"); |
| 488 | |
| 489 | /* Remove incoming IPA header to be able to prepend an outgoing IPA header */ |
| 490 | msgb_pull_to_l2(msg); |
| 491 | ret = osmo_gsup_addr_send(g_hlr->gs, gsup->destination_name, gsup->destination_name_len, msg); |
| 492 | /* AT THIS POINT, THE msg MAY BE DEALLOCATED and the data like gsup->imsi, gsup->source_name etc may all be |
| 493 | * invalid and cause segfaults. */ |
| 494 | msg = NULL; |
| 495 | gsup = NULL; |
| 496 | if (ret == -ENODEV) |
| 497 | LOGP_GSUP_FWD(gsup_err, LOGL_ERROR, "destination not connected\n"); |
| 498 | else if (ret) |
| 499 | LOGP_GSUP_FWD(gsup_err, LOGL_ERROR, "unknown error %i\n", ret); |
| 500 | |
| 501 | end: |
| 502 | /* Send error back to source */ |
| 503 | if (ret) { |
| 504 | struct msgb *msg_err = msgb_alloc_headroom(1024+16, 16, "GSUP forward ERR response"); |
| 505 | OSMO_ASSERT(msg_err); |
Vadim Yanitskiy | 8625cda | 2019-05-07 21:09:01 +0700 | [diff] [blame] | 506 | gsup_err->message_type = OSMO_GSUP_MSGT_E_ROUTING_ERROR; |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 507 | osmo_gsup_encode(msg_err, gsup_err); |
| 508 | LOGP_GSUP_FWD(gsup_err, LOGL_NOTICE, "Tx %s\n", osmo_gsup_message_type_name(gsup_err->message_type)); |
| 509 | osmo_gsup_conn_send(conn, msg_err); |
| 510 | } |
| 511 | talloc_free(gsup_err); |
| 512 | if (msg) |
| 513 | msgb_free(msg); |
| 514 | return ret; |
| 515 | } |
| 516 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 517 | static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 518 | { |
| 519 | static struct osmo_gsup_message gsup; |
| 520 | int rc; |
| 521 | |
Vadim Yanitskiy | c69a18b | 2019-05-07 20:53:54 +0700 | [diff] [blame] | 522 | if (!msgb_l2(msg) || !msgb_l2len(msg)) { |
| 523 | LOGP(DMAIN, LOGL_ERROR, "missing or empty L2 data\n"); |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame^] | 524 | msgb_free(msg); |
| 525 | return -EINVAL; |
Vadim Yanitskiy | c69a18b | 2019-05-07 20:53:54 +0700 | [diff] [blame] | 526 | } |
| 527 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 528 | rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 529 | if (rc < 0) { |
| 530 | LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc); |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame^] | 531 | msgb_free(msg); |
| 532 | return rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 535 | /* 3GPP TS 23.003 Section 2.2 clearly states that an IMSI with less than 5 |
| 536 | * digits is impossible. Even 5 digits is a highly theoretical case */ |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame^] | 537 | if (strlen(gsup.imsi) < 5) { /* TODO: move this check to libosmogsm/gsup.c? */ |
Neels Hofmeyr | 9f6e558 | 2019-04-03 18:16:50 +0200 | [diff] [blame] | 538 | LOGP(DMAIN, LOGL_ERROR, "IMSI too short: %s\n", osmo_quote_str(gsup.imsi, -1)); |
Vadim Yanitskiy | d9724f4 | 2019-05-07 21:05:18 +0700 | [diff] [blame^] | 539 | gsup_send_err_reply(conn, gsup.imsi, gsup.message_type, GMM_CAUSE_INV_MAND_INFO); |
| 540 | msgb_free(msg); |
| 541 | return -EINVAL; |
Neels Hofmeyr | 9f6e558 | 2019-04-03 18:16:50 +0200 | [diff] [blame] | 542 | } |
Harald Welte | a1d3b04 | 2018-06-11 20:28:35 +0200 | [diff] [blame] | 543 | |
Oliver Smith | 28f0af8 | 2019-02-22 11:46:33 +0100 | [diff] [blame] | 544 | if (gsup.destination_name_len) |
| 545 | return read_cb_forward(conn, msg, &gsup); |
| 546 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 547 | switch (gsup.message_type) { |
| 548 | /* requests sent to us */ |
| 549 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 550 | rx_send_auth_info(conn, &gsup, g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 551 | break; |
| 552 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 553 | rx_upd_loc_req(conn, &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 554 | break; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 555 | case OSMO_GSUP_MSGT_PURGE_MS_REQUEST: |
| 556 | rx_purge_ms_req(conn, &gsup); |
| 557 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 558 | /* responses to requests sent by us */ |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 559 | case OSMO_GSUP_MSGT_DELETE_DATA_ERROR: |
| 560 | LOGP(DMAIN, LOGL_ERROR, "Error while deleting subscriber data " |
| 561 | "for IMSI %s\n", gsup.imsi); |
| 562 | break; |
| 563 | case OSMO_GSUP_MSGT_DELETE_DATA_RESULT: |
| 564 | LOGP(DMAIN, LOGL_ERROR, "Deleting subscriber data for IMSI %s\n", |
| 565 | gsup.imsi); |
| 566 | break; |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 567 | case OSMO_GSUP_MSGT_PROC_SS_REQUEST: |
| 568 | case OSMO_GSUP_MSGT_PROC_SS_RESULT: |
| 569 | rx_proc_ss_req(conn, &gsup); |
| 570 | break; |
| 571 | case OSMO_GSUP_MSGT_PROC_SS_ERROR: |
| 572 | rx_proc_ss_error(conn, &gsup); |
| 573 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 574 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 575 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 576 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 577 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 578 | { |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 579 | struct lu_operation *luop = lu_op_by_imsi(gsup.imsi, |
| 580 | &g_lu_ops); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 581 | if (!luop) { |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 582 | LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for " |
| 583 | "unknown IMSI %s\n", |
| 584 | osmo_gsup_message_type_name(gsup.message_type), |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 585 | gsup.imsi); |
| 586 | break; |
| 587 | } |
| 588 | lu_op_rx_gsup(luop, &gsup); |
| 589 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 590 | break; |
Oliver Smith | 783ac81 | 2018-12-17 11:34:51 +0100 | [diff] [blame] | 591 | case OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST: |
| 592 | rx_check_imei_req(conn, &gsup); |
| 593 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 594 | default: |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 595 | LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n", |
| 596 | osmo_gsup_message_type_name(gsup.message_type)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 597 | break; |
| 598 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 599 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 603 | static void print_usage() |
| 604 | { |
| 605 | printf("Usage: osmo-hlr\n"); |
| 606 | } |
| 607 | |
| 608 | static void print_help() |
| 609 | { |
| 610 | printf(" -h --help This text.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 611 | printf(" -c --config-file filename The config file to use.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 612 | printf(" -l --database db-name The database to use.\n"); |
| 613 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n"); |
| 614 | printf(" -D --daemonize Fork the process into a background daemon.\n"); |
| 615 | printf(" -s --disable-color Do not print ANSI colors in the log\n"); |
| 616 | printf(" -T --timestamp Prefix every log line with a timestamp.\n"); |
| 617 | printf(" -e --log-level number Set a global loglevel.\n"); |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 618 | printf(" -U --db-upgrade Allow HLR database schema upgrades.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 619 | printf(" -V --version Print the version of OsmoHLR.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static struct { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 623 | const char *config_file; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 624 | const char *db_file; |
| 625 | bool daemonize; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 626 | bool db_upgrade; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 627 | } cmdline_opts = { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 628 | .config_file = "osmo-hlr.cfg", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 629 | .db_file = "hlr.db", |
| 630 | .daemonize = false, |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 631 | .db_upgrade = false, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | static void handle_options(int argc, char **argv) |
| 635 | { |
| 636 | while (1) { |
| 637 | int option_index = 0, c; |
| 638 | static struct option long_options[] = { |
| 639 | {"help", 0, 0, 'h'}, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 640 | {"config-file", 1, 0, 'c'}, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 641 | {"database", 1, 0, 'l'}, |
| 642 | {"debug", 1, 0, 'd'}, |
| 643 | {"daemonize", 0, 0, 'D'}, |
| 644 | {"disable-color", 0, 0, 's'}, |
| 645 | {"log-level", 1, 0, 'e'}, |
| 646 | {"timestamp", 0, 0, 'T'}, |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 647 | {"db-upgrade", 0, 0, 'U' }, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 648 | {"version", 0, 0, 'V' }, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 649 | {0, 0, 0, 0} |
| 650 | }; |
| 651 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 652 | c = getopt_long(argc, argv, "hc:l:d:Dse:TUV", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 653 | long_options, &option_index); |
| 654 | if (c == -1) |
| 655 | break; |
| 656 | |
| 657 | switch (c) { |
| 658 | case 'h': |
| 659 | print_usage(); |
| 660 | print_help(); |
| 661 | exit(0); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 662 | case 'c': |
| 663 | cmdline_opts.config_file = optarg; |
| 664 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 665 | case 'l': |
| 666 | cmdline_opts.db_file = optarg; |
| 667 | break; |
| 668 | case 'd': |
| 669 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 670 | break; |
| 671 | case 'D': |
| 672 | cmdline_opts.daemonize = 1; |
| 673 | break; |
| 674 | case 's': |
| 675 | log_set_use_color(osmo_stderr_target, 0); |
| 676 | break; |
| 677 | case 'e': |
| 678 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 679 | break; |
| 680 | case 'T': |
| 681 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 682 | break; |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 683 | case 'U': |
| 684 | cmdline_opts.db_upgrade = true; |
| 685 | break; |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 686 | case 'V': |
| 687 | print_version(1); |
| 688 | exit(0); |
| 689 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 690 | default: |
| 691 | /* catch unknown options *as well as* missing arguments. */ |
| 692 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 693 | exit(-1); |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 699 | static void signal_hdlr(int signal) |
| 700 | { |
| 701 | switch (signal) { |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 702 | case SIGTERM: |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 703 | case SIGINT: |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 704 | LOGP(DMAIN, LOGL_NOTICE, "Terminating due to signal=%d\n", signal); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 705 | quit++; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 706 | break; |
| 707 | case SIGUSR1: |
| 708 | LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n"); |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 709 | talloc_report_full(hlr_ctx, stderr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 710 | break; |
| 711 | } |
| 712 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 713 | |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 714 | static const char vlr_copyright[] = |
| 715 | "Copyright (C) 2016, 2017 by Harald Welte, sysmocom s.f.m.c. GmbH\r\n" |
| 716 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 717 | "This is free software: you are free to change and redistribute it.\r\n" |
| 718 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 719 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 720 | static struct vty_app_info vty_info = { |
| 721 | .name = "OsmoHLR", |
| 722 | .version = PACKAGE_VERSION, |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 723 | .copyright = vlr_copyright, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 724 | .is_config_node = hlr_vty_is_config_node, |
Pau Espin Pedrol | ce9bc40 | 2017-05-31 13:19:22 +0200 | [diff] [blame] | 725 | .go_parent_cb = hlr_vty_go_parent, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 726 | }; |
| 727 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 728 | int main(int argc, char **argv) |
| 729 | { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 730 | int rc; |
| 731 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 732 | /* Track the use of talloc NULL memory contexts */ |
| 733 | talloc_enable_null_tracking(); |
| 734 | |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 735 | hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR"); |
| 736 | msgb_talloc_ctx_init(hlr_ctx, 0); |
Harald Welte | 7ee6e55 | 2018-02-14 00:52:05 +0100 | [diff] [blame] | 737 | vty_info.tall_ctx = hlr_ctx; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 738 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 739 | g_hlr = talloc_zero(hlr_ctx, struct hlr); |
Harald Welte | 4956ae1 | 2018-06-15 22:04:28 +0200 | [diff] [blame] | 740 | INIT_LLIST_HEAD(&g_hlr->euse_list); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 741 | INIT_LLIST_HEAD(&g_hlr->iuse_list); |
Harald Welte | bb77939 | 2018-06-16 20:21:10 +0200 | [diff] [blame] | 742 | INIT_LLIST_HEAD(&g_hlr->ss_sessions); |
Harald Welte | dab544e | 2018-07-29 16:14:48 +0200 | [diff] [blame] | 743 | INIT_LLIST_HEAD(&g_hlr->ussd_routes); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 744 | |
Vadim Yanitskiy | d157a56 | 2018-12-01 00:03:39 +0700 | [diff] [blame] | 745 | /* Init default (call independent) SS session guard timeout value */ |
| 746 | g_hlr->ncss_guard_timeout = NCSS_GUARD_TIMEOUT_DEFAULT; |
| 747 | |
Pau Espin Pedrol | 5153031 | 2018-04-17 15:07:06 +0200 | [diff] [blame] | 748 | rc = osmo_init_logging2(hlr_ctx, &hlr_log_info); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 749 | if (rc < 0) { |
| 750 | fprintf(stderr, "Error initializing logging\n"); |
| 751 | exit(1); |
| 752 | } |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 753 | |
Max | 20ddfdb | 2019-02-18 13:12:27 +0100 | [diff] [blame] | 754 | osmo_stats_init(hlr_ctx); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 755 | vty_init(&vty_info); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 756 | ctrl_vty_init(hlr_ctx); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 757 | handle_options(argc, argv); |
Harald Welte | d5807b8 | 2018-07-29 12:27:41 +0200 | [diff] [blame] | 758 | hlr_vty_init(&hlr_log_info); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 759 | |
| 760 | rc = vty_read_config_file(cmdline_opts.config_file, NULL); |
| 761 | if (rc < 0) { |
| 762 | LOGP(DMAIN, LOGL_FATAL, |
| 763 | "Failed to parse the config file: '%s'\n", |
| 764 | cmdline_opts.config_file); |
| 765 | return rc; |
| 766 | } |
| 767 | |
| 768 | /* start telnet after reading config for vty_get_bind_addr() */ |
| 769 | rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(), |
| 770 | OSMO_VTY_PORT_HLR); |
| 771 | if (rc < 0) |
| 772 | return rc; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 773 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 774 | LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n"); |
| 775 | |
| 776 | rc = rand_init(); |
| 777 | if (rc < 0) { |
| 778 | LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n"); |
| 779 | exit(1); |
| 780 | } |
| 781 | |
Stefan Sperling | 8f3a7cc | 2018-11-27 12:10:45 +0100 | [diff] [blame] | 782 | 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] | 783 | if (!g_hlr->dbc) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 784 | LOGP(DMAIN, LOGL_FATAL, "Error opening database\n"); |
| 785 | exit(1); |
| 786 | } |
| 787 | |
Neels Hofmeyr | 84201d3 | 2017-07-21 16:00:32 +0200 | [diff] [blame] | 788 | 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] | 789 | read_cb, &g_lu_ops, g_hlr); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 790 | if (!g_hlr->gs) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 791 | LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n"); |
| 792 | exit(1); |
| 793 | } |
| 794 | |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 795 | g_hlr->ctrl_bind_addr = ctrl_vty_get_bind_addr(); |
Neels Hofmeyr | 234f9cb | 2017-10-24 17:23:04 +0200 | [diff] [blame] | 796 | g_hlr->ctrl = hlr_controlif_setup(g_hlr); |
Max | 372868b | 2017-03-02 12:12:00 +0100 | [diff] [blame] | 797 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 798 | osmo_init_ignore_signals(); |
| 799 | signal(SIGINT, &signal_hdlr); |
Vadim Yanitskiy | ee7c0cb | 2019-03-19 18:10:51 +0700 | [diff] [blame] | 800 | signal(SIGTERM, &signal_hdlr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 801 | signal(SIGUSR1, &signal_hdlr); |
| 802 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 803 | if (cmdline_opts.daemonize) { |
| 804 | rc = osmo_daemonize(); |
| 805 | if (rc < 0) { |
| 806 | perror("Error during daemonize"); |
| 807 | exit(1); |
| 808 | } |
| 809 | } |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 810 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 811 | while (!quit) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 812 | osmo_select_main(0); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 813 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 814 | osmo_gsup_server_destroy(g_hlr->gs); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 815 | db_close(g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 816 | log_fini(); |
| 817 | |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 818 | /** |
| 819 | * Report the heap state of root context, then free, |
| 820 | * so both ASAN and Valgrind are happy... |
| 821 | */ |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 822 | talloc_report_full(hlr_ctx, stderr); |
Vadim Yanitskiy | 4793a7e | 2018-07-30 03:04:34 +0700 | [diff] [blame] | 823 | talloc_free(hlr_ctx); |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 824 | |
Vadim Yanitskiy | 9fdb854 | 2018-07-30 03:09:22 +0700 | [diff] [blame] | 825 | /* FIXME: VTY code still uses NULL-context */ |
| 826 | talloc_free(tall_vty_ctx); |
| 827 | |
| 828 | /** |
| 829 | * Report the heap state of NULL context, then free, |
| 830 | * so both ASAN and Valgrind are happy... |
| 831 | */ |
| 832 | talloc_report_full(NULL, stderr); |
| 833 | talloc_disable_null_tracking(); |
| 834 | |
Vadim Yanitskiy | 527d934 | 2018-07-30 02:42:25 +0700 | [diff] [blame] | 835 | return 0; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 836 | } |