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