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