Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 1 | /* OsmoHLR TX/RX lu operations */ |
| 2 | |
| 3 | /* (C) 2017 sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Harald Welte <laforge@gnumonks.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdbool.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include <osmocom/core/logging.h> |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 28 | #include <osmocom/gsm/gsup.h> |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/apn.h> |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 30 | |
Neels Hofmeyr | 2f75803 | 2019-11-20 00:37:07 +0100 | [diff] [blame] | 31 | #include <osmocom/hlr/gsup_server.h> |
| 32 | #include <osmocom/hlr/gsup_router.h> |
| 33 | #include <osmocom/hlr/logging.h> |
| 34 | #include <osmocom/hlr/luop.h> |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 35 | |
| 36 | const struct value_string lu_state_names[] = { |
| 37 | { LU_S_NULL, "NULL" }, |
| 38 | { LU_S_LU_RECEIVED, "LU RECEIVED" }, |
| 39 | { LU_S_CANCEL_SENT, "CANCEL SENT" }, |
| 40 | { LU_S_CANCEL_ACK_RECEIVED, "CANCEL-ACK RECEIVED" }, |
| 41 | { LU_S_ISD_SENT, "ISD SENT" }, |
| 42 | { LU_S_ISD_ACK_RECEIVED, "ISD-ACK RECEIVED" }, |
| 43 | { LU_S_COMPLETE, "COMPLETE" }, |
| 44 | { 0, NULL } |
| 45 | }; |
| 46 | |
| 47 | /* Transmit a given GSUP message for the given LU operation */ |
| 48 | static void _luop_tx_gsup(struct lu_operation *luop, |
| 49 | const struct osmo_gsup_message *gsup) |
| 50 | { |
| 51 | struct msgb *msg_out; |
| 52 | |
Neels Hofmeyr | a7d0f87 | 2019-10-30 02:08:28 +0100 | [diff] [blame^] | 53 | msg_out = osmo_gsup_msgb_alloc("GSUP LUOP"); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 54 | osmo_gsup_encode(msg_out, gsup); |
| 55 | |
| 56 | osmo_gsup_addr_send(luop->gsup_server, luop->peer, |
| 57 | talloc_total_size(luop->peer), |
| 58 | msg_out); |
| 59 | } |
| 60 | |
| 61 | static inline void fill_gsup_msg(struct osmo_gsup_message *out, |
| 62 | const struct lu_operation *lu, |
| 63 | enum osmo_gsup_message_type mt) |
| 64 | { |
| 65 | memset(out, 0, sizeof(struct osmo_gsup_message)); |
| 66 | if (lu) |
| 67 | osmo_strlcpy(out->imsi, lu->subscr.imsi, |
| 68 | GSM23003_IMSI_MAX_DIGITS + 1); |
| 69 | out->message_type = mt; |
| 70 | } |
| 71 | |
| 72 | /* timer call-back in case LU operation doesn't receive an response */ |
| 73 | static void lu_op_timer_cb(void *data) |
| 74 | { |
| 75 | struct lu_operation *luop = data; |
| 76 | |
| 77 | DEBUGP(DMAIN, "LU OP timer expired in state %s\n", |
| 78 | get_value_string(lu_state_names, luop->state)); |
| 79 | |
| 80 | switch (luop->state) { |
| 81 | case LU_S_CANCEL_SENT: |
| 82 | break; |
| 83 | case LU_S_ISD_SENT: |
| 84 | break; |
| 85 | default: |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | lu_op_tx_error(luop, GMM_CAUSE_NET_FAIL); |
| 90 | } |
| 91 | |
| 92 | bool lu_op_fill_subscr(struct lu_operation *luop, struct db_context *dbc, |
| 93 | const char *imsi) |
| 94 | { |
| 95 | struct hlr_subscriber *subscr = &luop->subscr; |
| 96 | |
Neels Hofmeyr | 518335e | 2017-10-06 03:20:14 +0200 | [diff] [blame] | 97 | if (db_subscr_get_by_imsi(dbc, imsi, subscr) < 0) |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 98 | return false; |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | struct lu_operation *lu_op_alloc(struct osmo_gsup_server *srv) |
| 104 | { |
| 105 | struct lu_operation *luop; |
| 106 | |
| 107 | luop = talloc_zero(srv, struct lu_operation); |
| 108 | OSMO_ASSERT(luop); |
| 109 | luop->gsup_server = srv; |
Stefan Sperling | 8f84014 | 2018-03-29 18:10:56 +0200 | [diff] [blame] | 110 | osmo_timer_setup(&luop->timer, lu_op_timer_cb, luop); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 111 | |
| 112 | return luop; |
| 113 | } |
| 114 | |
Neels Hofmeyr | 200f56e | 2017-10-17 01:46:04 +0200 | [diff] [blame] | 115 | void lu_op_free(struct lu_operation *luop) |
| 116 | { |
| 117 | /* Only attempt to remove when it was ever added to a list. */ |
| 118 | if (luop->list.next) |
| 119 | llist_del(&luop->list); |
Stefan Sperling | 8f84014 | 2018-03-29 18:10:56 +0200 | [diff] [blame] | 120 | |
| 121 | /* Delete timer just in case it is still pending. */ |
| 122 | osmo_timer_del(&luop->timer); |
| 123 | |
Neels Hofmeyr | 200f56e | 2017-10-17 01:46:04 +0200 | [diff] [blame] | 124 | talloc_free(luop); |
| 125 | } |
| 126 | |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 127 | struct lu_operation *lu_op_alloc_conn(struct osmo_gsup_conn *conn) |
| 128 | { |
| 129 | uint8_t *peer_addr; |
| 130 | struct lu_operation *luop = lu_op_alloc(conn->server); |
| 131 | int rc = osmo_gsup_conn_ccm_get(conn, &peer_addr, IPAC_IDTAG_SERNR); |
Neels Hofmeyr | e86437c | 2017-10-17 01:46:45 +0200 | [diff] [blame] | 132 | if (rc < 0) { |
| 133 | lu_op_free(luop); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 134 | return NULL; |
Neels Hofmeyr | e86437c | 2017-10-17 01:46:45 +0200 | [diff] [blame] | 135 | } |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 136 | |
| 137 | luop->peer = talloc_memdup(luop, peer_addr, rc); |
| 138 | |
| 139 | return luop; |
| 140 | } |
| 141 | |
| 142 | /* FIXME: this doesn't seem to work at all */ |
| 143 | struct lu_operation *lu_op_by_imsi(const char *imsi, |
| 144 | const struct llist_head *lst) |
| 145 | { |
| 146 | struct lu_operation *luop; |
| 147 | |
| 148 | llist_for_each_entry(luop, lst, list) { |
| 149 | if (!strcmp(imsi, luop->subscr.imsi)) |
| 150 | return luop; |
| 151 | } |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | void lu_op_statechg(struct lu_operation *luop, enum lu_state new_state) |
| 156 | { |
| 157 | enum lu_state old_state = luop->state; |
| 158 | |
| 159 | DEBUGP(DMAIN, "LU OP state change: %s -> ", |
| 160 | get_value_string(lu_state_names, old_state)); |
| 161 | DEBUGPC(DMAIN, "%s\n", |
| 162 | get_value_string(lu_state_names, new_state)); |
| 163 | |
| 164 | luop->state = new_state; |
| 165 | } |
| 166 | |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 167 | /*! Transmit UPD_LOC_ERROR and destroy lu_operation */ |
| 168 | void lu_op_tx_error(struct lu_operation *luop, enum gsm48_gmm_cause cause) |
| 169 | { |
| 170 | struct osmo_gsup_message gsup; |
| 171 | |
| 172 | DEBUGP(DMAIN, "%s: LU OP Tx Error (cause %s)\n", |
| 173 | luop->subscr.imsi, get_value_string(gsm48_gmm_cause_names, |
| 174 | cause)); |
| 175 | |
| 176 | fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR); |
| 177 | gsup.cause = cause; |
| 178 | |
| 179 | _luop_tx_gsup(luop, &gsup); |
| 180 | |
Neels Hofmeyr | 200f56e | 2017-10-17 01:46:04 +0200 | [diff] [blame] | 181 | lu_op_free(luop); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /*! Transmit UPD_LOC_RESULT and destroy lu_operation */ |
| 185 | void lu_op_tx_ack(struct lu_operation *luop) |
| 186 | { |
| 187 | struct osmo_gsup_message gsup; |
| 188 | |
| 189 | fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT); |
| 190 | //FIXME gsup.hlr_enc; |
| 191 | |
| 192 | _luop_tx_gsup(luop, &gsup); |
| 193 | |
Neels Hofmeyr | 200f56e | 2017-10-17 01:46:04 +0200 | [diff] [blame] | 194 | lu_op_free(luop); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /*! Send Cancel Location to old VLR/SGSN */ |
| 198 | void lu_op_tx_cancel_old(struct lu_operation *luop) |
| 199 | { |
| 200 | struct osmo_gsup_message gsup; |
| 201 | |
| 202 | OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED); |
| 203 | |
| 204 | fill_gsup_msg(&gsup, NULL, OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST); |
| 205 | //gsup.cause = FIXME; |
| 206 | //gsup.cancel_type = FIXME; |
| 207 | |
| 208 | _luop_tx_gsup(luop, &gsup); |
| 209 | |
| 210 | lu_op_statechg(luop, LU_S_CANCEL_SENT); |
| 211 | osmo_timer_schedule(&luop->timer, CANCEL_TIMEOUT_SECS, 0); |
| 212 | } |
| 213 | |
| 214 | /*! Transmit Insert Subscriber Data to new VLR/SGSN */ |
| 215 | void lu_op_tx_insert_subscr_data(struct lu_operation *luop) |
| 216 | { |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 217 | struct hlr_subscriber *subscr = &luop->subscr; |
| 218 | struct osmo_gsup_message gsup = { }; |
| 219 | uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN]; |
Neels Hofmeyr | 5aeb438 | 2018-05-04 16:02:44 +0200 | [diff] [blame] | 220 | uint8_t apn[APN_MAXLEN]; |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 221 | enum osmo_gsup_cn_domain cn_domain; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 222 | |
| 223 | OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED || |
| 224 | luop->state == LU_S_CANCEL_ACK_RECEIVED); |
| 225 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 226 | if (luop->is_ps) |
| 227 | cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
| 228 | else |
| 229 | cn_domain = OSMO_GSUP_CN_DOMAIN_CS; |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 230 | |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 231 | if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc, |
| 232 | sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) { |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 233 | LOGP(DMAIN, LOGL_ERROR, |
Stefan Sperling | f83432c | 2018-05-03 14:26:59 +0200 | [diff] [blame] | 234 | "IMSI='%s': Cannot notify GSUP client; could not create gsup message " |
| 235 | "for %s\n", subscr->imsi, luop->peer); |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 236 | return; |
| 237 | } |
Max | ea8b0d4 | 2017-02-14 16:53:04 +0100 | [diff] [blame] | 238 | |
| 239 | /* Send ISD to new VLR/SGSN */ |
| 240 | _luop_tx_gsup(luop, &gsup); |
| 241 | |
| 242 | lu_op_statechg(luop, LU_S_ISD_SENT); |
| 243 | osmo_timer_schedule(&luop->timer, ISD_TIMEOUT_SECS, 0); |
| 244 | } |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 245 | |
Neels Hofmeyr | 7ae8d87 | 2017-10-17 01:46:50 +0200 | [diff] [blame] | 246 | /*! Transmit Delete Subscriber Data to new VLR/SGSN. |
| 247 | * The luop is not freed. */ |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 248 | void lu_op_tx_del_subscr_data(struct lu_operation *luop) |
| 249 | { |
| 250 | struct osmo_gsup_message gsup; |
| 251 | |
| 252 | fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_DELETE_DATA_REQUEST); |
| 253 | |
| 254 | gsup.cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
| 255 | |
| 256 | /* Send ISD to new VLR/SGSN */ |
| 257 | _luop_tx_gsup(luop, &gsup); |
| 258 | } |