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> |
| 28 | #include <osmocom/gsm/gsm48_ie.h> |
| 29 | #include <osmocom/gsm/gsup.h> |
| 30 | #include <osmocom/gsm/apn.h> |
| 31 | |
| 32 | #include "gsup_server.h" |
| 33 | #include "gsup_router.h" |
| 34 | #include "logging.h" |
| 35 | #include "luop.h" |
| 36 | |
| 37 | const struct value_string lu_state_names[] = { |
| 38 | { LU_S_NULL, "NULL" }, |
| 39 | { LU_S_LU_RECEIVED, "LU RECEIVED" }, |
| 40 | { LU_S_CANCEL_SENT, "CANCEL SENT" }, |
| 41 | { LU_S_CANCEL_ACK_RECEIVED, "CANCEL-ACK RECEIVED" }, |
| 42 | { LU_S_ISD_SENT, "ISD SENT" }, |
| 43 | { LU_S_ISD_ACK_RECEIVED, "ISD-ACK RECEIVED" }, |
| 44 | { LU_S_COMPLETE, "COMPLETE" }, |
| 45 | { 0, NULL } |
| 46 | }; |
| 47 | |
| 48 | /* Transmit a given GSUP message for the given LU operation */ |
| 49 | static void _luop_tx_gsup(struct lu_operation *luop, |
| 50 | const struct osmo_gsup_message *gsup) |
| 51 | { |
| 52 | struct msgb *msg_out; |
| 53 | |
| 54 | msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP LUOP"); |
| 55 | osmo_gsup_encode(msg_out, gsup); |
| 56 | |
| 57 | osmo_gsup_addr_send(luop->gsup_server, luop->peer, |
| 58 | talloc_total_size(luop->peer), |
| 59 | msg_out); |
| 60 | } |
| 61 | |
| 62 | static inline void fill_gsup_msg(struct osmo_gsup_message *out, |
| 63 | const struct lu_operation *lu, |
| 64 | enum osmo_gsup_message_type mt) |
| 65 | { |
| 66 | memset(out, 0, sizeof(struct osmo_gsup_message)); |
| 67 | if (lu) |
| 68 | osmo_strlcpy(out->imsi, lu->subscr.imsi, |
| 69 | GSM23003_IMSI_MAX_DIGITS + 1); |
| 70 | out->message_type = mt; |
| 71 | } |
| 72 | |
| 73 | /* timer call-back in case LU operation doesn't receive an response */ |
| 74 | static void lu_op_timer_cb(void *data) |
| 75 | { |
| 76 | struct lu_operation *luop = data; |
| 77 | |
| 78 | DEBUGP(DMAIN, "LU OP timer expired in state %s\n", |
| 79 | get_value_string(lu_state_names, luop->state)); |
| 80 | |
| 81 | switch (luop->state) { |
| 82 | case LU_S_CANCEL_SENT: |
| 83 | break; |
| 84 | case LU_S_ISD_SENT: |
| 85 | break; |
| 86 | default: |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | lu_op_tx_error(luop, GMM_CAUSE_NET_FAIL); |
| 91 | } |
| 92 | |
| 93 | bool lu_op_fill_subscr(struct lu_operation *luop, struct db_context *dbc, |
| 94 | const char *imsi) |
| 95 | { |
| 96 | struct hlr_subscriber *subscr = &luop->subscr; |
| 97 | |
| 98 | if (db_subscr_get(dbc, imsi, subscr) < 0) |
| 99 | return false; |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | struct lu_operation *lu_op_alloc(struct osmo_gsup_server *srv) |
| 105 | { |
| 106 | struct lu_operation *luop; |
| 107 | |
| 108 | luop = talloc_zero(srv, struct lu_operation); |
| 109 | OSMO_ASSERT(luop); |
| 110 | luop->gsup_server = srv; |
| 111 | luop->timer.cb = lu_op_timer_cb; |
| 112 | luop->timer.data = luop; |
| 113 | |
| 114 | return luop; |
| 115 | } |
| 116 | |
| 117 | struct lu_operation *lu_op_alloc_conn(struct osmo_gsup_conn *conn) |
| 118 | { |
| 119 | uint8_t *peer_addr; |
| 120 | struct lu_operation *luop = lu_op_alloc(conn->server); |
| 121 | int rc = osmo_gsup_conn_ccm_get(conn, &peer_addr, IPAC_IDTAG_SERNR); |
| 122 | if (rc < 0) |
| 123 | return NULL; |
| 124 | |
| 125 | luop->peer = talloc_memdup(luop, peer_addr, rc); |
| 126 | |
| 127 | return luop; |
| 128 | } |
| 129 | |
| 130 | /* FIXME: this doesn't seem to work at all */ |
| 131 | struct lu_operation *lu_op_by_imsi(const char *imsi, |
| 132 | const struct llist_head *lst) |
| 133 | { |
| 134 | struct lu_operation *luop; |
| 135 | |
| 136 | llist_for_each_entry(luop, lst, list) { |
| 137 | if (!strcmp(imsi, luop->subscr.imsi)) |
| 138 | return luop; |
| 139 | } |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | void lu_op_statechg(struct lu_operation *luop, enum lu_state new_state) |
| 144 | { |
| 145 | enum lu_state old_state = luop->state; |
| 146 | |
| 147 | DEBUGP(DMAIN, "LU OP state change: %s -> ", |
| 148 | get_value_string(lu_state_names, old_state)); |
| 149 | DEBUGPC(DMAIN, "%s\n", |
| 150 | get_value_string(lu_state_names, new_state)); |
| 151 | |
| 152 | luop->state = new_state; |
| 153 | } |
| 154 | |
| 155 | /* Send a msgb to a given address using routing */ |
| 156 | int osmo_gsup_addr_send(struct osmo_gsup_server *gs, |
| 157 | const uint8_t *addr, size_t addrlen, |
| 158 | struct msgb *msg) |
| 159 | { |
| 160 | struct osmo_gsup_conn *conn; |
| 161 | |
| 162 | conn = gsup_route_find(gs, addr, addrlen); |
| 163 | if (!conn) { |
| 164 | DEBUGP(DMAIN, "Cannot find route for addr %s\n", addr); |
| 165 | msgb_free(msg); |
| 166 | return -ENODEV; |
| 167 | } |
| 168 | |
| 169 | return osmo_gsup_conn_send(conn, msg); |
| 170 | } |
| 171 | |
| 172 | /*! Transmit UPD_LOC_ERROR and destroy lu_operation */ |
| 173 | void lu_op_tx_error(struct lu_operation *luop, enum gsm48_gmm_cause cause) |
| 174 | { |
| 175 | struct osmo_gsup_message gsup; |
| 176 | |
| 177 | DEBUGP(DMAIN, "%s: LU OP Tx Error (cause %s)\n", |
| 178 | luop->subscr.imsi, get_value_string(gsm48_gmm_cause_names, |
| 179 | cause)); |
| 180 | |
| 181 | fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR); |
| 182 | gsup.cause = cause; |
| 183 | |
| 184 | _luop_tx_gsup(luop, &gsup); |
| 185 | |
| 186 | llist_del(&luop->list); |
| 187 | talloc_free(luop); |
| 188 | } |
| 189 | |
| 190 | /*! Transmit UPD_LOC_RESULT and destroy lu_operation */ |
| 191 | void lu_op_tx_ack(struct lu_operation *luop) |
| 192 | { |
| 193 | struct osmo_gsup_message gsup; |
| 194 | |
| 195 | fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT); |
| 196 | //FIXME gsup.hlr_enc; |
| 197 | |
| 198 | _luop_tx_gsup(luop, &gsup); |
| 199 | |
| 200 | llist_del(&luop->list); |
| 201 | talloc_free(luop); |
| 202 | } |
| 203 | |
| 204 | /*! Send Cancel Location to old VLR/SGSN */ |
| 205 | void lu_op_tx_cancel_old(struct lu_operation *luop) |
| 206 | { |
| 207 | struct osmo_gsup_message gsup; |
| 208 | |
| 209 | OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED); |
| 210 | |
| 211 | fill_gsup_msg(&gsup, NULL, OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST); |
| 212 | //gsup.cause = FIXME; |
| 213 | //gsup.cancel_type = FIXME; |
| 214 | |
| 215 | _luop_tx_gsup(luop, &gsup); |
| 216 | |
| 217 | lu_op_statechg(luop, LU_S_CANCEL_SENT); |
| 218 | osmo_timer_schedule(&luop->timer, CANCEL_TIMEOUT_SECS, 0); |
| 219 | } |
| 220 | |
| 221 | /*! Transmit Insert Subscriber Data to new VLR/SGSN */ |
| 222 | void lu_op_tx_insert_subscr_data(struct lu_operation *luop) |
| 223 | { |
| 224 | struct osmo_gsup_message gsup; |
| 225 | uint8_t apn[APN_MAXLEN]; |
| 226 | uint8_t msisdn_enc[43]; /* TODO use constant; TS 24.008 10.5.4.7 */ |
| 227 | int l; |
| 228 | |
| 229 | OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED || |
| 230 | luop->state == LU_S_CANCEL_ACK_RECEIVED); |
| 231 | |
| 232 | fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_INSERT_DATA_REQUEST); |
| 233 | |
| 234 | l = gsm48_encode_bcd_number(msisdn_enc, sizeof(msisdn_enc), 0, |
| 235 | luop->subscr.msisdn); |
| 236 | if (l < 1) { |
| 237 | LOGP(DMAIN, LOGL_ERROR, |
| 238 | "%s: Error: cannot encode MSISDN '%s'\n", |
| 239 | luop->subscr.imsi, luop->subscr.msisdn); |
| 240 | lu_op_tx_error(luop, GMM_CAUSE_PROTO_ERR_UNSPEC); |
| 241 | return; |
| 242 | } |
| 243 | gsup.msisdn_enc = msisdn_enc; |
| 244 | gsup.msisdn_enc_len = l; |
| 245 | |
| 246 | /* FIXME: deal with encoding the following data */ |
| 247 | gsup.hlr_enc; |
| 248 | |
| 249 | if (luop->is_ps) { |
| 250 | /* FIXME: PDP infos - use more fine-grained access control |
| 251 | instead of wildcard APN */ |
| 252 | l = osmo_apn_from_str(apn, sizeof(apn), "*"); |
| 253 | if (l > 0) { |
| 254 | gsup.pdp_infos[0].apn_enc = apn; |
| 255 | gsup.pdp_infos[0].apn_enc_len = l; |
| 256 | gsup.pdp_infos[0].have_info = 1; |
| 257 | gsup.num_pdp_infos = 1; |
| 258 | /* FIXME: use real value: */ |
| 259 | gsup.pdp_infos[0].context_id = 1; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /* Send ISD to new VLR/SGSN */ |
| 264 | _luop_tx_gsup(luop, &gsup); |
| 265 | |
| 266 | lu_op_statechg(luop, LU_S_ISD_SENT); |
| 267 | osmo_timer_schedule(&luop->timer, ISD_TIMEOUT_SECS, 0); |
| 268 | } |
Max | 9cacb6f | 2017-02-20 17:22:56 +0100 | [diff] [blame] | 269 | |
| 270 | /*! Transmit Delete Subscriber Data to new VLR/SGSN */ |
| 271 | void lu_op_tx_del_subscr_data(struct lu_operation *luop) |
| 272 | { |
| 273 | struct osmo_gsup_message gsup; |
| 274 | |
| 275 | fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_DELETE_DATA_REQUEST); |
| 276 | |
| 277 | gsup.cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
| 278 | |
| 279 | /* Send ISD to new VLR/SGSN */ |
| 280 | _luop_tx_gsup(luop, &gsup); |
| 281 | } |