Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GPRS SGSN functionality */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 23 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 24 | #include <osmocom/core/linuxlist.h> |
| 25 | #include <osmocom/core/talloc.h> |
| 26 | #include <osmocom/core/timer.h> |
| 27 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 28 | #include <osmocom/core/stats.h> |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 29 | #include <osmocom/core/backtrace.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 30 | #include <osmocom/gprs/gprs_ns.h> |
| 31 | #include <osmocom/gprs/gprs_bssgp.h> |
Harald Welte | 53373bc | 2016-04-20 17:11:43 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 33 | |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 34 | #include <openbsc/gprs_subscriber.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 36 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 37 | #include <openbsc/sgsn.h> |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 38 | #include <openbsc/gprs_gmm.h> |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 39 | #include <openbsc/gprs_utils.h> |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 40 | #include <openbsc/signal.h> |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 41 | #include "openbsc/gprs_llc.h" |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 42 | #include <openbsc/iu.h> |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 43 | |
Neels Hofmeyr | f4daf16 | 2016-05-21 00:44:50 +0200 | [diff] [blame] | 44 | #include <pdp.h> |
| 45 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 46 | #include <time.h> |
| 47 | |
Daniel Willmann | 044ce5f | 2015-10-12 19:36:33 +0200 | [diff] [blame] | 48 | #include <openssl/rand.h> |
| 49 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 50 | #define GPRS_LLME_CHECK_TICK 30 |
| 51 | |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 52 | extern struct sgsn_instance *sgsn; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 53 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 54 | LLIST_HEAD(sgsn_mm_ctxts); |
| 55 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 56 | LLIST_HEAD(sgsn_apn_ctxts); |
| 57 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 58 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 59 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 60 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 61 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 62 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 63 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 64 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 65 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 66 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 67 | { "suspend", "SUSPEND Count " }, |
| 68 | { "paging.ps", "Paging Packet Switched " }, |
| 69 | { "paging.cs", "Paging Circuit Switched " }, |
| 70 | { "ra_update", "Routing Area Update " }, |
| 71 | }; |
| 72 | |
| 73 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 74 | .group_name_prefix = "sgsn.mmctx", |
| 75 | .group_description = "SGSN MM Context Statistics", |
| 76 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 77 | .ctr_desc = mmctx_ctr_description, |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 78 | .class_id = OSMO_STATS_CLASS_SUBSCRIBER, |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 81 | static const struct rate_ctr_desc pdpctx_ctr_description[] = { |
| 82 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 83 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 84 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 85 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 86 | }; |
| 87 | |
| 88 | static const struct rate_ctr_group_desc pdpctx_ctrg_desc = { |
| 89 | .group_name_prefix = "sgsn.pdpctx", |
| 90 | .group_description = "SGSN PDP Context Statistics", |
| 91 | .num_ctr = ARRAY_SIZE(pdpctx_ctr_description), |
| 92 | .ctr_desc = pdpctx_ctr_description, |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 93 | .class_id = OSMO_STATS_CLASS_SUBSCRIBER, |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 96 | static const struct rate_ctr_desc sgsn_ctr_description[] = { |
Alexander Couzens | 4e699a9 | 2016-07-05 11:04:27 +0200 | [diff] [blame] | 97 | { "llc.dl_bytes", "Count sent LLC bytes before giving it to the bssgp layer" }, |
| 98 | { "llc.ul_bytes", "Count sucessful received LLC bytes (encrypt & fcs correct)" }, |
| 99 | { "llc.dl_packets", "Count sucessful sent LLC packets before giving it to the bssgp layer" }, |
| 100 | { "llc.ul_packets", "Count sucessful received LLC packets (encrypt & fcs correct)" }, |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 101 | { "gprs.attach_requested", "Received attach requests" }, |
| 102 | { "gprs.attach_accepted", "Sent attach accepts" }, |
| 103 | { "gprs.attach_rejected", "Sent attach rejects" }, |
| 104 | { "gprs.detach_requested", "Received detach requests" }, |
| 105 | { "gprs.detach_acked", "Sent detach acks" }, |
| 106 | { "gprs.routing_area_requested", "Received routing area requests" }, |
| 107 | { "gprs.routing_area_requested", "Sent routing area acks" }, |
| 108 | { "gprs.routing_area_requested", "Sent routing area rejects" }, |
| 109 | { "pdp.activate_requested", "Received activate requests" }, |
| 110 | { "pdp.activate_rejected", "Sent activate rejects" }, |
| 111 | { "pdp.activate_accepted", "Sent activate accepts" }, |
| 112 | { "pdp.request_activated", "unused" }, |
| 113 | { "pdp.request_activate_rejected", "unused" }, |
| 114 | { "pdp.modify_requested", "unused" }, |
| 115 | { "pdp.modify_accepted", "unused" }, |
| 116 | { "pdp.dl_deactivate_requested", "Sent deactivate requests" }, |
| 117 | { "pdp.dl_deactivate_accepted", "Sent deactivate accepted" }, |
| 118 | { "pdp.ul_deactivate_requested", "Received deactivate requests" }, |
| 119 | { "pdp.ul_deactivate_accepted", "Received deactivate accepts" }, |
| 120 | }; |
| 121 | |
| 122 | static const struct rate_ctr_group_desc sgsn_ctrg_desc = { |
| 123 | "sgsn", |
| 124 | "SGSN Overall Statistics", |
| 125 | OSMO_STATS_CLASS_GLOBAL, |
| 126 | ARRAY_SIZE(sgsn_ctr_description), |
| 127 | sgsn_ctr_description, |
| 128 | }; |
| 129 | |
| 130 | void sgsn_rate_ctr_init() { |
| 131 | sgsn->rate_ctrs = rate_ctr_group_alloc(tall_bsc_ctx, &sgsn_ctrg_desc, 0); |
| 132 | } |
| 133 | |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 134 | /* look-up an SGSN MM context based on Iu UE context (struct ue_conn_ctx)*/ |
| 135 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_ue_ctx(const void *uectx) |
| 136 | { |
| 137 | struct sgsn_mm_ctx *ctx; |
| 138 | |
| 139 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 140 | if (ctx->ran_type == MM_CTX_T_UTRAN_Iu |
| 141 | && uectx == ctx->iu.ue_ctx) |
| 142 | return ctx; |
| 143 | } |
| 144 | |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 148 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 149 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 150 | const struct gprs_ra_id *raid) |
| 151 | { |
| 152 | struct sgsn_mm_ctx *ctx; |
| 153 | |
| 154 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 155 | if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) && |
Jacob Erlbeck | e7bcdc3 | 2016-01-04 18:43:34 +0100 | [diff] [blame] | 156 | gprs_ra_id_equals(raid, &ctx->ra)) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 157 | return ctx; |
| 158 | } |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 159 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
| 162 | |
Jacob Erlbeck | 5ac4aad | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 163 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli, |
| 164 | const struct gprs_ra_id *raid) |
| 165 | { |
| 166 | struct sgsn_mm_ctx *ctx; |
| 167 | int tlli_type; |
| 168 | |
| 169 | /* TODO: Also check the P_TMSI signature to be safe. That signature |
| 170 | * should be different (at least with a sufficiently high probability) |
| 171 | * after SGSN restarts and for multiple SGSN instances. |
| 172 | */ |
| 173 | |
| 174 | tlli_type = gprs_tlli_type(tlli); |
| 175 | if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL) |
| 176 | return NULL; |
| 177 | |
| 178 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 179 | if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli || |
| 180 | gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) && |
| 181 | gprs_ra_id_equals(raid, &ctx->ra)) |
| 182 | return ctx; |
| 183 | } |
| 184 | |
| 185 | return NULL; |
| 186 | } |
| 187 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 188 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t p_tmsi) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 189 | { |
| 190 | struct sgsn_mm_ctx *ctx; |
| 191 | |
| 192 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 193 | if (p_tmsi == ctx->p_tmsi || |
| 194 | (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi)) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 195 | return ctx; |
| 196 | } |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 201 | { |
| 202 | struct sgsn_mm_ctx *ctx; |
| 203 | |
| 204 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 205 | if (!strcmp(imsi, ctx->imsi)) |
| 206 | return ctx; |
| 207 | } |
| 208 | return NULL; |
| 209 | |
| 210 | } |
| 211 | |
Alexander Couzens | 2b5fb8e | 2017-02-04 06:01:00 +0100 | [diff] [blame] | 212 | /* Allocate a new SGSN MM context for GERAN_Gb */ |
| 213 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_gb(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 214 | const struct gprs_ra_id *raid) |
| 215 | { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 216 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 217 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 218 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 219 | if (!ctx) |
| 220 | return NULL; |
| 221 | |
| 222 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 223 | ctx->ran_type = MM_CTX_T_GERAN_Gb; |
| 224 | ctx->gb.tlli = tlli; |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 225 | ctx->gmm_state = GMM_DEREGISTERED; |
Alexander Couzens | 1013550 | 2017-02-04 05:53:07 +0100 | [diff] [blame] | 226 | ctx->pmm_state = MM_IDLE; |
Jacob Erlbeck | bd0cf11 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 227 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 228 | ctx->ciph_algo = sgsn->cfg.cipher; |
Max | 549ebc7 | 2016-11-18 14:07:04 +0100 | [diff] [blame] | 229 | LOGMMCTXP(LOGL_DEBUG, ctx, "Allocated with %s cipher.\n", |
| 230 | get_value_string(gprs_cipher_names, ctx->ciph_algo)); |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 231 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | 6ffbaab | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 232 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 233 | |
| 234 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 235 | |
| 236 | return ctx; |
| 237 | } |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 238 | |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 239 | /* Allocate a new SGSN MM context */ |
| 240 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx) |
| 241 | { |
| 242 | struct sgsn_mm_ctx *ctx; |
| 243 | |
| 244 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
| 245 | if (!ctx) |
| 246 | return NULL; |
| 247 | |
| 248 | ctx->ran_type = MM_CTX_T_UTRAN_Iu; |
| 249 | ctx->iu.ue_ctx = uectx; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 250 | ctx->iu.ue_ctx->rab_assign_addr_enc = sgsn->cfg.iu.rab_assign_addr_enc; |
Daniel Willmann | 3ecfbbb | 2016-05-21 00:16:55 +0200 | [diff] [blame] | 251 | ctx->iu.new_key = 1; |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 252 | ctx->gmm_state = GMM_DEREGISTERED; |
Daniel Willmann | 5b2363e | 2016-05-21 00:01:21 +0200 | [diff] [blame] | 253 | ctx->pmm_state = PMM_DETACHED; |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 254 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
| 255 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, 0); |
| 256 | |
| 257 | /* Need to get RAID from IU conn */ |
| 258 | ctx->ra = ctx->iu.ue_ctx->ra_id; |
| 259 | |
| 260 | INIT_LLIST_HEAD(&ctx->pdp_list); |
| 261 | |
| 262 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 263 | |
| 264 | return ctx; |
| 265 | } |
| 266 | |
| 267 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 268 | /* this is a hard _free_ function, it doesn't clean up the PDP contexts |
| 269 | * in libgtp! */ |
Holger Hans Peter Freyther | b448dd8 | 2015-05-03 11:46:58 +0200 | [diff] [blame] | 270 | static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm) |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 271 | { |
| 272 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 273 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 274 | /* Unlink from global list of MM contexts */ |
| 275 | llist_del(&mm->list); |
| 276 | |
| 277 | /* Free all PDP contexts */ |
| 278 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) |
| 279 | sgsn_pdp_ctx_free(pdp); |
| 280 | |
| 281 | rate_ctr_group_free(mm->ctrg); |
| 282 | |
| 283 | talloc_free(mm); |
| 284 | } |
| 285 | |
| 286 | void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm) |
| 287 | { |
Daniel Willmann | 7ec8ca4 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 288 | struct gprs_llc_llme *llme = NULL; |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 289 | uint32_t tlli = mm->gb.tlli; |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 290 | struct sgsn_pdp_ctx *pdp, *pdp2; |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 291 | struct sgsn_signal_data sig_data; |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 292 | |
Daniel Willmann | 7ec8ca4 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 293 | if (mm->ran_type == MM_CTX_T_GERAN_Gb) |
| 294 | llme = mm->gb.llme; |
| 295 | else |
| 296 | OSMO_ASSERT(mm->gb.llme == NULL); |
| 297 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 298 | /* Forget about ongoing look-ups */ |
| 299 | if (mm->ggsn_lookup) { |
| 300 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 301 | "Cleaning mmctx with on-going query.\n"); |
| 302 | mm->ggsn_lookup->mmctx = NULL; |
| 303 | mm->ggsn_lookup = NULL; |
| 304 | } |
| 305 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 306 | /* delete all existing PDP contexts for this MS */ |
| 307 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) { |
| 308 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 309 | "Dropping PDP context for NSAPI=%u\n", pdp->nsapi); |
| 310 | sgsn_pdp_ctx_terminate(pdp); |
| 311 | } |
| 312 | |
Jacob Erlbeck | ae20b4b | 2014-10-20 16:05:55 +0200 | [diff] [blame] | 313 | if (osmo_timer_pending(&mm->timer)) { |
| 314 | LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T); |
| 315 | osmo_timer_del(&mm->timer); |
| 316 | } |
| 317 | |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 318 | memset(&sig_data, 0, sizeof(sig_data)); |
| 319 | sig_data.mm = mm; |
| 320 | osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data); |
| 321 | |
| 322 | |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 323 | /* Detach from subscriber which is possibly freed then */ |
| 324 | if (mm->subscr) { |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 325 | struct gprs_subscr *subscr = gprs_subscr_get(mm->subscr); |
Jacob Erlbeck | 3e4e58f | 2015-01-26 11:07:24 +0100 | [diff] [blame] | 326 | gprs_subscr_cleanup(subscr); |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 327 | gprs_subscr_put(subscr); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 330 | sgsn_mm_ctx_free(mm); |
| 331 | mm = NULL; |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 332 | |
Daniel Willmann | 7ec8ca4 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 333 | if (llme) { |
| 334 | /* TLLI unassignment, must be called after sgsn_mm_ctx_free */ |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 335 | gprs_llgmm_assign(llme, tlli, 0xffffffff); |
Daniel Willmann | 7ec8ca4 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 336 | } |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 337 | } |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 338 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 339 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 340 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 341 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 342 | uint8_t nsapi) |
| 343 | { |
| 344 | struct sgsn_pdp_ctx *pdp; |
| 345 | |
| 346 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 347 | if (pdp->nsapi == nsapi) |
| 348 | return pdp; |
| 349 | } |
| 350 | return NULL; |
| 351 | } |
| 352 | |
Harald Welte | 96df606 | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 353 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 354 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 355 | uint8_t tid) |
| 356 | { |
| 357 | struct sgsn_pdp_ctx *pdp; |
| 358 | |
| 359 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 360 | if (pdp->ti == tid) |
| 361 | return pdp; |
| 362 | } |
| 363 | return NULL; |
| 364 | } |
| 365 | |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 366 | /* you don't want to use this directly, call sgsn_create_pdp_ctx() */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 367 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 368 | uint8_t nsapi) |
| 369 | { |
| 370 | struct sgsn_pdp_ctx *pdp; |
| 371 | |
| 372 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 373 | if (pdp) |
| 374 | return NULL; |
| 375 | |
| 376 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 377 | if (!pdp) |
| 378 | return NULL; |
| 379 | |
| 380 | pdp->mm = mm; |
| 381 | pdp->nsapi = nsapi; |
Harald Welte | efbdee9 | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 382 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 383 | llist_add(&pdp->list, &mm->pdp_list); |
| 384 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 385 | |
| 386 | return pdp; |
| 387 | } |
| 388 | |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 389 | /* |
| 390 | * This function will not trigger any GSM DEACT PDP ACK messages, so you |
| 391 | * probably want to call sgsn_delete_pdp_ctx() instead if the connection |
| 392 | * isn't detached already. |
| 393 | */ |
| 394 | void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp) |
| 395 | { |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 396 | struct sgsn_signal_data sig_data; |
| 397 | |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 398 | OSMO_ASSERT(pdp->mm != NULL); |
| 399 | |
| 400 | /* There might still be pending callbacks in libgtp. So the parts of |
| 401 | * this object relevant to GTP need to remain intact in this case. */ |
| 402 | |
| 403 | LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n"); |
| 404 | |
Daniel Willmann | f9f4387 | 2016-05-20 22:36:23 +0200 | [diff] [blame] | 405 | if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) { |
| 406 | /* Force the deactivation of the SNDCP layer */ |
| 407 | sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi); |
| 408 | } |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 409 | |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 410 | memset(&sig_data, 0, sizeof(sig_data)); |
| 411 | sig_data.pdp = pdp; |
| 412 | osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data); |
| 413 | |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 414 | /* Detach from MM context */ |
| 415 | llist_del(&pdp->list); |
| 416 | pdp->mm = NULL; |
| 417 | |
| 418 | sgsn_delete_pdp_ctx(pdp); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Don't call this function directly unless you know what you are doing. |
| 423 | * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or |
| 424 | * implementation dependent abnormal ones sgsn_pdp_ctx_terminate. |
| 425 | */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 426 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 427 | { |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 428 | struct sgsn_signal_data sig_data; |
| 429 | |
| 430 | memset(&sig_data, 0, sizeof(sig_data)); |
| 431 | sig_data.pdp = pdp; |
| 432 | osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data); |
| 433 | |
Harald Welte | 376d5e5 | 2010-06-28 18:57:21 +0200 | [diff] [blame] | 434 | rate_ctr_group_free(pdp->ctrg); |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 435 | if (pdp->mm) |
| 436 | llist_del(&pdp->list); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 437 | llist_del(&pdp->g_list); |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 438 | |
| 439 | /* _if_ we still have a library handle, at least set it to NULL |
| 440 | * to avoid any dereferences of the now-deleted PDP context from |
| 441 | * sgsn_libgtp:cb_data_ind() */ |
| 442 | if (pdp->lib) { |
| 443 | struct pdp_t *lib = pdp->lib; |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 444 | LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still " |
Harald Welte | fdf453c | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 445 | "has a libgtp handle attached to it, this shouldn't " |
| 446 | "happen!\n"); |
| 447 | osmo_generate_backtrace(); |
| 448 | lib->priv = NULL; |
| 449 | } |
| 450 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 451 | if (pdp->destroy_ggsn) |
| 452 | sgsn_ggsn_ctx_free(pdp->ggsn); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 453 | talloc_free(pdp); |
| 454 | } |
| 455 | |
| 456 | /* GGSN contexts */ |
| 457 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 458 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 459 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 460 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 461 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 462 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 463 | if (!ggc) |
| 464 | return NULL; |
| 465 | |
| 466 | ggc->id = id; |
| 467 | ggc->gtp_version = 1; |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 468 | ggc->remote_restart_ctr = -1; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 469 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 470 | ggc->gsn = sgsn->gsn; |
Harald Welte | 119c2ba | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 471 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 472 | |
| 473 | return ggc; |
| 474 | } |
| 475 | |
Jacob Erlbeck | f345612 | 2015-02-03 19:53:15 +0100 | [diff] [blame] | 476 | void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc) |
| 477 | { |
| 478 | llist_del(&ggc->list); |
| 479 | talloc_free(ggc); |
| 480 | } |
| 481 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 482 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 483 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 484 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 485 | |
| 486 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 487 | if (id == ggc->id) |
| 488 | return ggc; |
| 489 | } |
| 490 | return NULL; |
| 491 | } |
| 492 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 493 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr) |
| 494 | { |
| 495 | struct sgsn_ggsn_ctx *ggc; |
| 496 | |
| 497 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 498 | if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr))) |
| 499 | return ggc; |
| 500 | } |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 505 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 506 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 507 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 508 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 509 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 510 | if (!ggc) |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 511 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 512 | return ggc; |
| 513 | } |
| 514 | |
| 515 | /* APN contexts */ |
| 516 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 517 | static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 518 | { |
| 519 | struct apn_ctx *actx; |
| 520 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 521 | actx = talloc_zero(tall_bsc_ctx, struct apn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 522 | if (!actx) |
| 523 | return NULL; |
| 524 | actx->name = talloc_strdup(actx, ap_name); |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 525 | actx->imsi_prefix = talloc_strdup(actx, imsi_prefix); |
| 526 | |
| 527 | llist_add_tail(&actx->list, &sgsn_apn_ctxts); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 528 | |
| 529 | return actx; |
| 530 | } |
| 531 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 532 | void sgsn_apn_ctx_free(struct apn_ctx *actx) |
| 533 | { |
| 534 | llist_del(&actx->list); |
| 535 | talloc_free(actx); |
| 536 | } |
| 537 | |
| 538 | struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi) |
| 539 | { |
| 540 | struct apn_ctx *actx; |
| 541 | struct apn_ctx *found_actx = NULL; |
| 542 | size_t imsi_prio = 0; |
| 543 | size_t name_prio = 0; |
| 544 | size_t name_req_len = strlen(name); |
| 545 | |
| 546 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 547 | size_t name_ref_len, imsi_ref_len; |
| 548 | const char *name_ref_start, *name_match_start; |
| 549 | |
| 550 | imsi_ref_len = strlen(actx->imsi_prefix); |
| 551 | if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0) |
| 552 | continue; |
| 553 | |
| 554 | if (imsi_ref_len < imsi_prio) |
| 555 | continue; |
| 556 | |
| 557 | /* IMSI matches */ |
| 558 | |
| 559 | name_ref_start = &actx->name[0]; |
| 560 | if (name_ref_start[0] == '*') { |
| 561 | /* Suffix match */ |
| 562 | name_ref_start += 1; |
| 563 | name_ref_len = strlen(name_ref_start); |
| 564 | if (name_ref_len > name_req_len) |
| 565 | continue; |
| 566 | } else { |
| 567 | name_ref_len = strlen(name_ref_start); |
| 568 | if (name_ref_len != name_req_len) |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | name_match_start = name + (name_req_len - name_ref_len); |
| 573 | if (strcasecmp(name_match_start, name_ref_start) != 0) |
| 574 | continue; |
| 575 | |
| 576 | /* IMSI and name match */ |
| 577 | |
| 578 | if (imsi_ref_len == imsi_prio && name_ref_len < name_prio) |
| 579 | /* Lower priority, skip */ |
| 580 | continue; |
| 581 | |
| 582 | imsi_prio = imsi_ref_len; |
| 583 | name_prio = name_ref_len; |
| 584 | found_actx = actx; |
| 585 | } |
| 586 | return found_actx; |
| 587 | } |
| 588 | |
| 589 | struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 590 | { |
| 591 | struct apn_ctx *actx; |
| 592 | |
| 593 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 594 | if (strcasecmp(name, actx->name) == 0 && |
| 595 | strcasecmp(imsi_prefix, actx->imsi_prefix) == 0) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 596 | return actx; |
| 597 | } |
| 598 | return NULL; |
| 599 | } |
| 600 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 601 | struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 602 | { |
| 603 | struct apn_ctx *actx; |
| 604 | |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 605 | actx = sgsn_apn_ctx_by_name(name, imsi_prefix); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 606 | if (!actx) |
Jacob Erlbeck | cb1db8b | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 607 | actx = sgsn_apn_ctx_alloc(name, imsi_prefix); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 608 | |
| 609 | return actx; |
| 610 | } |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 611 | |
| 612 | uint32_t sgsn_alloc_ptmsi(void) |
| 613 | { |
| 614 | struct sgsn_mm_ctx *mm; |
Alexander Couzens | 8a215c3 | 2017-02-03 23:22:18 +0100 | [diff] [blame] | 615 | uint32_t ptmsi = 0xdeadbeef; |
Jacob Erlbeck | d8a6553 | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 616 | int max_retries = 100; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 617 | |
| 618 | restart: |
Daniel Willmann | 044ce5f | 2015-10-12 19:36:33 +0200 | [diff] [blame] | 619 | if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1) |
| 620 | goto failed; |
| 621 | |
Jacob Erlbeck | d8a6553 | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 622 | /* Enforce that the 2 MSB are set without loosing the distance between |
| 623 | * identical values. Since rand() has no duplicate values within a |
| 624 | * period (because the size of the state is the same like the size of |
| 625 | * the random value), this leads to a distance of period/4 when the |
| 626 | * distribution of the 2 MSB is uniform. This approach fails with a |
| 627 | * probability of (3/4)^max_retries, only 1% of the approaches will |
| 628 | * need more than 16 numbers (even distribution assumed). |
| 629 | * |
| 630 | * Alternatively, a freeze list could be used if another PRNG is used |
| 631 | * or when this approach proves to be not sufficient. |
| 632 | */ |
| 633 | if (ptmsi >= 0xC0000000) { |
| 634 | if (!max_retries--) |
| 635 | goto failed; |
| 636 | goto restart; |
| 637 | } |
| 638 | ptmsi |= 0xC0000000; |
| 639 | |
| 640 | if (ptmsi == GSM_RESERVED_TMSI) { |
| 641 | if (!max_retries--) |
| 642 | goto failed; |
| 643 | goto restart; |
| 644 | } |
| 645 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 646 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 647 | if (mm->p_tmsi == ptmsi) { |
| 648 | if (!max_retries--) |
| 649 | goto failed; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 650 | goto restart; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 651 | } |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | return ptmsi; |
Jacob Erlbeck | 08fbeb8 | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 655 | |
| 656 | failed: |
| 657 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n"); |
| 658 | return GSM_RESERVED_TMSI; |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 659 | } |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 660 | |
| 661 | static void drop_one_pdp(struct sgsn_pdp_ctx *pdp) |
| 662 | { |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 663 | if (pdp->mm->gmm_state == GMM_REGISTERED_NORMAL) |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 664 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
| 665 | else { |
| 666 | /* FIXME: GPRS paging in case MS is SUSPENDED */ |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 667 | LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN " |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 668 | "recovery\n"); |
Harald Welte | 7b022ee | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 669 | /* FIXME: how to tell this to libgtp? */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 670 | sgsn_pdp_ctx_free(pdp); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /* High-level function to be called in case a GGSN has disappeared or |
Holger Hans Peter Freyther | 19e990d | 2014-10-27 10:24:37 +0100 | [diff] [blame] | 675 | * otherwise lost state (recovery procedure) */ |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 676 | int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn) |
| 677 | { |
| 678 | struct sgsn_mm_ctx *mm; |
| 679 | int num = 0; |
| 680 | |
| 681 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 682 | struct sgsn_pdp_ctx *pdp; |
| 683 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 684 | if (pdp->ggsn == ggsn) { |
| 685 | drop_one_pdp(pdp); |
| 686 | num++; |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | return num; |
| 692 | } |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 693 | |
Jacob Erlbeck | 555b2e5 | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 694 | void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 695 | { |
Jacob Erlbeck | 555b2e5 | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 696 | OSMO_ASSERT(mmctx != NULL); |
Jacob Erlbeck | c939196 | 2014-12-18 09:53:07 +0100 | [diff] [blame] | 697 | LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n"); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 698 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 699 | sgsn_auth_update(mmctx); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 700 | } |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 701 | |
Holger Hans Peter Freyther | 8cedded | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 702 | static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp) |
| 703 | { |
| 704 | tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len; |
| 705 | tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * The tlv_parsed tp parameter will be modified to insert a |
| 710 | * OSMO_IE_GSM_SUB_QOS in case the data is available in the |
| 711 | * PDP context handling. |
| 712 | */ |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 713 | struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx, |
| 714 | struct tlv_parsed *tp, |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 715 | enum gsm48_gsm_cause *gsm_cause, |
| 716 | char *out_apn_str) |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 717 | { |
| 718 | char req_apn_str[GSM_APN_LENGTH] = {0}; |
| 719 | const struct apn_ctx *apn_ctx = NULL; |
| 720 | const char *selected_apn_str = NULL; |
| 721 | struct sgsn_subscriber_pdp_data *pdp; |
| 722 | struct sgsn_ggsn_ctx *ggsn = NULL; |
| 723 | int allow_any_apn = 0; |
| 724 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 725 | out_apn_str[0] = '\0'; |
| 726 | |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 727 | if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) { |
| 728 | if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) { |
| 729 | LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n"); |
| 730 | *gsm_cause = GSM_CAUSE_INV_MAND_INFO; |
| 731 | return NULL; |
| 732 | } |
| 733 | |
| 734 | gprs_apn_to_str(req_apn_str, |
| 735 | TLVP_VAL(tp, GSM48_IE_GSM_APN), |
| 736 | TLVP_LEN(tp, GSM48_IE_GSM_APN)); |
| 737 | |
| 738 | if (strcmp(req_apn_str, "*") == 0) |
| 739 | req_apn_str[0] = 0; |
| 740 | } |
| 741 | |
Holger Hans Peter Freyther | 9270d99 | 2015-05-24 20:51:17 +0800 | [diff] [blame] | 742 | if (mmctx->subscr == NULL) |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 743 | allow_any_apn = 1; |
| 744 | |
| 745 | if (strlen(req_apn_str) == 0 && !allow_any_apn) { |
| 746 | /* No specific APN requested, check for an APN that is both |
| 747 | * granted and configured */ |
| 748 | |
| 749 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 750 | if (strcmp(pdp->apn_str, "*") == 0) |
| 751 | { |
| 752 | allow_any_apn = 1; |
| 753 | selected_apn_str = ""; |
Holger Hans Peter Freyther | 8cedded | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 754 | insert_qos(tp, pdp); |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 755 | continue; |
| 756 | } |
| 757 | if (!llist_empty(&sgsn_apn_ctxts)) { |
| 758 | apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi); |
| 759 | /* Not configured */ |
| 760 | if (apn_ctx == NULL) |
| 761 | continue; |
| 762 | } |
Holger Hans Peter Freyther | 8cedded | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 763 | insert_qos(tp, pdp); |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 764 | selected_apn_str = pdp->apn_str; |
| 765 | break; |
| 766 | } |
| 767 | } else if (!allow_any_apn) { |
| 768 | /* Check whether the given APN is granted */ |
| 769 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 770 | if (strcmp(pdp->apn_str, "*") == 0) { |
Holger Hans Peter Freyther | 8cedded | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 771 | insert_qos(tp, pdp); |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 772 | selected_apn_str = req_apn_str; |
| 773 | allow_any_apn = 1; |
| 774 | continue; |
| 775 | } |
| 776 | if (strcasecmp(pdp->apn_str, req_apn_str) == 0) { |
Holger Hans Peter Freyther | 8cedded | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 777 | insert_qos(tp, pdp); |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 778 | selected_apn_str = req_apn_str; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | } else if (strlen(req_apn_str) != 0) { |
| 783 | /* Any APN is allowed */ |
| 784 | selected_apn_str = req_apn_str; |
| 785 | } else { |
| 786 | /* Prefer the GGSN associated with the wildcard APN */ |
| 787 | selected_apn_str = ""; |
| 788 | } |
| 789 | |
| 790 | if (!allow_any_apn && selected_apn_str == NULL) { |
| 791 | /* Access not granted */ |
| 792 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 793 | "The requested APN '%s' is not allowed\n", |
| 794 | req_apn_str); |
| 795 | *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB; |
| 796 | return NULL; |
| 797 | } |
| 798 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 799 | /* copy the selected apn_str */ |
Holger Hans Peter Freyther | f2e114a | 2015-06-02 09:33:31 +0200 | [diff] [blame] | 800 | if (selected_apn_str) |
| 801 | strcpy(out_apn_str, selected_apn_str); |
| 802 | else |
| 803 | out_apn_str[0] = '\0'; |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 804 | |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 805 | if (apn_ctx == NULL && selected_apn_str) |
| 806 | apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi); |
| 807 | |
| 808 | if (apn_ctx != NULL) { |
| 809 | ggsn = apn_ctx->ggsn; |
| 810 | } else if (llist_empty(&sgsn_apn_ctxts)) { |
| 811 | /* No configuration -> use GGSN 0 */ |
| 812 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 813 | } else if (allow_any_apn && |
| 814 | (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) { |
| 815 | /* No APN given and no default configuration -> Use GGSN 0 */ |
| 816 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 817 | } else { |
| 818 | /* No matching configuration found */ |
| 819 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 820 | "The selected APN '%s' has not been configured\n", |
| 821 | selected_apn_str); |
| 822 | *gsm_cause = GSM_CAUSE_MISSING_APN; |
| 823 | return NULL; |
| 824 | } |
| 825 | |
Holger Hans Peter Freyther | 08bb84b | 2015-05-25 14:35:10 +0800 | [diff] [blame] | 826 | if (!ggsn) { |
| 827 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 828 | "No static GGSN configured. Selected APN '%s'\n", |
| 829 | selected_apn_str); |
| 830 | return NULL; |
| 831 | } |
| 832 | |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 833 | LOGMMCTXP(LOGL_INFO, mmctx, |
| 834 | "Found GGSN %d for APN '%s' (requested '%s')\n", |
| 835 | ggsn->id, selected_apn_str ? selected_apn_str : "---", |
| 836 | req_apn_str); |
| 837 | |
| 838 | return ggsn; |
| 839 | } |
| 840 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 841 | static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme) |
| 842 | { |
| 843 | struct sgsn_mm_ctx *mmctx = NULL; |
| 844 | |
| 845 | llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) { |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 846 | if (llme == mmctx->gb.llme) { |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 847 | gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE); |
| 848 | return; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* No MM context found */ |
| 853 | LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n", |
| 854 | llme->tlli); |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 855 | gprs_llgmm_unassign(llme); |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static void sgsn_llme_check_cb(void *data_) |
| 859 | { |
| 860 | struct gprs_llc_llme *llme, *llme_tmp; |
| 861 | struct timespec now_tp; |
| 862 | time_t now, age; |
| 863 | time_t max_age = gprs_max_time_to_idle(); |
| 864 | |
| 865 | int rc; |
| 866 | |
| 867 | rc = clock_gettime(CLOCK_MONOTONIC, &now_tp); |
| 868 | OSMO_ASSERT(rc >= 0); |
| 869 | now = now_tp.tv_sec; |
| 870 | |
| 871 | LOGP(DGPRS, LOGL_DEBUG, |
| 872 | "Checking for inactive LLMEs, time = %u\n", (unsigned)now); |
| 873 | |
| 874 | llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) { |
| 875 | if (llme->age_timestamp == GPRS_LLME_RESET_AGE) |
| 876 | llme->age_timestamp = now; |
| 877 | |
| 878 | age = now - llme->age_timestamp; |
| 879 | |
| 880 | if (age > max_age || age < 0) { |
| 881 | LOGP(DGPRS, LOGL_INFO, |
| 882 | "Inactivity timeout for TLLI 0x%08x, age %d\n", |
| 883 | llme->tlli, (int)age); |
| 884 | sgsn_llme_cleanup_free(llme); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 889 | } |
| 890 | |
| 891 | void sgsn_inst_init() |
| 892 | { |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 893 | osmo_timer_setup(&sgsn->llme_timer, sgsn_llme_check_cb, NULL); |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 894 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 895 | } |
| 896 | |