blob: e63ba91ce9efce9397656faae47edd1bafb2cec0 [file] [log] [blame]
Maxea8b0d42017-02-14 16:53:04 +01001/* 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>
Maxea8b0d42017-02-14 16:53:04 +010028#include <osmocom/gsm/gsup.h>
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +020029#include <osmocom/gsm/apn.h>
Maxea8b0d42017-02-14 16:53:04 +010030
Neels Hofmeyr2f758032019-11-20 00:37:07 +010031#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>
Maxea8b0d42017-02-14 16:53:04 +010035
36const 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 */
48static void _luop_tx_gsup(struct lu_operation *luop,
49 const struct osmo_gsup_message *gsup)
50{
51 struct msgb *msg_out;
52
Neels Hofmeyra7d0f872019-10-30 02:08:28 +010053 msg_out = osmo_gsup_msgb_alloc("GSUP LUOP");
Maxea8b0d42017-02-14 16:53:04 +010054 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
61static 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 */
73static 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
92bool 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 Hofmeyr518335e2017-10-06 03:20:14 +020097 if (db_subscr_get_by_imsi(dbc, imsi, subscr) < 0)
Maxea8b0d42017-02-14 16:53:04 +010098 return false;
99
100 return true;
101}
102
103struct 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 Sperling8f840142018-03-29 18:10:56 +0200110 osmo_timer_setup(&luop->timer, lu_op_timer_cb, luop);
Maxea8b0d42017-02-14 16:53:04 +0100111
112 return luop;
113}
114
Neels Hofmeyr200f56e2017-10-17 01:46:04 +0200115void 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 Sperling8f840142018-03-29 18:10:56 +0200120
121 /* Delete timer just in case it is still pending. */
122 osmo_timer_del(&luop->timer);
123
Neels Hofmeyr200f56e2017-10-17 01:46:04 +0200124 talloc_free(luop);
125}
126
Maxea8b0d42017-02-14 16:53:04 +0100127struct 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 Hofmeyre86437c2017-10-17 01:46:45 +0200132 if (rc < 0) {
133 lu_op_free(luop);
Maxea8b0d42017-02-14 16:53:04 +0100134 return NULL;
Neels Hofmeyre86437c2017-10-17 01:46:45 +0200135 }
Maxea8b0d42017-02-14 16:53:04 +0100136
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 */
143struct 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
155void 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
Maxea8b0d42017-02-14 16:53:04 +0100167/*! Transmit UPD_LOC_ERROR and destroy lu_operation */
168void 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 Hofmeyr200f56e2017-10-17 01:46:04 +0200181 lu_op_free(luop);
Maxea8b0d42017-02-14 16:53:04 +0100182}
183
184/*! Transmit UPD_LOC_RESULT and destroy lu_operation */
185void 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 Hofmeyr200f56e2017-10-17 01:46:04 +0200194 lu_op_free(luop);
Maxea8b0d42017-02-14 16:53:04 +0100195}
196
Oliver Smith0c27a4c2020-04-15 14:37:41 +0200197/*! Send Cancel Location to old VLR/SGSN (FIXME: OS#4491) */
Maxea8b0d42017-02-14 16:53:04 +0100198void 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 */
215void lu_op_tx_insert_subscr_data(struct lu_operation *luop)
216{
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200217 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 Hofmeyr5aeb4382018-05-04 16:02:44 +0200220 uint8_t apn[APN_MAXLEN];
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200221 enum osmo_gsup_cn_domain cn_domain;
Maxea8b0d42017-02-14 16:53:04 +0100222
223 OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED ||
224 luop->state == LU_S_CANCEL_ACK_RECEIVED);
225
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200226 if (luop->is_ps)
227 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
228 else
229 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Maxea8b0d42017-02-14 16:53:04 +0100230
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200231 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) {
Maxea8b0d42017-02-14 16:53:04 +0100233 LOGP(DMAIN, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200234 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
235 "for %s\n", subscr->imsi, luop->peer);
Maxea8b0d42017-02-14 16:53:04 +0100236 return;
237 }
Maxea8b0d42017-02-14 16:53:04 +0100238
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}
Max9cacb6f2017-02-20 17:22:56 +0100245
Neels Hofmeyr7ae8d872017-10-17 01:46:50 +0200246/*! Transmit Delete Subscriber Data to new VLR/SGSN.
247 * The luop is not freed. */
Max9cacb6f2017-02-20 17:22:56 +0100248void 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}