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> |
Max | 2fc63a6 | 2016-12-20 16:49:20 +0100 | [diff] [blame] | 29 | #include <osmocom/gsm/apn.h> |
Neels Hofmeyr | 627de84 | 2016-12-19 13:16:06 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm48_ie.h> |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 31 | #include <osmocom/vty/vty.h> |
| 32 | #include <osmocom/vty/command.h> |
| 33 | #include <osmocom/vty/telnet_interface.h> |
| 34 | #include <osmocom/vty/ports.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" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 38 | #include "logging.h" |
| 39 | #include "gsup_server.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 40 | #include "gsup_router.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 41 | #include "rand.h" |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 42 | #include "luop.h" |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 43 | #include "hlr_vty.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 44 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 45 | static struct hlr *g_hlr; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 46 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 47 | /*********************************************************************** |
| 48 | * Send Auth Info handling |
| 49 | ***********************************************************************/ |
| 50 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 51 | /* process an incoming SAI request */ |
| 52 | static int rx_send_auth_info(struct osmo_gsup_conn *conn, |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 53 | const struct osmo_gsup_message *gsup, |
| 54 | struct db_context *dbc) |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 55 | { |
| 56 | struct osmo_gsup_message gsup_out; |
| 57 | struct msgb *msg_out; |
| 58 | int rc; |
| 59 | |
| 60 | /* initialize return message structure */ |
| 61 | memset(&gsup_out, 0, sizeof(gsup_out)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 62 | memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi)); |
| 63 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 64 | rc = db_get_auc(dbc, gsup->imsi, gsup_out.auth_vectors, |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 65 | ARRAY_SIZE(gsup_out.auth_vectors), |
Harald Welte | 9be0d2f | 2016-06-10 17:34:02 +0200 | [diff] [blame] | 66 | gsup->rand, gsup->auts); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 67 | if (rc < 0) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 68 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 69 | gsup_out.cause = GMM_CAUSE_NET_FAIL; |
| 70 | } else if (rc == 0) { |
| 71 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
| 72 | gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN; |
Harald Welte | 15db826 | 2016-05-05 16:50:39 +0200 | [diff] [blame] | 73 | } else { |
| 74 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT; |
| 75 | gsup_out.num_auth_vectors = rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 78 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 79 | osmo_gsup_encode(msg_out, &gsup_out); |
| 80 | return osmo_gsup_conn_send(conn, msg_out); |
| 81 | } |
| 82 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 83 | /*********************************************************************** |
| 84 | * LU Operation State / Structure |
| 85 | ***********************************************************************/ |
| 86 | |
| 87 | static LLIST_HEAD(g_lu_ops); |
| 88 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 89 | /*! Receive Cancel Location Result from old VLR/SGSN */ |
| 90 | void lu_op_rx_cancel_old_ack(struct lu_operation *luop, |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 91 | const struct osmo_gsup_message *gsup) |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 92 | { |
| 93 | OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT); |
| 94 | /* FIXME: Check for spoofing */ |
| 95 | |
| 96 | osmo_timer_del(&luop->timer); |
| 97 | |
| 98 | /* FIXME */ |
| 99 | |
| 100 | lu_op_tx_insert_subscr_data(luop); |
| 101 | } |
| 102 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 103 | /*! Receive Insert Subscriber Data Result from new VLR/SGSN */ |
| 104 | static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop, |
| 105 | const struct osmo_gsup_message *gsup) |
| 106 | { |
| 107 | OSMO_ASSERT(luop->state == LU_S_ISD_SENT); |
| 108 | /* FIXME: Check for spoofing */ |
| 109 | |
| 110 | osmo_timer_del(&luop->timer); |
| 111 | |
| 112 | /* Subscriber_Present_HLR */ |
| 113 | /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */ |
| 114 | |
| 115 | /* Send final ACK towards inquiring VLR/SGSN */ |
| 116 | lu_op_tx_ack(luop); |
| 117 | } |
| 118 | |
| 119 | /*! Receive GSUP message for given \ref lu_operation */ |
| 120 | void lu_op_rx_gsup(struct lu_operation *luop, |
| 121 | const struct osmo_gsup_message *gsup) |
| 122 | { |
| 123 | switch (gsup->message_type) { |
| 124 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
| 125 | /* FIXME */ |
| 126 | break; |
| 127 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
| 128 | lu_op_rx_insert_subscr_data_ack(luop, gsup); |
| 129 | break; |
| 130 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 131 | /* FIXME */ |
| 132 | break; |
| 133 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 134 | lu_op_rx_cancel_old_ack(luop, gsup); |
| 135 | break; |
| 136 | default: |
| 137 | LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n", |
| 138 | gsup->message_type); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 143 | /*! Receive Update Location Request, creates new \ref lu_operation */ |
| 144 | static int rx_upd_loc_req(struct osmo_gsup_conn *conn, |
| 145 | const struct osmo_gsup_message *gsup) |
| 146 | { |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 147 | struct lu_operation *luop = lu_op_alloc_conn(conn); |
| 148 | if (!luop) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 149 | LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n"); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 150 | return -EINVAL; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 153 | lu_op_statechg(luop, LU_S_LU_RECEIVED); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 154 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 155 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) |
| 156 | luop->is_ps = true; |
| 157 | llist_add(&luop->list, &g_lu_ops); |
| 158 | |
| 159 | /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */ |
| 160 | |
| 161 | /* check if subscriber is known at all */ |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 162 | if (!lu_op_fill_subscr(luop, g_hlr->dbc, gsup->imsi)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 163 | /* Send Error back: Subscriber Unknown in HLR */ |
| 164 | strcpy(luop->subscr.imsi, gsup->imsi); |
| 165 | lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN); |
| 166 | return 0; |
| 167 | } |
| 168 | |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 169 | /* Check if subscriber is generally permitted on CS or PS |
| 170 | * service (as requested) */ |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 171 | if (!luop->is_ps && !luop->subscr.nam_cs) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 172 | lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED); |
| 173 | return 0; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 174 | } else if (luop->is_ps && !luop->subscr.nam_ps) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 175 | lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* TODO: Set subscriber tracing = deactive in VLR/SGSN */ |
| 180 | |
| 181 | #if 0 |
| 182 | /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */ |
| 183 | if (luop->is_ps == false && |
| 184 | strcmp(subscr->vlr_number, vlr_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 185 | lu_op_tx_cancel_old(luop); |
| 186 | } else if (luop->is_ps == true && |
| 187 | strcmp(subscr->sgsn_number, sgsn_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 188 | lu_op_tx_cancel_old(luop); |
| 189 | } else |
| 190 | #endif |
| 191 | { |
| 192 | /* TODO: Subscriber allowed to roam in PLMN? */ |
| 193 | /* TODO: Update RoutingInfo */ |
| 194 | /* TODO: Reset Flag MS Purged (cs/ps) */ |
| 195 | /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */ |
| 196 | lu_op_tx_insert_subscr_data(luop); |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 201 | static int rx_purge_ms_req(struct osmo_gsup_conn *conn, |
| 202 | const struct osmo_gsup_message *gsup) |
| 203 | { |
| 204 | struct osmo_gsup_message gsup_reply = {0}; |
| 205 | struct msgb *msg_out; |
| 206 | bool is_ps = false; |
| 207 | int rc; |
| 208 | |
| 209 | LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi, |
| 210 | is_ps ? "PS" : "CS"); |
| 211 | |
| 212 | memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi)); |
| 213 | |
| 214 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) |
| 215 | is_ps = true; |
| 216 | |
| 217 | /* FIXME: check if the VLR that sends the purge is the same that |
| 218 | * we have on record. Only update if yes */ |
| 219 | |
| 220 | /* Perform the actual update of the DB */ |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 221 | rc = db_subscr_purge(g_hlr->dbc, gsup->imsi, is_ps); |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 222 | |
| 223 | if (rc == 1) |
| 224 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT; |
| 225 | else if (rc == 0) { |
| 226 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 227 | gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 228 | } else { |
| 229 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 230 | gsup_reply.cause = GMM_CAUSE_NET_FAIL; |
| 231 | } |
| 232 | |
| 233 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
| 234 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 235 | return osmo_gsup_conn_send(conn, msg_out); |
| 236 | } |
| 237 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 238 | static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 239 | { |
| 240 | static struct osmo_gsup_message gsup; |
| 241 | int rc; |
| 242 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 243 | rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 244 | if (rc < 0) { |
| 245 | LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc); |
| 246 | return rc; |
| 247 | } |
| 248 | |
| 249 | switch (gsup.message_type) { |
| 250 | /* requests sent to us */ |
| 251 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 252 | rx_send_auth_info(conn, &gsup, g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 253 | break; |
| 254 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 255 | rx_upd_loc_req(conn, &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 256 | break; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 257 | case OSMO_GSUP_MSGT_PURGE_MS_REQUEST: |
| 258 | rx_purge_ms_req(conn, &gsup); |
| 259 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 260 | /* responses to requests sent by us */ |
| 261 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 262 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 263 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 264 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 265 | { |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 266 | struct lu_operation *luop = lu_op_by_imsi(gsup.imsi, |
| 267 | &g_lu_ops); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 268 | if (!luop) { |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 269 | LOGP(DMAIN, LOGL_ERROR, "GSUP message %s for " |
| 270 | "unknown IMSI %s\n", |
| 271 | osmo_gsup_message_type_name(gsup.message_type), |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 272 | gsup.imsi); |
| 273 | break; |
| 274 | } |
| 275 | lu_op_rx_gsup(luop, &gsup); |
| 276 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 277 | break; |
| 278 | default: |
Max | aa0fefd | 2017-02-16 12:25:22 +0100 | [diff] [blame] | 279 | LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %s\n", |
| 280 | osmo_gsup_message_type_name(gsup.message_type)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 281 | break; |
| 282 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 283 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 287 | static void print_usage() |
| 288 | { |
| 289 | printf("Usage: osmo-hlr\n"); |
| 290 | } |
| 291 | |
| 292 | static void print_help() |
| 293 | { |
| 294 | printf(" -h --help This text.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 295 | printf(" -c --config-file filename The config file to use.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 296 | printf(" -l --database db-name The database to use.\n"); |
| 297 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n"); |
| 298 | printf(" -D --daemonize Fork the process into a background daemon.\n"); |
| 299 | printf(" -s --disable-color Do not print ANSI colors in the log\n"); |
| 300 | printf(" -T --timestamp Prefix every log line with a timestamp.\n"); |
| 301 | printf(" -e --log-level number Set a global loglevel.\n"); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 302 | printf(" -V --version Print the version of OsmoHLR.\n"); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static struct { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 306 | const char *config_file; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 307 | const char *db_file; |
| 308 | bool daemonize; |
| 309 | } cmdline_opts = { |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 310 | .config_file = "osmo-hlr.cfg", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 311 | .db_file = "hlr.db", |
| 312 | .daemonize = false, |
| 313 | }; |
| 314 | |
| 315 | static void handle_options(int argc, char **argv) |
| 316 | { |
| 317 | while (1) { |
| 318 | int option_index = 0, c; |
| 319 | static struct option long_options[] = { |
| 320 | {"help", 0, 0, 'h'}, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 321 | {"config-file", 1, 0, 'c'}, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 322 | {"database", 1, 0, 'l'}, |
| 323 | {"debug", 1, 0, 'd'}, |
| 324 | {"daemonize", 0, 0, 'D'}, |
| 325 | {"disable-color", 0, 0, 's'}, |
| 326 | {"log-level", 1, 0, 'e'}, |
| 327 | {"timestamp", 0, 0, 'T'}, |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 328 | {"version", 0, 0, 'V' }, |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 329 | {0, 0, 0, 0} |
| 330 | }; |
| 331 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 332 | c = getopt_long(argc, argv, "hc:l:d:Dse:TV", |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 333 | long_options, &option_index); |
| 334 | if (c == -1) |
| 335 | break; |
| 336 | |
| 337 | switch (c) { |
| 338 | case 'h': |
| 339 | print_usage(); |
| 340 | print_help(); |
| 341 | exit(0); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 342 | case 'c': |
| 343 | cmdline_opts.config_file = optarg; |
| 344 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 345 | case 'l': |
| 346 | cmdline_opts.db_file = optarg; |
| 347 | break; |
| 348 | case 'd': |
| 349 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 350 | break; |
| 351 | case 'D': |
| 352 | cmdline_opts.daemonize = 1; |
| 353 | break; |
| 354 | case 's': |
| 355 | log_set_use_color(osmo_stderr_target, 0); |
| 356 | break; |
| 357 | case 'e': |
| 358 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 359 | break; |
| 360 | case 'T': |
| 361 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 362 | break; |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 363 | case 'V': |
| 364 | print_version(1); |
| 365 | exit(0); |
| 366 | break; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 367 | default: |
| 368 | /* catch unknown options *as well as* missing arguments. */ |
| 369 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 370 | exit(-1); |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 376 | static void *hlr_ctx = NULL; |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 377 | |
| 378 | static void signal_hdlr(int signal) |
| 379 | { |
| 380 | switch (signal) { |
| 381 | case SIGINT: |
| 382 | LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n"); |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 383 | osmo_gsup_server_destroy(g_hlr->gs); |
| 384 | db_close(g_hlr->dbc); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 385 | log_fini(); |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 386 | talloc_report_full(hlr_ctx, stderr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 387 | exit(0); |
| 388 | break; |
| 389 | case SIGUSR1: |
| 390 | LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n"); |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 391 | talloc_report_full(hlr_ctx, stderr); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 392 | break; |
| 393 | } |
| 394 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 395 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 396 | static struct vty_app_info vty_info = { |
| 397 | .name = "OsmoHLR", |
| 398 | .version = PACKAGE_VERSION, |
| 399 | .is_config_node = hlr_vty_is_config_node, |
| 400 | }; |
| 401 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 402 | int main(int argc, char **argv) |
| 403 | { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 404 | int rc; |
| 405 | |
Neels Hofmeyr | ca43e30 | 2017-01-30 13:18:23 +0100 | [diff] [blame] | 406 | hlr_ctx = talloc_named_const(NULL, 1, "OsmoHLR"); |
| 407 | msgb_talloc_ctx_init(hlr_ctx, 0); |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 408 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 409 | g_hlr = talloc_zero(hlr_ctx, struct hlr); |
| 410 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 411 | rc = osmo_init_logging(&hlr_log_info); |
| 412 | if (rc < 0) { |
| 413 | fprintf(stderr, "Error initializing logging\n"); |
| 414 | exit(1); |
| 415 | } |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 416 | |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 417 | vty_init(&vty_info); |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 418 | handle_options(argc, argv); |
Neels Hofmeyr | 7685a78 | 2017-01-30 23:30:26 +0100 | [diff] [blame] | 419 | hlr_vty_init(&hlr_log_info); |
| 420 | |
| 421 | rc = vty_read_config_file(cmdline_opts.config_file, NULL); |
| 422 | if (rc < 0) { |
| 423 | LOGP(DMAIN, LOGL_FATAL, |
| 424 | "Failed to parse the config file: '%s'\n", |
| 425 | cmdline_opts.config_file); |
| 426 | return rc; |
| 427 | } |
| 428 | |
| 429 | /* start telnet after reading config for vty_get_bind_addr() */ |
| 430 | rc = telnet_init_dynif(hlr_ctx, NULL, vty_get_bind_addr(), |
| 431 | OSMO_VTY_PORT_HLR); |
| 432 | if (rc < 0) |
| 433 | return rc; |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 434 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 435 | LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n"); |
| 436 | |
| 437 | rc = rand_init(); |
| 438 | if (rc < 0) { |
| 439 | LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n"); |
| 440 | exit(1); |
| 441 | } |
| 442 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 443 | g_hlr->dbc = db_open(hlr_ctx, cmdline_opts.db_file); |
| 444 | if (!g_hlr->dbc) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 445 | LOGP(DMAIN, LOGL_FATAL, "Error opening database\n"); |
| 446 | exit(1); |
| 447 | } |
| 448 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 449 | g_hlr->gs = osmo_gsup_server_create(hlr_ctx, NULL, 2222, read_cb); |
| 450 | if (!g_hlr->gs) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 451 | LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n"); |
| 452 | exit(1); |
| 453 | } |
| 454 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 455 | osmo_init_ignore_signals(); |
| 456 | signal(SIGINT, &signal_hdlr); |
| 457 | signal(SIGUSR1, &signal_hdlr); |
| 458 | |
Neels Hofmeyr | 7f9491f | 2017-01-30 13:30:47 +0100 | [diff] [blame] | 459 | if (cmdline_opts.daemonize) { |
| 460 | rc = osmo_daemonize(); |
| 461 | if (rc < 0) { |
| 462 | perror("Error during daemonize"); |
| 463 | exit(1); |
| 464 | } |
| 465 | } |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 466 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 467 | while (1) { |
| 468 | osmo_select_main(0); |
| 469 | } |
| 470 | |
Max | d4bebbd | 2017-03-02 12:00:19 +0100 | [diff] [blame] | 471 | db_close(g_hlr->dbc); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 472 | |
| 473 | log_fini(); |
| 474 | |
| 475 | exit(0); |
| 476 | } |