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> |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 22 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 23 | #include <osmocom/core/msgb.h> |
| 24 | #include <osmocom/core/logging.h> |
| 25 | #include <osmocom/core/application.h> |
| 26 | #include <osmocom/gsm/gsup.h> |
Max | 2fc63a6 | 2016-12-20 16:49:20 +0100 | [diff] [blame^] | 27 | #include <osmocom/gsm/apn.h> |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 28 | |
| 29 | #include "db.h" |
| 30 | #include "logging.h" |
| 31 | #include "gsup_server.h" |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 32 | #include "gsup_router.h" |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 33 | #include "rand.h" |
| 34 | |
| 35 | static struct db_context *g_dbc; |
| 36 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 37 | /*********************************************************************** |
| 38 | * Send Auth Info handling |
| 39 | ***********************************************************************/ |
| 40 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 41 | /* process an incoming SAI request */ |
| 42 | static int rx_send_auth_info(struct osmo_gsup_conn *conn, |
| 43 | const struct osmo_gsup_message *gsup) |
| 44 | { |
| 45 | struct osmo_gsup_message gsup_out; |
| 46 | struct msgb *msg_out; |
| 47 | int rc; |
| 48 | |
| 49 | /* initialize return message structure */ |
| 50 | memset(&gsup_out, 0, sizeof(gsup_out)); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 51 | memcpy(&gsup_out.imsi, &gsup->imsi, sizeof(gsup_out.imsi)); |
| 52 | |
| 53 | rc = db_get_auc(g_dbc, gsup->imsi, gsup_out.auth_vectors, |
| 54 | ARRAY_SIZE(gsup_out.auth_vectors), |
Harald Welte | 9be0d2f | 2016-06-10 17:34:02 +0200 | [diff] [blame] | 55 | gsup->rand, gsup->auts); |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 56 | if (rc < 0) { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 57 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
Harald Welte | cfc752b | 2016-05-05 16:38:14 +0200 | [diff] [blame] | 58 | gsup_out.cause = GMM_CAUSE_NET_FAIL; |
| 59 | } else if (rc == 0) { |
| 60 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR; |
| 61 | gsup_out.cause = GMM_CAUSE_IMSI_UNKNOWN; |
Harald Welte | 15db826 | 2016-05-05 16:50:39 +0200 | [diff] [blame] | 62 | } else { |
| 63 | gsup_out.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT; |
| 64 | gsup_out.num_auth_vectors = rc; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 67 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 68 | osmo_gsup_encode(msg_out, &gsup_out); |
| 69 | return osmo_gsup_conn_send(conn, msg_out); |
| 70 | } |
| 71 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 72 | /*********************************************************************** |
| 73 | * LU Operation State / Structure |
| 74 | ***********************************************************************/ |
| 75 | |
| 76 | static LLIST_HEAD(g_lu_ops); |
| 77 | |
| 78 | #define CANCEL_TIMEOUT_SECS 30 |
| 79 | #define ISD_TIMEOUT_SECS 30 |
| 80 | |
| 81 | enum lu_state { |
| 82 | LU_S_NULL, |
| 83 | LU_S_LU_RECEIVED, |
| 84 | LU_S_CANCEL_SENT, |
| 85 | LU_S_CANCEL_ACK_RECEIVED, |
| 86 | LU_S_ISD_SENT, |
| 87 | LU_S_ISD_ACK_RECEIVED, |
| 88 | LU_S_COMPLETE, |
| 89 | }; |
| 90 | |
| 91 | static const struct value_string lu_state_names[] = { |
| 92 | { LU_S_NULL, "NULL" }, |
| 93 | { LU_S_LU_RECEIVED, "LU RECEIVED" }, |
| 94 | { LU_S_CANCEL_SENT, "CANCEL SENT" }, |
| 95 | { LU_S_CANCEL_ACK_RECEIVED, "CANCEL-ACK RECEIVED" }, |
| 96 | { LU_S_ISD_SENT, "ISD SENT" }, |
| 97 | { LU_S_ISD_ACK_RECEIVED, "ISD-ACK RECEIVED" }, |
| 98 | { LU_S_COMPLETE, "COMPLETE" }, |
| 99 | { 0, NULL } |
| 100 | }; |
| 101 | |
| 102 | struct lu_operation { |
| 103 | /*! entry in global list of location update operations */ |
| 104 | struct llist_head list; |
| 105 | /*! to which gsup_server do we belong */ |
| 106 | struct osmo_gsup_server *gsup_server; |
| 107 | /*! state of the location update */ |
| 108 | enum lu_state state; |
| 109 | /*! CS (false) or PS (true) Location Update? */ |
| 110 | bool is_ps; |
| 111 | /*! currently running timer */ |
| 112 | struct osmo_timer_list timer; |
| 113 | |
| 114 | /*! subscriber related to this operation */ |
| 115 | struct hlr_subscriber subscr; |
| 116 | /*! peer VLR/SGSN starting the request */ |
| 117 | uint8_t *peer; |
| 118 | }; |
| 119 | |
Neels Hofmeyr | 6eed322 | 2016-12-11 01:21:49 +0100 | [diff] [blame] | 120 | void lu_op_tx_insert_subscr_data(struct lu_operation *luop); |
| 121 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 122 | void lu_op_statechg(struct lu_operation *luop, enum lu_state new_state) |
| 123 | { |
| 124 | enum lu_state old_state = luop->state; |
| 125 | |
| 126 | DEBUGP(DMAIN, "LU OP state change: %s -> ", |
| 127 | get_value_string(lu_state_names, old_state)); |
| 128 | DEBUGPC(DMAIN, "%s\n", |
| 129 | get_value_string(lu_state_names, new_state)); |
| 130 | |
| 131 | luop->state = new_state; |
| 132 | } |
| 133 | |
| 134 | struct lu_operation *lu_op_by_imsi(const char *imsi) |
| 135 | { |
| 136 | struct lu_operation *luop; |
| 137 | |
| 138 | llist_for_each_entry(luop, &g_lu_ops, list) { |
| 139 | if (!strcmp(imsi, luop->subscr.imsi)) |
| 140 | return luop; |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | /* Send a msgb to a given address using routing */ |
| 146 | int osmo_gsup_addr_send(struct osmo_gsup_server *gs, |
| 147 | const uint8_t *addr, size_t addrlen, |
| 148 | struct msgb *msg) |
| 149 | { |
| 150 | struct osmo_gsup_conn *conn; |
| 151 | |
| 152 | conn = gsup_route_find(gs, addr, addrlen); |
| 153 | if (!conn) { |
| 154 | DEBUGP(DMAIN, "Cannot find route for addr %s\n", addr); |
| 155 | msgb_free(msg); |
| 156 | return -ENODEV; |
| 157 | } |
| 158 | |
| 159 | return osmo_gsup_conn_send(conn, msg); |
| 160 | } |
| 161 | |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 162 | /* Transmit a given GSUP message for the given LU operation */ |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 163 | static void _luop_tx_gsup(struct lu_operation *luop, |
| 164 | const struct osmo_gsup_message *gsup) |
| 165 | { |
| 166 | struct msgb *msg_out; |
| 167 | |
| 168 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP LUOP"); |
| 169 | osmo_gsup_encode(msg_out, gsup); |
| 170 | |
| 171 | osmo_gsup_addr_send(luop->gsup_server, luop->peer, |
| 172 | talloc_total_size(luop->peer), |
| 173 | msg_out); |
| 174 | } |
| 175 | |
| 176 | /*! Transmit UPD_LOC_ERROR and destroy lu_operation */ |
| 177 | void lu_op_tx_error(struct lu_operation *luop, enum gsm48_gmm_cause cause) |
| 178 | { |
| 179 | struct osmo_gsup_message gsup; |
| 180 | |
| 181 | DEBUGP(DMAIN, "%s: LU OP Tx Error (cause=%u)\n", |
| 182 | luop->subscr.imsi, cause); |
| 183 | |
| 184 | memset(&gsup, 0, sizeof(gsup)); |
| 185 | gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR; |
Neels Hofmeyr | ec1b959 | 2016-12-11 01:22:23 +0100 | [diff] [blame] | 186 | strncpy((char*)&gsup.imsi, luop->subscr.imsi, sizeof(gsup.imsi)); |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 187 | gsup.imsi[sizeof(gsup.imsi)-1] = '\0'; |
| 188 | gsup.cause = cause; |
| 189 | |
| 190 | _luop_tx_gsup(luop, &gsup); |
| 191 | |
| 192 | llist_del(&luop->list); |
| 193 | talloc_free(luop); |
| 194 | } |
| 195 | |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 196 | /* timer call-back in case LU operation doesn't receive an response */ |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 197 | static void lu_op_timer_cb(void *data) |
| 198 | { |
| 199 | struct lu_operation *luop = data; |
| 200 | |
| 201 | DEBUGP(DMAIN, "LU OP timer expired in state %s\n", |
| 202 | get_value_string(lu_state_names, luop->state)); |
| 203 | |
| 204 | switch (luop->state) { |
| 205 | case LU_S_CANCEL_SENT: |
| 206 | break; |
| 207 | case LU_S_ISD_SENT: |
| 208 | break; |
| 209 | default: |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | lu_op_tx_error(luop, GMM_CAUSE_NET_FAIL); |
| 214 | } |
| 215 | |
| 216 | /*! Transmit UPD_LOC_RESULT and destroy lu_operation */ |
| 217 | void lu_op_tx_ack(struct lu_operation *luop) |
| 218 | { |
| 219 | struct osmo_gsup_message gsup; |
| 220 | |
| 221 | memset(&gsup, 0, sizeof(gsup)); |
| 222 | gsup.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT; |
| 223 | strncpy(gsup.imsi, luop->subscr.imsi, sizeof(gsup.imsi)-1); |
| 224 | //FIXME gsup.hlr_enc; |
| 225 | |
| 226 | _luop_tx_gsup(luop, &gsup); |
| 227 | |
| 228 | llist_del(&luop->list); |
| 229 | talloc_free(luop); |
| 230 | } |
| 231 | |
| 232 | /*! Send Cancel Location to old VLR/SGSN */ |
| 233 | void lu_op_tx_cancel_old(struct lu_operation *luop) |
| 234 | { |
| 235 | struct osmo_gsup_message gsup; |
| 236 | |
| 237 | OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED); |
| 238 | |
| 239 | memset(&gsup, 0, sizeof(gsup)); |
| 240 | gsup.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST; |
| 241 | //gsup.cause = FIXME; |
| 242 | //gsup.cancel_type = FIXME; |
| 243 | |
| 244 | _luop_tx_gsup(luop, &gsup); |
| 245 | |
| 246 | lu_op_statechg(luop, LU_S_CANCEL_SENT); |
| 247 | osmo_timer_schedule(&luop->timer, CANCEL_TIMEOUT_SECS, 0); |
| 248 | } |
| 249 | |
| 250 | /*! Receive Cancel Location Result from old VLR/SGSN */ |
| 251 | void lu_op_rx_cancel_old_ack(struct lu_operation *luop, |
| 252 | const struct osmo_gsup_message *gsup) |
| 253 | { |
| 254 | OSMO_ASSERT(luop->state == LU_S_CANCEL_SENT); |
| 255 | /* FIXME: Check for spoofing */ |
| 256 | |
| 257 | osmo_timer_del(&luop->timer); |
| 258 | |
| 259 | /* FIXME */ |
| 260 | |
| 261 | lu_op_tx_insert_subscr_data(luop); |
| 262 | } |
| 263 | |
| 264 | /*! Transmit Insert Subscriber Data to new VLR/SGSN */ |
| 265 | void lu_op_tx_insert_subscr_data(struct lu_operation *luop) |
| 266 | { |
| 267 | struct osmo_gsup_message gsup; |
Max | 2fc63a6 | 2016-12-20 16:49:20 +0100 | [diff] [blame^] | 268 | uint8_t apn[APN_MAXLEN]; |
| 269 | int l; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 270 | |
| 271 | OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED || |
| 272 | luop->state == LU_S_CANCEL_ACK_RECEIVED); |
| 273 | |
| 274 | memset(&gsup, 0, sizeof(gsup)); |
| 275 | gsup.message_type = OSMO_GSUP_MSGT_INSERT_DATA_REQUEST; |
| 276 | strncpy(gsup.imsi, luop->subscr.imsi, sizeof(gsup.imsi)-1); |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 277 | /* FIXME: deal with encoding the following data */ |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 278 | gsup.msisdn_enc; |
| 279 | gsup.hlr_enc; |
| 280 | |
| 281 | if (luop->is_ps) { |
Max | 2fc63a6 | 2016-12-20 16:49:20 +0100 | [diff] [blame^] | 282 | /* FIXME: PDP infos - use more fine-grained access control |
| 283 | instead of wildcard APN */ |
| 284 | l = osmo_apn_from_str(apn, sizeof(apn), "*"); |
| 285 | if (l > 0) { |
| 286 | gsup.pdp_infos[0].apn_enc = apn; |
| 287 | gsup.pdp_infos[0].apn_enc_len = l; |
| 288 | gsup.pdp_infos[0].have_info = 1; |
| 289 | gsup.num_pdp_infos = 1; |
| 290 | /* FIXME: use real value: */ |
| 291 | gsup.pdp_infos[0].context_id = 1; |
| 292 | } |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* Send ISD to new VLR/SGSN */ |
| 296 | _luop_tx_gsup(luop, &gsup); |
| 297 | |
| 298 | lu_op_statechg(luop, LU_S_ISD_SENT); |
| 299 | osmo_timer_schedule(&luop->timer, ISD_TIMEOUT_SECS, 0); |
| 300 | } |
| 301 | |
| 302 | /*! Receive Insert Subscriber Data Result from new VLR/SGSN */ |
| 303 | static void lu_op_rx_insert_subscr_data_ack(struct lu_operation *luop, |
| 304 | const struct osmo_gsup_message *gsup) |
| 305 | { |
| 306 | OSMO_ASSERT(luop->state == LU_S_ISD_SENT); |
| 307 | /* FIXME: Check for spoofing */ |
| 308 | |
| 309 | osmo_timer_del(&luop->timer); |
| 310 | |
| 311 | /* Subscriber_Present_HLR */ |
| 312 | /* CS only: Check_SS_required? -> MAP-FW-CHECK_SS_IND.req */ |
| 313 | |
| 314 | /* Send final ACK towards inquiring VLR/SGSN */ |
| 315 | lu_op_tx_ack(luop); |
| 316 | } |
| 317 | |
| 318 | /*! Receive GSUP message for given \ref lu_operation */ |
| 319 | void lu_op_rx_gsup(struct lu_operation *luop, |
| 320 | const struct osmo_gsup_message *gsup) |
| 321 | { |
| 322 | switch (gsup->message_type) { |
| 323 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
| 324 | /* FIXME */ |
| 325 | break; |
| 326 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
| 327 | lu_op_rx_insert_subscr_data_ack(luop, gsup); |
| 328 | break; |
| 329 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 330 | /* FIXME */ |
| 331 | break; |
| 332 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 333 | lu_op_rx_cancel_old_ack(luop, gsup); |
| 334 | break; |
| 335 | default: |
| 336 | LOGP(DMAIN, LOGL_ERROR, "Unhandled GSUP msg_type 0x%02x\n", |
| 337 | gsup->message_type); |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static struct lu_operation *lu_op_alloc(struct osmo_gsup_server *srv) |
| 343 | { |
| 344 | struct lu_operation *luop; |
| 345 | |
| 346 | luop = talloc_zero(srv, struct lu_operation); |
| 347 | OSMO_ASSERT(luop); |
| 348 | luop->gsup_server = srv; |
| 349 | luop->timer.cb = lu_op_timer_cb; |
| 350 | luop->timer.data = luop; |
| 351 | |
| 352 | return luop; |
| 353 | } |
| 354 | |
| 355 | /*! Receive Update Location Request, creates new \ref lu_operation */ |
| 356 | static int rx_upd_loc_req(struct osmo_gsup_conn *conn, |
| 357 | const struct osmo_gsup_message *gsup) |
| 358 | { |
| 359 | int rc; |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 360 | struct lu_operation *luop; |
| 361 | struct hlr_subscriber *subscr; |
| 362 | uint8_t *peer_addr; |
| 363 | |
| 364 | rc = osmo_gsup_conn_ccm_get(conn, &peer_addr, IPAC_IDTAG_SERNR); |
| 365 | if (rc < 0) { |
| 366 | LOGP(DMAIN, LOGL_ERROR, "LU REQ from conn without addr?\n"); |
| 367 | return rc; |
| 368 | } |
| 369 | |
| 370 | luop = lu_op_alloc(conn->server); |
| 371 | luop->peer = talloc_memdup(luop, peer_addr, rc); |
| 372 | lu_op_statechg(luop, LU_S_LU_RECEIVED); |
| 373 | subscr = &luop->subscr; |
| 374 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) |
| 375 | luop->is_ps = true; |
| 376 | llist_add(&luop->list, &g_lu_ops); |
| 377 | |
| 378 | /* Roughly follwing "Process Update_Location_HLR" of TS 09.02 */ |
| 379 | |
| 380 | /* check if subscriber is known at all */ |
| 381 | rc = db_subscr_get(g_dbc, gsup->imsi, subscr); |
| 382 | if (rc < 0) { |
| 383 | /* Send Error back: Subscriber Unknown in HLR */ |
| 384 | strcpy(luop->subscr.imsi, gsup->imsi); |
| 385 | lu_op_tx_error(luop, GMM_CAUSE_IMSI_UNKNOWN); |
| 386 | return 0; |
| 387 | } |
| 388 | |
Harald Welte | 9990927 | 2016-05-05 18:24:15 +0200 | [diff] [blame] | 389 | /* Check if subscriber is generally permitted on CS or PS |
| 390 | * service (as requested) */ |
Harald Welte | 53b8678 | 2016-05-05 21:04:11 +0200 | [diff] [blame] | 391 | if (!luop->is_ps && !subscr->nam_cs) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 392 | lu_op_tx_error(luop, GMM_CAUSE_PLMN_NOTALLOWED); |
| 393 | return 0; |
Harald Welte | 53b8678 | 2016-05-05 21:04:11 +0200 | [diff] [blame] | 394 | } else if (luop->is_ps && !subscr->nam_ps) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 395 | lu_op_tx_error(luop, GMM_CAUSE_GPRS_NOTALLOWED); |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | /* TODO: Set subscriber tracing = deactive in VLR/SGSN */ |
| 400 | |
| 401 | #if 0 |
| 402 | /* Cancel in old VLR/SGSN, if new VLR/SGSN differs from old */ |
| 403 | if (luop->is_ps == false && |
| 404 | strcmp(subscr->vlr_number, vlr_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 405 | lu_op_tx_cancel_old(luop); |
| 406 | } else if (luop->is_ps == true && |
| 407 | strcmp(subscr->sgsn_number, sgsn_number)) { |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 408 | lu_op_tx_cancel_old(luop); |
| 409 | } else |
| 410 | #endif |
| 411 | { |
| 412 | /* TODO: Subscriber allowed to roam in PLMN? */ |
| 413 | /* TODO: Update RoutingInfo */ |
| 414 | /* TODO: Reset Flag MS Purged (cs/ps) */ |
| 415 | /* TODO: Control_Tracing_HLR / Control_Tracing_HLR_with_SGSN */ |
| 416 | lu_op_tx_insert_subscr_data(luop); |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 421 | static int rx_purge_ms_req(struct osmo_gsup_conn *conn, |
| 422 | const struct osmo_gsup_message *gsup) |
| 423 | { |
| 424 | struct osmo_gsup_message gsup_reply = {0}; |
| 425 | struct msgb *msg_out; |
| 426 | bool is_ps = false; |
| 427 | int rc; |
| 428 | |
| 429 | LOGP(DAUC, LOGL_INFO, "%s: Purge MS (%s)\n", gsup->imsi, |
| 430 | is_ps ? "PS" : "CS"); |
| 431 | |
| 432 | memcpy(gsup_reply.imsi, gsup->imsi, sizeof(gsup_reply.imsi)); |
| 433 | |
| 434 | if (gsup->cn_domain == OSMO_GSUP_CN_DOMAIN_PS) |
| 435 | is_ps = true; |
| 436 | |
| 437 | /* FIXME: check if the VLR that sends the purge is the same that |
| 438 | * we have on record. Only update if yes */ |
| 439 | |
| 440 | /* Perform the actual update of the DB */ |
| 441 | rc = db_subscr_purge(g_dbc, gsup->imsi, is_ps); |
| 442 | |
| 443 | if (rc == 1) |
| 444 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_RESULT; |
| 445 | else if (rc == 0) { |
| 446 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 447 | gsup_reply.cause = GMM_CAUSE_IMSI_UNKNOWN; |
| 448 | } else { |
| 449 | gsup_reply.message_type = OSMO_GSUP_MSGT_PURGE_MS_ERROR; |
| 450 | gsup_reply.cause = GMM_CAUSE_NET_FAIL; |
| 451 | } |
| 452 | |
| 453 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP AUC response"); |
| 454 | osmo_gsup_encode(msg_out, &gsup_reply); |
| 455 | return osmo_gsup_conn_send(conn, msg_out); |
| 456 | } |
| 457 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 458 | static int read_cb(struct osmo_gsup_conn *conn, struct msgb *msg) |
| 459 | { |
| 460 | static struct osmo_gsup_message gsup; |
| 461 | int rc; |
| 462 | |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 463 | rc = osmo_gsup_decode(msgb_l2(msg), msgb_l2len(msg), &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 464 | if (rc < 0) { |
| 465 | LOGP(DMAIN, LOGL_ERROR, "error in GSUP decode: %d\n", rc); |
| 466 | return rc; |
| 467 | } |
| 468 | |
| 469 | switch (gsup.message_type) { |
| 470 | /* requests sent to us */ |
| 471 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST: |
| 472 | rx_send_auth_info(conn, &gsup); |
| 473 | break; |
| 474 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 475 | rx_upd_loc_req(conn, &gsup); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 476 | break; |
Harald Welte | b18f0e0 | 2016-05-05 21:03:03 +0200 | [diff] [blame] | 477 | case OSMO_GSUP_MSGT_PURGE_MS_REQUEST: |
| 478 | rx_purge_ms_req(conn, &gsup); |
| 479 | break; |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 480 | /* responses to requests sent by us */ |
| 481 | case OSMO_GSUP_MSGT_INSERT_DATA_ERROR: |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 482 | case OSMO_GSUP_MSGT_INSERT_DATA_RESULT: |
Harald Welte | e687be5 | 2016-05-03 18:49:27 +0200 | [diff] [blame] | 483 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR: |
| 484 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT: |
| 485 | { |
| 486 | struct lu_operation *luop = lu_op_by_imsi(gsup.imsi); |
| 487 | if (!luop) { |
| 488 | LOGP(DMAIN, LOGL_ERROR, "GSUP message %u for " |
| 489 | "unknown IMSI %s\n", gsup.message_type, |
| 490 | gsup.imsi); |
| 491 | break; |
| 492 | } |
| 493 | lu_op_rx_gsup(luop, &gsup); |
| 494 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 495 | break; |
| 496 | default: |
| 497 | LOGP(DMAIN, LOGL_DEBUG, "Unhandled GSUP message type %u\n", |
| 498 | gsup.message_type); |
| 499 | break; |
| 500 | } |
Harald Welte | 5341b5d | 2016-04-28 12:48:39 +0200 | [diff] [blame] | 501 | msgb_free(msg); |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 505 | static struct osmo_gsup_server *gs; |
| 506 | |
| 507 | static void signal_hdlr(int signal) |
| 508 | { |
| 509 | switch (signal) { |
| 510 | case SIGINT: |
| 511 | LOGP(DMAIN, LOGL_NOTICE, "Terminating due to SIGINT\n"); |
| 512 | osmo_gsup_server_destroy(gs); |
| 513 | db_close(g_dbc); |
| 514 | log_fini(); |
| 515 | exit(0); |
| 516 | break; |
| 517 | case SIGUSR1: |
| 518 | LOGP(DMAIN, LOGL_DEBUG, "Talloc Report due to SIGUSR1\n"); |
| 519 | talloc_report_full(NULL, stderr); |
| 520 | break; |
| 521 | } |
| 522 | } |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 523 | |
| 524 | int main(int argc, char **argv) |
| 525 | { |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 526 | int rc; |
| 527 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 528 | talloc_enable_leak_report_full(); |
| 529 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 530 | rc = osmo_init_logging(&hlr_log_info); |
| 531 | if (rc < 0) { |
| 532 | fprintf(stderr, "Error initializing logging\n"); |
| 533 | exit(1); |
| 534 | } |
| 535 | LOGP(DMAIN, LOGL_NOTICE, "hlr starting\n"); |
| 536 | |
| 537 | rc = rand_init(); |
| 538 | if (rc < 0) { |
| 539 | LOGP(DMAIN, LOGL_FATAL, "Error initializing random source\n"); |
| 540 | exit(1); |
| 541 | } |
| 542 | |
| 543 | g_dbc = db_open(NULL, "hlr.db"); |
| 544 | if (!g_dbc) { |
| 545 | LOGP(DMAIN, LOGL_FATAL, "Error opening database\n"); |
| 546 | exit(1); |
| 547 | } |
| 548 | |
| 549 | gs = osmo_gsup_server_create(NULL, NULL, 2222, read_cb); |
| 550 | if (!gs) { |
| 551 | LOGP(DMAIN, LOGL_FATAL, "Error starting GSUP server\n"); |
| 552 | exit(1); |
| 553 | } |
| 554 | |
Harald Welte | aabae9e | 2016-04-28 12:48:14 +0200 | [diff] [blame] | 555 | osmo_init_ignore_signals(); |
| 556 | signal(SIGINT, &signal_hdlr); |
| 557 | signal(SIGUSR1, &signal_hdlr); |
| 558 | |
| 559 | //osmo_daemonize(); |
| 560 | |
Harald Welte | e72cf55 | 2016-04-28 07:18:49 +0200 | [diff] [blame] | 561 | while (1) { |
| 562 | osmo_select_main(0); |
| 563 | } |
| 564 | |
| 565 | db_close(g_dbc); |
| 566 | |
| 567 | log_fini(); |
| 568 | |
| 569 | exit(0); |
| 570 | } |