Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 1 | /* MS subscriber data handling */ |
| 2 | |
| 3 | /* (C) 2014 by sysmocom s.f.m.c. GmbH |
Holger Hans Peter Freyther | 9ba273d | 2015-04-23 09:53:53 -0400 | [diff] [blame] | 4 | * (C) 2015 by Holger Hans Peter Freyther |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 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 | |
Harald Welte | 53373bc | 2016-04-20 17:11:43 +0200 | [diff] [blame] | 23 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Harald Welte | 23d77d5 | 2016-04-25 19:07:34 +0200 | [diff] [blame] | 24 | #include <osmocom/gsm/gsup.h> |
Harald Welte | 586e681 | 2017-08-12 13:43:54 +0200 | [diff] [blame^] | 25 | #include <osmocom/gsm/apn.h> |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 26 | #include <osmocom/core/utils.h> |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 27 | #include <osmocom/core/logging.h> |
| 28 | #include <openbsc/gprs_subscriber.h> |
Neels Hofmeyr | eaaee92 | 2016-12-08 21:22:58 +0100 | [diff] [blame] | 29 | #include <openbsc/gsup_client.h> |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 30 | |
| 31 | #include <openbsc/sgsn.h> |
| 32 | #include <openbsc/gprs_sgsn.h> |
| 33 | #include <openbsc/gprs_gmm.h> |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 34 | #include <openbsc/gprs_utils.h> |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 35 | |
| 36 | #include <openbsc/debug.h> |
| 37 | |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 38 | #include <netinet/in.h> |
| 39 | #include <arpa/inet.h> |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 40 | #include <limits.h> |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 41 | |
Jacob Erlbeck | 743dec4 | 2015-01-08 15:18:39 +0100 | [diff] [blame] | 42 | #define SGSN_SUBSCR_MAX_RETRIES 3 |
| 43 | #define SGSN_SUBSCR_RETRY_INTERVAL 10 |
| 44 | |
Jacob Erlbeck | 929acdf | 2015-01-27 13:47:24 +0100 | [diff] [blame] | 45 | #define LOGGSUPP(level, gsup, fmt, args...) \ |
| 46 | LOGP(DGPRS, level, "GSUP(%s) " fmt, \ |
| 47 | (gsup)->imsi, \ |
| 48 | ## args) |
| 49 | |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 50 | extern void *tall_bsc_ctx; |
| 51 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 52 | LLIST_HEAD(_gprs_subscribers); |
| 53 | struct llist_head * const gprs_subscribers = &_gprs_subscribers; |
| 54 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 55 | static int gsup_read_cb(struct gsup_client *gsupc, struct msgb *msg); |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 56 | |
| 57 | /* TODO: Some functions are specific to the SGSN, but this file is more general |
| 58 | * (it has gprs_* name). Either move these functions elsewhere, split them and |
| 59 | * move a part, or replace the gprs_ prefix by sgsn_. The applies to |
| 60 | * gprs_subscr_init, gsup_read_cb, and gprs_subscr_tx_gsup_message. |
| 61 | */ |
| 62 | |
| 63 | int gprs_subscr_init(struct sgsn_instance *sgi) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 64 | { |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 65 | const char *addr_str; |
| 66 | |
| 67 | if (!sgi->cfg.gsup_server_addr.sin_addr.s_addr) |
| 68 | return 0; |
| 69 | |
| 70 | addr_str = inet_ntoa(sgi->cfg.gsup_server_addr.sin_addr); |
| 71 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 72 | sgi->gsup_client = gsup_client_create( |
Neels Hofmeyr | f2ba813 | 2017-03-04 03:15:53 +0100 | [diff] [blame] | 73 | "SGSN", |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 74 | addr_str, sgi->cfg.gsup_server_port, |
Neels Hofmeyr | 9c534fd | 2015-10-12 11:57:37 +0200 | [diff] [blame] | 75 | &gsup_read_cb, |
| 76 | &sgi->cfg.oap); |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 77 | |
| 78 | if (!sgi->gsup_client) |
| 79 | return -1; |
| 80 | |
| 81 | return 1; |
| 82 | } |
| 83 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 84 | static int gsup_read_cb(struct gsup_client *gsupc, struct msgb *msg) |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 85 | { |
| 86 | int rc; |
| 87 | |
| 88 | rc = gprs_subscr_rx_gsup_message(msg); |
Jacob Erlbeck | e154d8b | 2014-12-19 19:15:55 +0100 | [diff] [blame] | 89 | msgb_free(msg); |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 90 | if (rc < 0) |
| 91 | return -1; |
| 92 | |
| 93 | return rc; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 96 | int gprs_subscr_purge(struct gprs_subscr *subscr); |
Jacob Erlbeck | 0f47b8f | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 97 | |
Jacob Erlbeck | a1e0373 | 2014-12-02 11:28:38 +0100 | [diff] [blame] | 98 | static struct sgsn_subscriber_data *sgsn_subscriber_data_alloc(void *ctx) |
| 99 | { |
| 100 | struct sgsn_subscriber_data *sdata; |
Jacob Erlbeck | 7921ab1 | 2014-12-08 15:52:00 +0100 | [diff] [blame] | 101 | int idx; |
Jacob Erlbeck | a1e0373 | 2014-12-02 11:28:38 +0100 | [diff] [blame] | 102 | |
| 103 | sdata = talloc_zero(ctx, struct sgsn_subscriber_data); |
| 104 | |
Jacob Erlbeck | d6267d1 | 2015-01-19 11:10:04 +0100 | [diff] [blame] | 105 | sdata->error_cause = SGSN_ERROR_CAUSE_NONE; |
| 106 | |
Jacob Erlbeck | 7921ab1 | 2014-12-08 15:52:00 +0100 | [diff] [blame] | 107 | for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++) |
| 108 | sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL; |
| 109 | |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 110 | INIT_LLIST_HEAD(&sdata->pdp_list); |
| 111 | |
Jacob Erlbeck | a1e0373 | 2014-12-02 11:28:38 +0100 | [diff] [blame] | 112 | return sdata; |
| 113 | } |
| 114 | |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 115 | struct sgsn_subscriber_pdp_data* sgsn_subscriber_pdp_data_alloc( |
| 116 | struct sgsn_subscriber_data *sdata) |
| 117 | { |
| 118 | struct sgsn_subscriber_pdp_data* pdata; |
| 119 | |
| 120 | pdata = talloc_zero(sdata, struct sgsn_subscriber_pdp_data); |
| 121 | |
| 122 | llist_add_tail(&pdata->list, &sdata->pdp_list); |
| 123 | |
| 124 | return pdata; |
| 125 | } |
| 126 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 127 | struct gprs_subscr *gprs_subscr_get_by_imsi(const char *imsi) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 128 | { |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 129 | struct gprs_subscr *gsub; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 130 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 131 | if (!imsi || !*imsi) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 132 | return NULL; |
| 133 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 134 | llist_for_each_entry(gsub, gprs_subscribers, entry) { |
| 135 | if (!strcmp(gsub->imsi, imsi)) |
| 136 | return gprs_subscr_get(gsub); |
| 137 | } |
| 138 | return NULL; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 141 | static struct gprs_subscr *gprs_subscr_alloc(void) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 142 | { |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 143 | struct gprs_subscr *gsub; |
| 144 | gsub = talloc_zero(tall_bsc_ctx, struct gprs_subscr); |
| 145 | if (!gsub) |
| 146 | return NULL; |
| 147 | llist_add_tail(&gsub->entry, gprs_subscribers); |
| 148 | gsub->use_count = 1; |
| 149 | gsub->tmsi = GSM_RESERVED_TMSI; |
| 150 | return gsub; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 153 | struct gprs_subscr *gprs_subscr_get_or_create(const char *imsi) |
| 154 | { |
| 155 | struct gprs_subscr *gsub; |
| 156 | |
| 157 | gsub = gprs_subscr_get_by_imsi(imsi); |
| 158 | if (!gsub) { |
| 159 | gsub = gprs_subscr_alloc(); |
| 160 | if (!gsub) |
| 161 | return NULL; |
Neels Hofmeyr | ca06e04 | 2017-03-08 18:30:22 +0100 | [diff] [blame] | 162 | osmo_strlcpy(gsub->imsi, imsi, sizeof(gsub->imsi)); |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if (!gsub->sgsn_data) |
| 166 | gsub->sgsn_data = sgsn_subscriber_data_alloc(gsub); |
| 167 | return gsub; |
| 168 | } |
| 169 | |
| 170 | void gprs_subscr_cleanup(struct gprs_subscr *subscr) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 171 | { |
Jacob Erlbeck | a1e0373 | 2014-12-02 11:28:38 +0100 | [diff] [blame] | 172 | if (subscr->sgsn_data->mm) { |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 173 | gprs_subscr_put(subscr->sgsn_data->mm->subscr); |
Jacob Erlbeck | a1e0373 | 2014-12-02 11:28:38 +0100 | [diff] [blame] | 174 | subscr->sgsn_data->mm->subscr = NULL; |
| 175 | subscr->sgsn_data->mm = NULL; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Holger Hans Peter Freyther | 1d778fd | 2015-01-20 21:14:03 +0100 | [diff] [blame] | 178 | if (subscr->flags & GPRS_SUBSCRIBER_ENABLE_PURGE) { |
| 179 | gprs_subscr_purge(subscr); |
| 180 | subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE; |
Jacob Erlbeck | 0f47b8f | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 181 | } |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 184 | void gprs_subscr_cancel(struct gprs_subscr *subscr) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 185 | { |
| 186 | subscr->authorized = 0; |
| 187 | subscr->flags |= GPRS_SUBSCRIBER_CANCELLED; |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 188 | subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 189 | |
| 190 | gprs_subscr_update(subscr); |
Jacob Erlbeck | 3e4e58f | 2015-01-26 11:07:24 +0100 | [diff] [blame] | 191 | gprs_subscr_cleanup(subscr); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 194 | static int gprs_subscr_tx_gsup_message(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 195 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 196 | { |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 197 | struct msgb *msg = gsup_client_msgb_alloc(); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 198 | |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 199 | if (strlen(gsup_msg->imsi) == 0 && subscr) |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 200 | osmo_strlcpy(gsup_msg->imsi, subscr->imsi, |
| 201 | sizeof(gsup_msg->imsi)); |
Max | 896c6f8 | 2016-07-01 15:24:44 +0200 | [diff] [blame] | 202 | gsup_msg->cn_domain = OSMO_GSUP_CN_DOMAIN_PS; |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 203 | osmo_gsup_encode(msg, gsup_msg); |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 204 | |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 205 | LOGGSUBSCRP(LOGL_INFO, subscr, |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 206 | "Sending GSUP, will send: %s\n", msgb_hexdump(msg)); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 207 | |
Jacob Erlbeck | 39f040d | 2014-12-18 12:46:47 +0100 | [diff] [blame] | 208 | if (!sgsn->gsup_client) { |
| 209 | msgb_free(msg); |
| 210 | return -ENOTSUP; |
| 211 | } |
| 212 | |
Neels Hofmeyr | 814fef0 | 2016-12-08 21:19:57 +0100 | [diff] [blame] | 213 | return gsup_client_send(sgsn->gsup_client, msg); |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 216 | static int gprs_subscr_tx_gsup_error_reply(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 217 | struct osmo_gsup_message *gsup_orig, |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 218 | enum gsm48_gmm_cause cause) |
| 219 | { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 220 | struct osmo_gsup_message gsup_reply = {0}; |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 221 | |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 222 | osmo_strlcpy(gsup_reply.imsi, gsup_orig->imsi, |
| 223 | sizeof(gsup_reply.imsi)); |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 224 | gsup_reply.cause = cause; |
| 225 | gsup_reply.message_type = |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 226 | OSMO_GSUP_TO_MSGT_ERROR(gsup_orig->message_type); |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 227 | |
| 228 | return gprs_subscr_tx_gsup_message(subscr, &gsup_reply); |
| 229 | } |
| 230 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 231 | static int gprs_subscr_handle_gsup_auth_res(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 232 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 233 | { |
| 234 | unsigned idx; |
| 235 | struct sgsn_subscriber_data *sdata = subscr->sgsn_data; |
| 236 | |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 237 | LOGGSUBSCRP(LOGL_INFO, subscr, |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 238 | "Got SendAuthenticationInfoResult, num_auth_vectors = %zu\n", |
| 239 | gsup_msg->num_auth_vectors); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 240 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 241 | if (gsup_msg->num_auth_vectors > 0) { |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 242 | memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets)); |
| 243 | |
| 244 | for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++) |
| 245 | sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL; |
| 246 | } |
| 247 | |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 248 | for (idx = 0; idx < gsup_msg->num_auth_vectors; idx++) { |
| 249 | size_t key_seq = idx; |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 250 | LOGGSUBSCRP(LOGL_DEBUG, subscr, |
Holger Hans Peter Freyther | 8e6ecc9 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 251 | "Adding auth tuple, cksn = %zu\n", key_seq); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 252 | if (key_seq >= ARRAY_SIZE(sdata->auth_triplets)) { |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 253 | LOGGSUBSCRP(LOGL_NOTICE, subscr, |
Holger Hans Peter Freyther | 8e6ecc9 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 254 | "Skipping auth triplet with invalid cksn %zu\n", |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 255 | key_seq); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 256 | continue; |
| 257 | } |
Harald Welte | 121e9a4 | 2016-04-20 13:13:19 +0200 | [diff] [blame] | 258 | sdata->auth_triplets[key_seq].vec = gsup_msg->auth_vectors[idx]; |
| 259 | sdata->auth_triplets[key_seq].key_seq = key_seq; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | sdata->auth_triplets_updated = 1; |
Jacob Erlbeck | d6267d1 | 2015-01-19 11:10:04 +0100 | [diff] [blame] | 263 | sdata->error_cause = SGSN_ERROR_CAUSE_NONE; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 264 | |
| 265 | gprs_subscr_update_auth_info(subscr); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 270 | static int gprs_subscr_pdp_data_clear(struct gprs_subscr *subscr) |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 271 | { |
| 272 | struct sgsn_subscriber_pdp_data *pdp, *pdp2; |
| 273 | int count = 0; |
| 274 | |
| 275 | llist_for_each_entry_safe(pdp, pdp2, &subscr->sgsn_data->pdp_list, list) { |
| 276 | llist_del(&pdp->list); |
| 277 | talloc_free(pdp); |
| 278 | count += 1; |
| 279 | } |
| 280 | |
| 281 | return count; |
| 282 | } |
| 283 | |
| 284 | static struct sgsn_subscriber_pdp_data *gprs_subscr_pdp_data_get_by_id( |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 285 | struct gprs_subscr *subscr, unsigned context_id) |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 286 | { |
| 287 | struct sgsn_subscriber_pdp_data *pdp; |
| 288 | |
| 289 | llist_for_each_entry(pdp, &subscr->sgsn_data->pdp_list, list) { |
| 290 | if (pdp->context_id == context_id) |
| 291 | return pdp; |
| 292 | } |
| 293 | |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 298 | static void gprs_subscr_gsup_insert_data(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 299 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 300 | { |
Holger Hans Peter Freyther | 9ba273d | 2015-04-23 09:53:53 -0400 | [diff] [blame] | 301 | struct sgsn_subscriber_data *sdata = subscr->sgsn_data; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 302 | unsigned idx; |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 303 | int rc; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 304 | |
Holger Hans Peter Freyther | 9ba273d | 2015-04-23 09:53:53 -0400 | [diff] [blame] | 305 | if (gsup_msg->msisdn_enc) { |
| 306 | if (gsup_msg->msisdn_enc_len > sizeof(sdata->msisdn)) { |
| 307 | LOGP(DGPRS, LOGL_ERROR, "MSISDN too long (%zu)\n", |
| 308 | gsup_msg->msisdn_enc_len); |
| 309 | sdata->msisdn_len = 0; |
| 310 | } else { |
| 311 | memcpy(sdata->msisdn, gsup_msg->msisdn_enc, |
| 312 | gsup_msg->msisdn_enc_len); |
| 313 | sdata->msisdn_len = gsup_msg->msisdn_enc_len; |
| 314 | } |
| 315 | } |
| 316 | |
Holger Hans Peter Freyther | 10c0f56 | 2015-05-17 20:58:40 +0200 | [diff] [blame] | 317 | if (gsup_msg->hlr_enc) { |
| 318 | if (gsup_msg->hlr_enc_len > sizeof(sdata->hlr)) { |
| 319 | LOGP(DGPRS, LOGL_ERROR, "HLR-Number too long (%zu)\n", |
| 320 | gsup_msg->hlr_enc_len); |
| 321 | sdata->hlr_len = 0; |
| 322 | } else { |
| 323 | memcpy(sdata->hlr, gsup_msg->hlr_enc, |
| 324 | gsup_msg->hlr_enc_len); |
| 325 | sdata->hlr_len = gsup_msg->hlr_enc_len; |
| 326 | } |
| 327 | } |
| 328 | |
Holger Hans Peter Freyther | 9d6f062 | 2017-07-09 13:18:17 +0200 | [diff] [blame] | 329 | if (gsup_msg->pdp_charg_enc && gsup_msg->pdp_charg_enc_len >= sizeof(sdata->pdp_charg)) { |
| 330 | memcpy(&sdata->pdp_charg, gsup_msg->pdp_charg_enc, sizeof(sdata->pdp_charg)); |
| 331 | sdata->has_pdp_charg = 1; |
| 332 | } else { |
| 333 | sdata->has_pdp_charg = 0; |
| 334 | } |
| 335 | |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 336 | if (gsup_msg->pdp_info_compl) { |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 337 | rc = gprs_subscr_pdp_data_clear(subscr); |
| 338 | if (rc > 0) |
| 339 | LOGP(DGPRS, LOGL_INFO, "Cleared existing PDP info\n"); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | for (idx = 0; idx < gsup_msg->num_pdp_infos; idx++) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 343 | struct osmo_gsup_pdp_info *pdp_info = &gsup_msg->pdp_infos[idx]; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 344 | size_t ctx_id = pdp_info->context_id; |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 345 | struct sgsn_subscriber_pdp_data *pdp_data; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 346 | |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 347 | if (pdp_info->apn_enc_len >= sizeof(pdp_data->apn_str)-1) { |
| 348 | LOGGSUBSCRP(LOGL_ERROR, subscr, |
Holger Hans Peter Freyther | 8e6ecc9 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 349 | "APN too long, context id = %zu, APN = %s\n", |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 350 | ctx_id, osmo_hexdump(pdp_info->apn_enc, |
| 351 | pdp_info->apn_enc_len)); |
| 352 | continue; |
| 353 | } |
| 354 | |
Holger Hans Peter Freyther | 8cedded | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 355 | if (pdp_info->qos_enc_len > sizeof(pdp_data->qos_subscribed)) { |
| 356 | LOGGSUBSCRP(LOGL_ERROR, subscr, |
| 357 | "QoS info too long (%zu)\n", |
| 358 | pdp_info->qos_enc_len); |
| 359 | continue; |
| 360 | } |
| 361 | |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 362 | LOGGSUBSCRP(LOGL_INFO, subscr, |
Holger Hans Peter Freyther | 8e6ecc9 | 2015-04-23 11:55:23 -0400 | [diff] [blame] | 363 | "Will set PDP info, context id = %zu, APN = %s\n", |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 364 | ctx_id, osmo_hexdump(pdp_info->apn_enc, pdp_info->apn_enc_len)); |
| 365 | |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 366 | /* Set PDP info [ctx_id] */ |
| 367 | pdp_data = gprs_subscr_pdp_data_get_by_id(subscr, ctx_id); |
| 368 | if (!pdp_data) { |
| 369 | pdp_data = sgsn_subscriber_pdp_data_alloc(subscr->sgsn_data); |
| 370 | pdp_data->context_id = ctx_id; |
| 371 | } |
| 372 | |
| 373 | OSMO_ASSERT(pdp_data != NULL); |
| 374 | pdp_data->pdp_type = pdp_info->pdp_type; |
Harald Welte | 586e681 | 2017-08-12 13:43:54 +0200 | [diff] [blame^] | 375 | osmo_apn_to_str(pdp_data->apn_str, |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 376 | pdp_info->apn_enc, pdp_info->apn_enc_len); |
Holger Hans Peter Freyther | 8cedded | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 377 | memcpy(pdp_data->qos_subscribed, pdp_info->qos_enc, pdp_info->qos_enc_len); |
| 378 | pdp_data->qos_subscribed_len = pdp_info->qos_enc_len; |
Holger Hans Peter Freyther | 9d6f062 | 2017-07-09 13:18:17 +0200 | [diff] [blame] | 379 | |
| 380 | if (pdp_info->pdp_charg_enc && pdp_info->pdp_charg_enc_len >= sizeof(pdp_data->pdp_charg)) { |
| 381 | memcpy(&pdp_data->pdp_charg, pdp_info->pdp_charg_enc, sizeof(pdp_data->pdp_charg)); |
| 382 | pdp_data->has_pdp_charg = 1; |
| 383 | } else { |
| 384 | pdp_data->has_pdp_charg = 0; |
| 385 | } |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 386 | } |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 389 | static int gprs_subscr_handle_gsup_upd_loc_res(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 390 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 391 | { |
Harald Welte | 5f2524f | 2016-05-05 18:25:12 +0200 | [diff] [blame] | 392 | /* contrary to MAP, we allow piggy-backing subscriber data onto |
| 393 | * the UPDATE LOCATION RESULT, and don't mandate the use of a |
| 394 | * separate nested INSERT SUBSCRIBER DATA transaction */ |
Jacob Erlbeck | 0e8add6 | 2014-12-17 14:03:35 +0100 | [diff] [blame] | 395 | gprs_subscr_gsup_insert_data(subscr, gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 396 | |
| 397 | subscr->authorized = 1; |
Jacob Erlbeck | d6267d1 | 2015-01-19 11:10:04 +0100 | [diff] [blame] | 398 | subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 399 | |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 400 | subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE; |
| 401 | |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 402 | gprs_subscr_update(subscr); |
| 403 | return 0; |
| 404 | } |
| 405 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 406 | static int gprs_subscr_handle_gsup_dsd_req(struct gprs_subscr *subscr, |
Max | a66d8cf | 2017-02-15 11:43:59 +0100 | [diff] [blame] | 407 | struct osmo_gsup_message *gsup_msg) |
| 408 | { |
| 409 | struct osmo_gsup_message gsup_reply = {0}; |
| 410 | |
| 411 | if (gsup_msg->cn_domain != OSMO_GSUP_CN_DOMAIN_PS) { |
| 412 | LOGGSUBSCRP(LOGL_ERROR, subscr, |
| 413 | "Rx GSUP message %s not supported for CS\n", |
| 414 | osmo_gsup_message_type_name(gsup_msg->message_type)); |
| 415 | gsup_reply.cause = GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL; |
| 416 | gsup_reply.message_type = OSMO_GSUP_MSGT_DELETE_DATA_ERROR; |
| 417 | } else { |
| 418 | gsm0408_gprs_access_cancelled(subscr->sgsn_data->mm, |
| 419 | GMM_CAUSE_GPRS_NOTALLOWED); |
| 420 | gsup_reply.message_type = OSMO_GSUP_MSGT_DELETE_DATA_RESULT; |
| 421 | } |
| 422 | |
| 423 | return gprs_subscr_tx_gsup_message(subscr, &gsup_reply); |
| 424 | } |
| 425 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 426 | static int gprs_subscr_handle_gsup_isd_req(struct gprs_subscr *subscr, |
Harald Welte | 5f2524f | 2016-05-05 18:25:12 +0200 | [diff] [blame] | 427 | struct osmo_gsup_message *gsup_msg) |
| 428 | { |
| 429 | struct osmo_gsup_message gsup_reply = {0}; |
| 430 | |
| 431 | gprs_subscr_gsup_insert_data(subscr, gsup_msg); |
| 432 | |
| 433 | subscr->authorized = 1; |
| 434 | subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE; |
| 435 | subscr->flags |= GPRS_SUBSCRIBER_ENABLE_PURGE; |
| 436 | gprs_subscr_update(subscr); |
| 437 | |
| 438 | gsup_reply.message_type = OSMO_GSUP_MSGT_INSERT_DATA_RESULT; |
| 439 | return gprs_subscr_tx_gsup_message(subscr, &gsup_reply); |
| 440 | } |
| 441 | |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 442 | static int check_cause(int cause) |
| 443 | { |
| 444 | switch (cause) { |
| 445 | case GMM_CAUSE_IMSI_UNKNOWN ... GMM_CAUSE_ILLEGAL_ME: |
| 446 | case GMM_CAUSE_GPRS_NOTALLOWED ... GMM_CAUSE_NO_GPRS_PLMN: |
| 447 | return EACCES; |
| 448 | |
| 449 | case GMM_CAUSE_MSC_TEMP_NOTREACH ... GMM_CAUSE_CONGESTION: |
Jacob Erlbeck | f06fe29 | 2015-01-05 16:20:47 +0100 | [diff] [blame] | 450 | return EHOSTUNREACH; |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 451 | |
| 452 | case GMM_CAUSE_SEM_INCORR_MSG ... GMM_CAUSE_PROTO_ERR_UNSPEC: |
| 453 | default: |
| 454 | return EINVAL; |
| 455 | } |
| 456 | } |
| 457 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 458 | static int gprs_subscr_handle_gsup_auth_err(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 459 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 460 | { |
| 461 | unsigned idx; |
| 462 | struct sgsn_subscriber_data *sdata = subscr->sgsn_data; |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 463 | int cause_err; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 464 | |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 465 | cause_err = check_cause(gsup_msg->cause); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 466 | |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 467 | LOGGSUBSCRP(LOGL_DEBUG, subscr, |
| 468 | "Send authentication info has failed with cause %d, " |
| 469 | "handled as: %s\n", |
| 470 | gsup_msg->cause, strerror(cause_err)); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 471 | |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 472 | switch (cause_err) { |
| 473 | case EACCES: |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 474 | LOGGSUBSCRP(LOGL_NOTICE, subscr, |
| 475 | "GPRS send auth info req failed, access denied, " |
| 476 | "GMM cause = '%s' (%d)\n", |
| 477 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 478 | gsup_msg->cause); |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 479 | /* Clear auth tuples */ |
| 480 | memset(sdata->auth_triplets, 0, sizeof(sdata->auth_triplets)); |
| 481 | for (idx = 0; idx < ARRAY_SIZE(sdata->auth_triplets); idx++) |
| 482 | sdata->auth_triplets[idx].key_seq = GSM_KEY_SEQ_INVAL; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 483 | |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 484 | subscr->authorized = 0; |
| 485 | sdata->error_cause = gsup_msg->cause; |
| 486 | gprs_subscr_update_auth_info(subscr); |
| 487 | break; |
| 488 | |
Jacob Erlbeck | f06fe29 | 2015-01-05 16:20:47 +0100 | [diff] [blame] | 489 | case EHOSTUNREACH: |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 490 | LOGGSUBSCRP(LOGL_NOTICE, subscr, |
| 491 | "GPRS send auth info req failed, GMM cause = '%s' (%d)\n", |
| 492 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 493 | gsup_msg->cause); |
Jacob Erlbeck | f06fe29 | 2015-01-05 16:20:47 +0100 | [diff] [blame] | 494 | |
| 495 | sdata->error_cause = gsup_msg->cause; |
| 496 | gprs_subscr_update_auth_info(subscr); |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 497 | break; |
| 498 | |
| 499 | default: |
| 500 | case EINVAL: |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 501 | LOGGSUBSCRP(LOGL_ERROR, subscr, |
| 502 | "GSUP protocol remote error, GMM cause = '%s' (%d)\n", |
| 503 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 504 | gsup_msg->cause); |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | |
| 508 | return -gsup_msg->cause; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 511 | static int gprs_subscr_handle_gsup_upd_loc_err(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 512 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 513 | { |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 514 | int cause_err; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 515 | |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 516 | cause_err = check_cause(gsup_msg->cause); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 517 | |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 518 | LOGGSUBSCRP(LOGL_DEBUG, subscr, |
| 519 | "Update location has failed with cause %d, handled as: %s\n", |
| 520 | gsup_msg->cause, strerror(cause_err)); |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 521 | |
| 522 | switch (cause_err) { |
| 523 | case EACCES: |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 524 | LOGGSUBSCRP(LOGL_NOTICE, subscr, |
| 525 | "GPRS update location failed, access denied, " |
| 526 | "GMM cause = '%s' (%d)\n", |
| 527 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 528 | gsup_msg->cause); |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 529 | |
| 530 | subscr->authorized = 0; |
| 531 | subscr->sgsn_data->error_cause = gsup_msg->cause; |
| 532 | gprs_subscr_update_auth_info(subscr); |
| 533 | break; |
| 534 | |
Jacob Erlbeck | f06fe29 | 2015-01-05 16:20:47 +0100 | [diff] [blame] | 535 | case EHOSTUNREACH: |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 536 | LOGGSUBSCRP(LOGL_NOTICE, subscr, |
| 537 | "GPRS update location failed, GMM cause = '%s' (%d)\n", |
| 538 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 539 | gsup_msg->cause); |
Jacob Erlbeck | f06fe29 | 2015-01-05 16:20:47 +0100 | [diff] [blame] | 540 | |
| 541 | subscr->sgsn_data->error_cause = gsup_msg->cause; |
| 542 | gprs_subscr_update_auth_info(subscr); |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 543 | break; |
| 544 | |
| 545 | default: |
| 546 | case EINVAL: |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 547 | LOGGSUBSCRP(LOGL_ERROR, subscr, |
| 548 | "GSUP protocol remote error, GMM cause = '%s' (%d)\n", |
| 549 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 550 | gsup_msg->cause); |
Jacob Erlbeck | 9aa9991 | 2015-01-05 18:38:41 +0100 | [diff] [blame] | 551 | break; |
| 552 | } |
| 553 | |
| 554 | return -gsup_msg->cause; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Jacob Erlbeck | 929acdf | 2015-01-27 13:47:24 +0100 | [diff] [blame] | 557 | static int gprs_subscr_handle_gsup_purge_no_subscr( |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 558 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | 929acdf | 2015-01-27 13:47:24 +0100 | [diff] [blame] | 559 | { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 560 | if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
Jacob Erlbeck | 929acdf | 2015-01-27 13:47:24 +0100 | [diff] [blame] | 561 | LOGGSUPP(LOGL_NOTICE, gsup_msg, |
| 562 | "Purge MS has failed with cause '%s' (%d)\n", |
| 563 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 564 | gsup_msg->cause); |
| 565 | return -gsup_msg->cause; |
| 566 | } |
| 567 | |
| 568 | LOGGSUPP(LOGL_INFO, gsup_msg, "Completing purge MS\n"); |
| 569 | return 0; |
| 570 | } |
| 571 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 572 | static int gprs_subscr_handle_gsup_purge_res(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 573 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 574 | { |
| 575 | LOGGSUBSCRP(LOGL_INFO, subscr, "Completing purge MS\n"); |
| 576 | |
| 577 | /* Force silent cancellation */ |
Jacob Erlbeck | d6267d1 | 2015-01-19 11:10:04 +0100 | [diff] [blame] | 578 | subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE; |
Jacob Erlbeck | 37139e5 | 2015-01-23 13:52:55 +0100 | [diff] [blame] | 579 | gprs_subscr_cancel(subscr); |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 584 | static int gprs_subscr_handle_gsup_purge_err(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 585 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 586 | { |
| 587 | LOGGSUBSCRP(LOGL_NOTICE, subscr, |
| 588 | "Purge MS has failed with cause '%s' (%d)\n", |
| 589 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 590 | gsup_msg->cause); |
| 591 | |
| 592 | /* In GSM 09.02, 19.1.4.4, the text and the SDL diagram imply that |
| 593 | * the subscriber data is not removed if the request has failed. On the |
| 594 | * other hand, keeping the subscriber data in either error case |
| 595 | * (subscriber unknown, syntactical message error, connection error) |
| 596 | * doesn't seem to give any advantage, since the data will be restored |
| 597 | * on the next Attach Request anyway. |
| 598 | * This approach ensures, that the subscriber record will not stick if |
| 599 | * an error happens. |
| 600 | */ |
| 601 | |
| 602 | /* TODO: Check whether this behaviour is acceptable and either just |
| 603 | * remove this TODO-notice or change the implementation to not delete |
| 604 | * the subscriber data (eventually resetting the ENABLE_PURGE flag and |
| 605 | * restarting the expiry timer based on the cause). |
| 606 | * |
| 607 | * Subscriber Unknown: cancel subscr |
| 608 | * Temporary network problems: do nothing (handled by timer based retry) |
| 609 | * Message problems (syntax, nyi, ...): cancel subscr (retry won't help) |
| 610 | */ |
| 611 | |
| 612 | gprs_subscr_handle_gsup_purge_res(subscr, gsup_msg); |
| 613 | |
| 614 | return -gsup_msg->cause; |
| 615 | } |
| 616 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 617 | static int gprs_subscr_handle_loc_cancel_req(struct gprs_subscr *subscr, |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 618 | struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | 87c7ffc | 2015-01-08 15:29:01 +0100 | [diff] [blame] | 619 | { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 620 | struct osmo_gsup_message gsup_reply = {0}; |
Jacob Erlbeck | 5b51205 | 2015-04-07 17:49:48 +0200 | [diff] [blame] | 621 | int is_update_procedure = !gsup_msg->cancel_type || |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 622 | gsup_msg->cancel_type == OSMO_GSUP_CANCEL_TYPE_UPDATE; |
Jacob Erlbeck | 87c7ffc | 2015-01-08 15:29:01 +0100 | [diff] [blame] | 623 | |
Jacob Erlbeck | 8000e0e | 2015-01-27 14:56:40 +0100 | [diff] [blame] | 624 | LOGGSUBSCRP(LOGL_INFO, subscr, "Cancelling MS subscriber (%s)\n", |
| 625 | is_update_procedure ? |
| 626 | "update procedure" : "subscription withdraw"); |
Jacob Erlbeck | 87c7ffc | 2015-01-08 15:29:01 +0100 | [diff] [blame] | 627 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 628 | gsup_reply.message_type = OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT; |
Jacob Erlbeck | 87c7ffc | 2015-01-08 15:29:01 +0100 | [diff] [blame] | 629 | gprs_subscr_tx_gsup_message(subscr, &gsup_reply); |
| 630 | |
Jacob Erlbeck | 8000e0e | 2015-01-27 14:56:40 +0100 | [diff] [blame] | 631 | if (is_update_procedure) |
| 632 | subscr->sgsn_data->error_cause = SGSN_ERROR_CAUSE_NONE; |
| 633 | else |
| 634 | /* Since a withdraw cause is not specified, just abort the |
| 635 | * current attachment. The following re-attachment should then |
| 636 | * be rejected with a proper cause value. |
| 637 | */ |
| 638 | subscr->sgsn_data->error_cause = GMM_CAUSE_IMPL_DETACHED; |
| 639 | |
Jacob Erlbeck | 37139e5 | 2015-01-23 13:52:55 +0100 | [diff] [blame] | 640 | gprs_subscr_cancel(subscr); |
Jacob Erlbeck | 87c7ffc | 2015-01-08 15:29:01 +0100 | [diff] [blame] | 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 645 | static int gprs_subscr_handle_unknown_imsi(struct osmo_gsup_message *gsup_msg) |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 646 | { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 647 | if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg->message_type)) { |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 648 | gprs_subscr_tx_gsup_error_reply(NULL, gsup_msg, |
| 649 | GMM_CAUSE_IMSI_UNKNOWN); |
| 650 | LOGP(DGPRS, LOGL_NOTICE, |
| 651 | "Unknown IMSI %s, discarding GSUP request " |
| 652 | "of type 0x%02x\n", |
| 653 | gsup_msg->imsi, gsup_msg->message_type); |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 654 | } else if (OSMO_GSUP_IS_MSGT_ERROR(gsup_msg->message_type)) { |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 655 | LOGP(DGPRS, LOGL_NOTICE, |
| 656 | "Unknown IMSI %s, discarding GSUP error " |
| 657 | "of type 0x%02x, cause '%s' (%d)\n", |
| 658 | gsup_msg->imsi, gsup_msg->message_type, |
| 659 | get_value_string(gsm48_gmm_cause_names, gsup_msg->cause), |
| 660 | gsup_msg->cause); |
| 661 | } else { |
| 662 | LOGP(DGPRS, LOGL_NOTICE, |
| 663 | "Unknown IMSI %s, discarding GSUP response " |
| 664 | "of type 0x%02x\n", |
| 665 | gsup_msg->imsi, gsup_msg->message_type); |
| 666 | } |
| 667 | |
| 668 | return -GMM_CAUSE_IMSI_UNKNOWN; |
| 669 | } |
| 670 | |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 671 | int gprs_subscr_rx_gsup_message(struct msgb *msg) |
| 672 | { |
| 673 | uint8_t *data = msgb_l2(msg); |
| 674 | size_t data_len = msgb_l2len(msg); |
| 675 | int rc = 0; |
| 676 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 677 | struct osmo_gsup_message gsup_msg = {0}; |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 678 | struct gprs_subscr *subscr; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 679 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 680 | rc = osmo_gsup_decode(data, data_len, &gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 681 | if (rc < 0) { |
| 682 | LOGP(DGPRS, LOGL_ERROR, |
Jacob Erlbeck | bce2061 | 2015-01-05 18:57:32 +0100 | [diff] [blame] | 683 | "decoding GSUP message fails with error '%s' (%d)\n", |
| 684 | get_value_string(gsm48_gmm_cause_names, -rc), -rc); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 685 | return rc; |
| 686 | } |
| 687 | |
Jacob Erlbeck | 07f6e36 | 2015-01-29 14:00:28 +0100 | [diff] [blame] | 688 | if (!gsup_msg.imsi[0]) { |
| 689 | LOGP(DGPRS, LOGL_ERROR, "Missing IMSI in GSUP message\n"); |
| 690 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 691 | if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type)) |
Jacob Erlbeck | 07f6e36 | 2015-01-29 14:00:28 +0100 | [diff] [blame] | 692 | gprs_subscr_tx_gsup_error_reply(NULL, &gsup_msg, |
| 693 | GMM_CAUSE_INV_MAND_INFO); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 694 | return -GMM_CAUSE_INV_MAND_INFO; |
Jacob Erlbeck | 07f6e36 | 2015-01-29 14:00:28 +0100 | [diff] [blame] | 695 | } |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 696 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 697 | if (!gsup_msg.cause && OSMO_GSUP_IS_MSGT_ERROR(gsup_msg.message_type)) |
Jacob Erlbeck | 9ff8289 | 2015-01-29 14:17:51 +0100 | [diff] [blame] | 698 | gsup_msg.cause = GMM_CAUSE_NET_FAIL; |
| 699 | |
Jacob Erlbeck | 4dedb27 | 2015-01-15 17:50:16 +0100 | [diff] [blame] | 700 | subscr = gprs_subscr_get_by_imsi(gsup_msg.imsi); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 701 | |
Jacob Erlbeck | 929acdf | 2015-01-27 13:47:24 +0100 | [diff] [blame] | 702 | if (!subscr) { |
| 703 | switch (gsup_msg.message_type) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 704 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
| 705 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
Jacob Erlbeck | 929acdf | 2015-01-27 13:47:24 +0100 | [diff] [blame] | 706 | return gprs_subscr_handle_gsup_purge_no_subscr(&gsup_msg); |
| 707 | default: |
| 708 | return gprs_subscr_handle_unknown_imsi(&gsup_msg); |
| 709 | } |
| 710 | } |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 711 | |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 712 | LOGGSUBSCRP(LOGL_INFO, subscr, |
Max | a66d8cf | 2017-02-15 11:43:59 +0100 | [diff] [blame] | 713 | "Received GSUP message %s\n", |
| 714 | osmo_gsup_message_type_name(gsup_msg.message_type)); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 715 | |
| 716 | switch (gsup_msg.message_type) { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 717 | case OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST: |
Jacob Erlbeck | 87c7ffc | 2015-01-08 15:29:01 +0100 | [diff] [blame] | 718 | rc = gprs_subscr_handle_loc_cancel_req(subscr, &gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 719 | break; |
| 720 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 721 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT: |
Jacob Erlbeck | bce2061 | 2015-01-05 18:57:32 +0100 | [diff] [blame] | 722 | rc = gprs_subscr_handle_gsup_auth_res(subscr, &gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 723 | break; |
| 724 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 725 | case OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR: |
Jacob Erlbeck | bce2061 | 2015-01-05 18:57:32 +0100 | [diff] [blame] | 726 | rc = gprs_subscr_handle_gsup_auth_err(subscr, &gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 727 | break; |
| 728 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 729 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT: |
Jacob Erlbeck | bce2061 | 2015-01-05 18:57:32 +0100 | [diff] [blame] | 730 | rc = gprs_subscr_handle_gsup_upd_loc_res(subscr, &gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 731 | break; |
| 732 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 733 | case OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR: |
Jacob Erlbeck | bce2061 | 2015-01-05 18:57:32 +0100 | [diff] [blame] | 734 | rc = gprs_subscr_handle_gsup_upd_loc_err(subscr, &gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 735 | break; |
| 736 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 737 | case OSMO_GSUP_MSGT_PURGE_MS_ERROR: |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 738 | rc = gprs_subscr_handle_gsup_purge_err(subscr, &gsup_msg); |
| 739 | break; |
| 740 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 741 | case OSMO_GSUP_MSGT_PURGE_MS_RESULT: |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 742 | rc = gprs_subscr_handle_gsup_purge_res(subscr, &gsup_msg); |
| 743 | break; |
| 744 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 745 | case OSMO_GSUP_MSGT_INSERT_DATA_REQUEST: |
Harald Welte | 5f2524f | 2016-05-05 18:25:12 +0200 | [diff] [blame] | 746 | rc = gprs_subscr_handle_gsup_isd_req(subscr, &gsup_msg); |
| 747 | break; |
| 748 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 749 | case OSMO_GSUP_MSGT_DELETE_DATA_REQUEST: |
Max | a66d8cf | 2017-02-15 11:43:59 +0100 | [diff] [blame] | 750 | rc = gprs_subscr_handle_gsup_dsd_req(subscr, &gsup_msg); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 751 | break; |
| 752 | |
| 753 | default: |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 754 | LOGGSUBSCRP(LOGL_ERROR, subscr, |
Max | a66d8cf | 2017-02-15 11:43:59 +0100 | [diff] [blame] | 755 | "Rx GSUP message %s not valid at SGSN\n", |
| 756 | osmo_gsup_message_type_name(gsup_msg.message_type)); |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 757 | if (OSMO_GSUP_IS_MSGT_REQUEST(gsup_msg.message_type)) |
Jacob Erlbeck | 9999fd9 | 2015-01-15 17:08:30 +0100 | [diff] [blame] | 758 | gprs_subscr_tx_gsup_error_reply( |
| 759 | subscr, &gsup_msg, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL); |
| 760 | rc = -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 761 | break; |
| 762 | }; |
| 763 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 764 | gprs_subscr_put(subscr); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 765 | |
| 766 | return rc; |
| 767 | } |
| 768 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 769 | int gprs_subscr_purge(struct gprs_subscr *subscr) |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 770 | { |
Holger Hans Peter Freyther | 10c0f56 | 2015-05-17 20:58:40 +0200 | [diff] [blame] | 771 | struct sgsn_subscriber_data *sdata = subscr->sgsn_data; |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 772 | struct osmo_gsup_message gsup_msg = {0}; |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 773 | |
| 774 | LOGGSUBSCRP(LOGL_INFO, subscr, "purging MS subscriber\n"); |
| 775 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 776 | gsup_msg.message_type = OSMO_GSUP_MSGT_PURGE_MS_REQUEST; |
Holger Hans Peter Freyther | 10c0f56 | 2015-05-17 20:58:40 +0200 | [diff] [blame] | 777 | |
| 778 | /* Provide the HLR number in case it is known */ |
| 779 | gsup_msg.hlr_enc_len = sdata->hlr_len; |
| 780 | gsup_msg.hlr_enc = sdata->hlr; |
| 781 | |
Jacob Erlbeck | ca69b0f | 2015-02-03 19:45:46 +0100 | [diff] [blame] | 782 | return gprs_subscr_tx_gsup_message(subscr, &gsup_msg); |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 783 | } |
| 784 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 785 | static int gprs_subscr_query_auth_info(struct gprs_subscr *subscr, |
| 786 | const uint8_t *auts, |
| 787 | const uint8_t *auts_rand) |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 788 | { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 789 | struct osmo_gsup_message gsup_msg = {0}; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 790 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 791 | /* Make sure we have a complete resync or clearly no resync. */ |
| 792 | OSMO_ASSERT((auts != NULL) == (auts_rand != NULL)); |
| 793 | |
| 794 | LOGGSUBSCRP(LOGL_INFO, subscr, "requesting auth info%s\n", |
| 795 | auts ? " with AUTS (UMTS Resynch)" : ""); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 796 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 797 | gsup_msg.message_type = OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST; |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 798 | gsup_msg.auts = auts; |
| 799 | gsup_msg.rand = auts_rand; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 800 | return gprs_subscr_tx_gsup_message(subscr, &gsup_msg); |
| 801 | } |
| 802 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 803 | int gprs_subscr_location_update(struct gprs_subscr *subscr) |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 804 | { |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 805 | struct osmo_gsup_message gsup_msg = {0}; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 806 | |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 807 | LOGGSUBSCRP(LOGL_INFO, subscr, |
| 808 | "subscriber data is not available\n"); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 809 | |
Harald Welte | 28903a9 | 2016-04-25 14:53:43 +0200 | [diff] [blame] | 810 | gsup_msg.message_type = OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST; |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 811 | return gprs_subscr_tx_gsup_message(subscr, &gsup_msg); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 812 | } |
| 813 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 814 | void gprs_subscr_update(struct gprs_subscr *subscr) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 815 | { |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 816 | LOGGSUBSCRP(LOGL_DEBUG, subscr, "Updating subscriber data\n"); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 817 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 818 | subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING; |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 819 | subscr->flags &= ~GPRS_SUBSCRIBER_FIRST_CONTACT; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 820 | |
Jacob Erlbeck | 555b2e5 | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 821 | if (subscr->sgsn_data->mm) |
| 822 | sgsn_update_subscriber_data(subscr->sgsn_data->mm); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 825 | void gprs_subscr_update_auth_info(struct gprs_subscr *subscr) |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 826 | { |
Jacob Erlbeck | bf34c67 | 2014-12-23 14:24:16 +0100 | [diff] [blame] | 827 | LOGGSUBSCRP(LOGL_DEBUG, subscr, |
| 828 | "Updating subscriber authentication info\n"); |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 829 | |
| 830 | subscr->flags &= ~GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING; |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 831 | subscr->flags &= ~GPRS_SUBSCRIBER_FIRST_CONTACT; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 832 | |
Jacob Erlbeck | 555b2e5 | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 833 | if (subscr->sgsn_data->mm) |
| 834 | sgsn_update_subscriber_data(subscr->sgsn_data->mm); |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 837 | struct gprs_subscr *gprs_subscr_get_or_create_by_mmctx(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 838 | { |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 839 | struct gprs_subscr *subscr = NULL; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 840 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 841 | if (mmctx->subscr) |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 842 | return gprs_subscr_get(mmctx->subscr); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 843 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 844 | if (mmctx->imsi[0]) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 845 | subscr = gprs_subscr_get_by_imsi(mmctx->imsi); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 846 | |
| 847 | if (!subscr) { |
| 848 | subscr = gprs_subscr_get_or_create(mmctx->imsi); |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 849 | subscr->flags |= GPRS_SUBSCRIBER_FIRST_CONTACT; |
Jacob Erlbeck | 65fa3f7 | 2015-01-06 16:32:41 +0100 | [diff] [blame] | 850 | subscr->flags &= ~GPRS_SUBSCRIBER_ENABLE_PURGE; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 853 | osmo_strlcpy(subscr->imei, mmctx->imei, sizeof(subscr->imei)); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 854 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 855 | if (subscr->lac != mmctx->ra.lac) |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 856 | subscr->lac = mmctx->ra.lac; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 857 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 858 | subscr->sgsn_data->mm = mmctx; |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 859 | mmctx->subscr = gprs_subscr_get(subscr); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 860 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 861 | return subscr; |
| 862 | } |
| 863 | |
| 864 | int gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx) |
| 865 | { |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 866 | struct gprs_subscr *subscr = NULL; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 867 | int rc; |
| 868 | |
| 869 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber data update\n"); |
| 870 | |
| 871 | subscr = gprs_subscr_get_or_create_by_mmctx(mmctx); |
| 872 | |
| 873 | subscr->flags |= GPRS_SUBSCRIBER_UPDATE_LOCATION_PENDING; |
| 874 | |
| 875 | rc = gprs_subscr_location_update(subscr); |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 876 | gprs_subscr_put(subscr); |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 877 | return rc; |
| 878 | } |
| 879 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 880 | /*! \brief Send Update Auth Info request via GSUP, with or without resync. |
| 881 | * \param[in] mmctx MM context to request authentication tuples for. |
| 882 | * \param[in] auts 14 octet AUTS token for UMTS resync, or NULL. |
| 883 | * \param[in] auts_rand 16 octet Random token for UMTS resync, or NULL. |
| 884 | * In case of normal Authentication Info request, both \a auts and \a auts_rand |
| 885 | * must be NULL. For resync, both must be non-NULL. |
| 886 | */ |
| 887 | int gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx, |
| 888 | const uint8_t *auts, |
| 889 | const uint8_t *auts_rand) |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 890 | { |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 891 | struct gprs_subscr *subscr = NULL; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 892 | int rc; |
| 893 | |
| 894 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Requesting subscriber authentication info\n"); |
| 895 | |
| 896 | subscr = gprs_subscr_get_or_create_by_mmctx(mmctx); |
| 897 | |
| 898 | subscr->flags |= GPRS_SUBSCRIBER_UPDATE_AUTH_INFO_PENDING; |
| 899 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 900 | rc = gprs_subscr_query_auth_info(subscr, auts, auts_rand); |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 901 | gprs_subscr_put(subscr); |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 902 | return rc; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 903 | } |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 904 | |
| 905 | static void gprs_subscr_free(struct gprs_subscr *gsub) |
| 906 | { |
| 907 | llist_del(&gsub->entry); |
| 908 | talloc_free(gsub); |
| 909 | } |
| 910 | |
| 911 | struct gprs_subscr *_gprs_subscr_get(struct gprs_subscr *gsub, |
| 912 | const char *file, int line) |
| 913 | { |
| 914 | OSMO_ASSERT(gsub->use_count < INT_MAX); |
| 915 | gsub->use_count++; |
| 916 | LOGPSRC(DREF, LOGL_DEBUG, file, line, |
| 917 | "subscr %s usage increases to: %d\n", |
| 918 | gsub->imsi, gsub->use_count); |
| 919 | return gsub; |
| 920 | } |
| 921 | |
| 922 | struct gprs_subscr *_gprs_subscr_put(struct gprs_subscr *gsub, |
| 923 | const char *file, int line) |
| 924 | { |
| 925 | gsub->use_count--; |
| 926 | LOGPSRC(DREF, gsub->use_count >= 0? LOGL_DEBUG : LOGL_ERROR, |
| 927 | file, line, |
| 928 | "subscr %s usage decreases to: %d%s\n", |
| 929 | gsub->imsi, gsub->use_count, |
| 930 | gsub->keep_in_ram? ", keep-in-ram flag is set" : ""); |
| 931 | if (gsub->use_count > 0) |
| 932 | return gsub; |
| 933 | if (gsub->keep_in_ram) |
| 934 | return gsub; |
| 935 | gprs_subscr_free(gsub); |
| 936 | return NULL; |
| 937 | } |