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