blob: aff4d8130c3a3e3f60358a1de49194911a64aa41 [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
31#include "gsup_server.h"
32#include "gsup_router.h"
33#include "logging.h"
34#include "luop.h"
35
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
53 msg_out = msgb_alloc_headroom(1024+16, 16, "GSUP LUOP");
Stefan Sperlingf83432c2018-05-03 14:26:59 +020054 OSMO_ASSERT(msg_out);
Maxea8b0d42017-02-14 16:53:04 +010055 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
62static 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 */
74static 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
93bool 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
Neels Hofmeyr518335e2017-10-06 03:20:14 +020098 if (db_subscr_get_by_imsi(dbc, imsi, subscr) < 0)
Maxea8b0d42017-02-14 16:53:04 +010099 return false;
100
101 return true;
102}
103
104struct 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;
Stefan Sperling8f840142018-03-29 18:10:56 +0200111 osmo_timer_setup(&luop->timer, lu_op_timer_cb, luop);
Maxea8b0d42017-02-14 16:53:04 +0100112
113 return luop;
114}
115
Neels Hofmeyr200f56e2017-10-17 01:46:04 +0200116void lu_op_free(struct lu_operation *luop)
117{
118 /* Only attempt to remove when it was ever added to a list. */
119 if (luop->list.next)
120 llist_del(&luop->list);
Stefan Sperling8f840142018-03-29 18:10:56 +0200121
122 /* Delete timer just in case it is still pending. */
123 osmo_timer_del(&luop->timer);
124
Neels Hofmeyr200f56e2017-10-17 01:46:04 +0200125 talloc_free(luop);
126}
127
Maxea8b0d42017-02-14 16:53:04 +0100128struct lu_operation *lu_op_alloc_conn(struct osmo_gsup_conn *conn)
129{
130 uint8_t *peer_addr;
131 struct lu_operation *luop = lu_op_alloc(conn->server);
132 int rc = osmo_gsup_conn_ccm_get(conn, &peer_addr, IPAC_IDTAG_SERNR);
Neels Hofmeyre86437c2017-10-17 01:46:45 +0200133 if (rc < 0) {
134 lu_op_free(luop);
Maxea8b0d42017-02-14 16:53:04 +0100135 return NULL;
Neels Hofmeyre86437c2017-10-17 01:46:45 +0200136 }
Maxea8b0d42017-02-14 16:53:04 +0100137
138 luop->peer = talloc_memdup(luop, peer_addr, rc);
139
140 return luop;
141}
142
143/* FIXME: this doesn't seem to work at all */
144struct lu_operation *lu_op_by_imsi(const char *imsi,
145 const struct llist_head *lst)
146{
147 struct lu_operation *luop;
148
149 llist_for_each_entry(luop, lst, list) {
150 if (!strcmp(imsi, luop->subscr.imsi))
151 return luop;
152 }
153 return NULL;
154}
155
156void lu_op_statechg(struct lu_operation *luop, enum lu_state new_state)
157{
158 enum lu_state old_state = luop->state;
159
160 DEBUGP(DMAIN, "LU OP state change: %s -> ",
161 get_value_string(lu_state_names, old_state));
162 DEBUGPC(DMAIN, "%s\n",
163 get_value_string(lu_state_names, new_state));
164
165 luop->state = new_state;
166}
167
Maxea8b0d42017-02-14 16:53:04 +0100168/*! Transmit UPD_LOC_ERROR and destroy lu_operation */
169void lu_op_tx_error(struct lu_operation *luop, enum gsm48_gmm_cause cause)
170{
171 struct osmo_gsup_message gsup;
172
173 DEBUGP(DMAIN, "%s: LU OP Tx Error (cause %s)\n",
174 luop->subscr.imsi, get_value_string(gsm48_gmm_cause_names,
175 cause));
176
177 fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR);
178 gsup.cause = cause;
179
180 _luop_tx_gsup(luop, &gsup);
181
Neels Hofmeyr200f56e2017-10-17 01:46:04 +0200182 lu_op_free(luop);
Maxea8b0d42017-02-14 16:53:04 +0100183}
184
185/*! Transmit UPD_LOC_RESULT and destroy lu_operation */
186void lu_op_tx_ack(struct lu_operation *luop)
187{
188 struct osmo_gsup_message gsup;
189
190 fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT);
191 //FIXME gsup.hlr_enc;
192
193 _luop_tx_gsup(luop, &gsup);
194
Neels Hofmeyr200f56e2017-10-17 01:46:04 +0200195 lu_op_free(luop);
Maxea8b0d42017-02-14 16:53:04 +0100196}
197
198/*! Send Cancel Location to old VLR/SGSN */
199void lu_op_tx_cancel_old(struct lu_operation *luop)
200{
201 struct osmo_gsup_message gsup;
202
203 OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED);
204
205 fill_gsup_msg(&gsup, NULL, OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST);
206 //gsup.cause = FIXME;
207 //gsup.cancel_type = FIXME;
208
209 _luop_tx_gsup(luop, &gsup);
210
211 lu_op_statechg(luop, LU_S_CANCEL_SENT);
212 osmo_timer_schedule(&luop->timer, CANCEL_TIMEOUT_SECS, 0);
213}
214
215/*! Transmit Insert Subscriber Data to new VLR/SGSN */
216void lu_op_tx_insert_subscr_data(struct lu_operation *luop)
217{
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200218 struct hlr_subscriber *subscr = &luop->subscr;
219 struct osmo_gsup_message gsup = { };
220 uint8_t msisdn_enc[OSMO_GSUP_MAX_CALLED_PARTY_BCD_LEN];
Neels Hofmeyr5aeb4382018-05-04 16:02:44 +0200221 uint8_t apn[APN_MAXLEN];
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200222 enum osmo_gsup_cn_domain cn_domain;
Maxea8b0d42017-02-14 16:53:04 +0100223
224 OSMO_ASSERT(luop->state == LU_S_LU_RECEIVED ||
225 luop->state == LU_S_CANCEL_ACK_RECEIVED);
226
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200227 if (luop->is_ps)
228 cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
229 else
230 cn_domain = OSMO_GSUP_CN_DOMAIN_CS;
Maxea8b0d42017-02-14 16:53:04 +0100231
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200232 if (osmo_gsup_create_insert_subscriber_data_msg(&gsup, subscr->imsi, subscr->msisdn, msisdn_enc,
233 sizeof(msisdn_enc), apn, sizeof(apn), cn_domain) != 0) {
Maxea8b0d42017-02-14 16:53:04 +0100234 LOGP(DMAIN, LOGL_ERROR,
Stefan Sperlingf83432c2018-05-03 14:26:59 +0200235 "IMSI='%s': Cannot notify GSUP client; could not create gsup message "
236 "for %s\n", subscr->imsi, luop->peer);
Maxea8b0d42017-02-14 16:53:04 +0100237 return;
238 }
Maxea8b0d42017-02-14 16:53:04 +0100239
240 /* Send ISD to new VLR/SGSN */
241 _luop_tx_gsup(luop, &gsup);
242
243 lu_op_statechg(luop, LU_S_ISD_SENT);
244 osmo_timer_schedule(&luop->timer, ISD_TIMEOUT_SECS, 0);
245}
Max9cacb6f2017-02-20 17:22:56 +0100246
Neels Hofmeyr7ae8d872017-10-17 01:46:50 +0200247/*! Transmit Delete Subscriber Data to new VLR/SGSN.
248 * The luop is not freed. */
Max9cacb6f2017-02-20 17:22:56 +0100249void lu_op_tx_del_subscr_data(struct lu_operation *luop)
250{
251 struct osmo_gsup_message gsup;
252
253 fill_gsup_msg(&gsup, luop, OSMO_GSUP_MSGT_DELETE_DATA_REQUEST);
254
255 gsup.cn_domain = OSMO_GSUP_CN_DOMAIN_PS;
256
257 /* Send ISD to new VLR/SGSN */
258 _luop_tx_gsup(luop, &gsup);
259}