Harald Welte | 75bb820 | 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 | 0e3e88e | 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 | 75bb820 | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 0e3e88e | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 23 | |
Pablo Neira Ayuso | dd5fff4 | 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 | d31596b | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 28 | #include <osmocom/core/stats.h> |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 29 | #include <osmocom/core/backtrace.h> |
Harald Welte | cfb6b28 | 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 | 35ade5e | 2016-04-20 17:11:43 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Harald Welte | cfb6b28 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 33 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 34 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 36 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 37 | #include <openbsc/sgsn.h> |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 38 | #include <openbsc/gprs_gmm.h> |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 39 | #include <openbsc/gprs_utils.h> |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 40 | #include <openbsc/signal.h> |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 41 | #include "openbsc/gprs_llc.h" |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 42 | #include <openbsc/iu.h> |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 43 | |
Neels Hofmeyr | aef31f7 | 2016-05-21 00:44:50 +0200 | [diff] [blame] | 44 | #include <pdp.h> |
| 45 | |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 46 | #include <time.h> |
| 47 | |
Daniel Willmann | d7f6cdf | 2015-10-12 19:36:33 +0200 | [diff] [blame] | 48 | #include <openssl/rand.h> |
| 49 | |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 50 | #define GPRS_LLME_CHECK_TICK 30 |
| 51 | |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 52 | extern struct sgsn_instance *sgsn; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 53 | |
Harald Welte | c1f6bfe | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 58 | |
Harald Welte | 8a035af | 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 | d31596b | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 78 | .class_id = OSMO_STATS_CLASS_SUBSCRIBER, |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Harald Welte | 0fe506b | 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 | d31596b | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 93 | .class_id = OSMO_STATS_CLASS_SUBSCRIBER, |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
Alexander Couzens | 32fdba0 | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 96 | static const struct rate_ctr_desc sgsn_ctr_description[] = { |
Alexander Couzens | b7d11e3 | 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 | 32fdba0 | 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 | 945f608 | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 148 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 149 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 75bb820 | 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 | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 155 | if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) && |
Jacob Erlbeck | d6466c9 | 2016-01-04 18:43:34 +0100 | [diff] [blame] | 156 | gprs_ra_id_equals(raid, &ctx->ra)) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 157 | return ctx; |
| 158 | } |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 159 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 160 | return NULL; |
| 161 | } |
| 162 | |
Jacob Erlbeck | 556aa91 | 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 | d85d9a9 | 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 | 75bb820 | 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 | 7e01d8e | 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 | 75bb820 | 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 | |
| 212 | /* Allocate a new SGSN MM context */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 213 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 214 | const struct gprs_ra_id *raid) |
| 215 | { |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 216 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 217 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 218 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 75bb820 | 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 | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 223 | ctx->ran_type = MM_CTX_T_GERAN_Gb; |
| 224 | ctx->gb.tlli = tlli; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 225 | ctx->mm_state = GMM_DEREGISTERED; |
Jacob Erlbeck | 4d12767 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 226 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
Max | 6d99958 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 227 | ctx->ciph_algo = sgsn->cfg.cipher; |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 228 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | ded83ec | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 229 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 230 | |
| 231 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 232 | |
| 233 | return ctx; |
| 234 | } |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 235 | |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 236 | /* Allocate a new SGSN MM context */ |
| 237 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx) |
| 238 | { |
| 239 | struct sgsn_mm_ctx *ctx; |
| 240 | |
| 241 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
| 242 | if (!ctx) |
| 243 | return NULL; |
| 244 | |
| 245 | ctx->ran_type = MM_CTX_T_UTRAN_Iu; |
| 246 | ctx->iu.ue_ctx = uectx; |
Daniel Willmann | 522fee8 | 2016-05-21 00:16:55 +0200 | [diff] [blame] | 247 | ctx->iu.new_key = 1; |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 248 | ctx->mm_state = GMM_DEREGISTERED; |
Daniel Willmann | 7d62c88 | 2016-05-21 00:01:21 +0200 | [diff] [blame] | 249 | ctx->pmm_state = PMM_DETACHED; |
Daniel Willmann | 945f608 | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 250 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
| 251 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, 0); |
| 252 | |
| 253 | /* Need to get RAID from IU conn */ |
| 254 | ctx->ra = ctx->iu.ue_ctx->ra_id; |
| 255 | |
| 256 | INIT_LLIST_HEAD(&ctx->pdp_list); |
| 257 | |
| 258 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 259 | |
| 260 | return ctx; |
| 261 | } |
| 262 | |
| 263 | |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 264 | /* this is a hard _free_ function, it doesn't clean up the PDP contexts |
| 265 | * in libgtp! */ |
Holger Hans Peter Freyther | ea2825a | 2015-05-03 11:46:58 +0200 | [diff] [blame] | 266 | static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm) |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 267 | { |
| 268 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 269 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 270 | /* Unlink from global list of MM contexts */ |
| 271 | llist_del(&mm->list); |
| 272 | |
| 273 | /* Free all PDP contexts */ |
| 274 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) |
| 275 | sgsn_pdp_ctx_free(pdp); |
| 276 | |
| 277 | rate_ctr_group_free(mm->ctrg); |
| 278 | |
| 279 | talloc_free(mm); |
| 280 | } |
| 281 | |
| 282 | void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm) |
| 283 | { |
Daniel Willmann | 8ddce12 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 284 | struct gprs_llc_llme *llme = NULL; |
Harald Welte | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 285 | uint32_t tlli = mm->gb.tlli; |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 286 | struct sgsn_pdp_ctx *pdp, *pdp2; |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 287 | struct sgsn_signal_data sig_data; |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 288 | |
Daniel Willmann | 8ddce12 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 289 | if (mm->ran_type == MM_CTX_T_GERAN_Gb) |
| 290 | llme = mm->gb.llme; |
| 291 | else |
| 292 | OSMO_ASSERT(mm->gb.llme == NULL); |
| 293 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 294 | /* Forget about ongoing look-ups */ |
| 295 | if (mm->ggsn_lookup) { |
| 296 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 297 | "Cleaning mmctx with on-going query.\n"); |
| 298 | mm->ggsn_lookup->mmctx = NULL; |
| 299 | mm->ggsn_lookup = NULL; |
| 300 | } |
| 301 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 302 | /* delete all existing PDP contexts for this MS */ |
| 303 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) { |
| 304 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 305 | "Dropping PDP context for NSAPI=%u\n", pdp->nsapi); |
| 306 | sgsn_pdp_ctx_terminate(pdp); |
| 307 | } |
| 308 | |
Jacob Erlbeck | 11493a2 | 2014-10-20 16:05:55 +0200 | [diff] [blame] | 309 | if (osmo_timer_pending(&mm->timer)) { |
| 310 | LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T); |
| 311 | osmo_timer_del(&mm->timer); |
| 312 | } |
| 313 | |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 314 | memset(&sig_data, 0, sizeof(sig_data)); |
| 315 | sig_data.mm = mm; |
| 316 | osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data); |
| 317 | |
| 318 | |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 319 | /* Detach from subscriber which is possibly freed then */ |
| 320 | if (mm->subscr) { |
Jacob Erlbeck | 480e820 | 2015-01-26 13:41:11 +0100 | [diff] [blame] | 321 | struct gsm_subscriber *subscr = subscr_get(mm->subscr); |
Jacob Erlbeck | e71ab2f | 2015-01-26 11:07:24 +0100 | [diff] [blame] | 322 | gprs_subscr_cleanup(subscr); |
Jacob Erlbeck | 7a7d881 | 2015-01-23 13:52:55 +0100 | [diff] [blame] | 323 | subscr_put(subscr); |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 326 | sgsn_mm_ctx_free(mm); |
| 327 | mm = NULL; |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 328 | |
Daniel Willmann | 8ddce12 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 329 | if (llme) { |
| 330 | /* TLLI unassignment, must be called after sgsn_mm_ctx_free */ |
Max | eb64e3f | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 331 | gprs_llgmm_assign(llme, tlli, 0xffffffff); |
Daniel Willmann | 8ddce12 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 332 | } |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 333 | } |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 334 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 335 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 336 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 337 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 338 | uint8_t nsapi) |
| 339 | { |
| 340 | struct sgsn_pdp_ctx *pdp; |
| 341 | |
| 342 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 343 | if (pdp->nsapi == nsapi) |
| 344 | return pdp; |
| 345 | } |
| 346 | return NULL; |
| 347 | } |
| 348 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 349 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 350 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 351 | uint8_t tid) |
| 352 | { |
| 353 | struct sgsn_pdp_ctx *pdp; |
| 354 | |
| 355 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 356 | if (pdp->ti == tid) |
| 357 | return pdp; |
| 358 | } |
| 359 | return NULL; |
| 360 | } |
| 361 | |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 362 | /* you don't want to use this directly, call sgsn_create_pdp_ctx() */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 363 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 364 | uint8_t nsapi) |
| 365 | { |
| 366 | struct sgsn_pdp_ctx *pdp; |
| 367 | |
| 368 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 369 | if (pdp) |
| 370 | return NULL; |
| 371 | |
| 372 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 373 | if (!pdp) |
| 374 | return NULL; |
| 375 | |
| 376 | pdp->mm = mm; |
| 377 | pdp->nsapi = nsapi; |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 378 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 379 | llist_add(&pdp->list, &mm->pdp_list); |
| 380 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 381 | |
| 382 | return pdp; |
| 383 | } |
| 384 | |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 385 | /* |
| 386 | * This function will not trigger any GSM DEACT PDP ACK messages, so you |
| 387 | * probably want to call sgsn_delete_pdp_ctx() instead if the connection |
| 388 | * isn't detached already. |
| 389 | */ |
| 390 | void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp) |
| 391 | { |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 392 | struct sgsn_signal_data sig_data; |
| 393 | |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 394 | OSMO_ASSERT(pdp->mm != NULL); |
| 395 | |
| 396 | /* There might still be pending callbacks in libgtp. So the parts of |
| 397 | * this object relevant to GTP need to remain intact in this case. */ |
| 398 | |
| 399 | LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n"); |
| 400 | |
Daniel Willmann | 711adc2 | 2016-05-20 22:36:23 +0200 | [diff] [blame] | 401 | if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) { |
| 402 | /* Force the deactivation of the SNDCP layer */ |
| 403 | sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi); |
| 404 | } |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 405 | |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 406 | memset(&sig_data, 0, sizeof(sig_data)); |
| 407 | sig_data.pdp = pdp; |
| 408 | osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data); |
| 409 | |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 410 | /* Detach from MM context */ |
| 411 | llist_del(&pdp->list); |
| 412 | pdp->mm = NULL; |
| 413 | |
| 414 | sgsn_delete_pdp_ctx(pdp); |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Don't call this function directly unless you know what you are doing. |
| 419 | * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or |
| 420 | * implementation dependent abnormal ones sgsn_pdp_ctx_terminate. |
| 421 | */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 422 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 423 | { |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 424 | struct sgsn_signal_data sig_data; |
| 425 | |
| 426 | memset(&sig_data, 0, sizeof(sig_data)); |
| 427 | sig_data.pdp = pdp; |
| 428 | osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data); |
| 429 | |
Harald Welte | 17a40a6 | 2010-06-28 18:57:21 +0200 | [diff] [blame] | 430 | rate_ctr_group_free(pdp->ctrg); |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 431 | if (pdp->mm) |
| 432 | llist_del(&pdp->list); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 433 | llist_del(&pdp->g_list); |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 434 | |
| 435 | /* _if_ we still have a library handle, at least set it to NULL |
| 436 | * to avoid any dereferences of the now-deleted PDP context from |
| 437 | * sgsn_libgtp:cb_data_ind() */ |
| 438 | if (pdp->lib) { |
| 439 | struct pdp_t *lib = pdp->lib; |
Daniel Willmann | c9ac973 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 440 | LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still " |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 441 | "has a libgtp handle attached to it, this shouldn't " |
| 442 | "happen!\n"); |
| 443 | osmo_generate_backtrace(); |
| 444 | lib->priv = NULL; |
| 445 | } |
| 446 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 447 | if (pdp->destroy_ggsn) |
| 448 | sgsn_ggsn_ctx_free(pdp->ggsn); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 449 | talloc_free(pdp); |
| 450 | } |
| 451 | |
| 452 | /* GGSN contexts */ |
| 453 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 454 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 455 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 456 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 457 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 458 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 459 | if (!ggc) |
| 460 | return NULL; |
| 461 | |
| 462 | ggc->id = id; |
| 463 | ggc->gtp_version = 1; |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 464 | ggc->remote_restart_ctr = -1; |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 465 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 466 | ggc->gsn = sgsn->gsn; |
Harald Welte | e58eee5 | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 467 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 468 | |
| 469 | return ggc; |
| 470 | } |
| 471 | |
Jacob Erlbeck | e3c935d | 2015-02-03 19:53:15 +0100 | [diff] [blame] | 472 | void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc) |
| 473 | { |
| 474 | llist_del(&ggc->list); |
| 475 | talloc_free(ggc); |
| 476 | } |
| 477 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 478 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 479 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 480 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 481 | |
| 482 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 483 | if (id == ggc->id) |
| 484 | return ggc; |
| 485 | } |
| 486 | return NULL; |
| 487 | } |
| 488 | |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 489 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr) |
| 490 | { |
| 491 | struct sgsn_ggsn_ctx *ggc; |
| 492 | |
| 493 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 494 | if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr))) |
| 495 | return ggc; |
| 496 | } |
| 497 | return NULL; |
| 498 | } |
| 499 | |
| 500 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 501 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 502 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 503 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 504 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 505 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 506 | if (!ggc) |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 507 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 508 | return ggc; |
| 509 | } |
| 510 | |
| 511 | /* APN contexts */ |
| 512 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 513 | static struct apn_ctx *sgsn_apn_ctx_alloc(const char *ap_name, const char *imsi_prefix) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 514 | { |
| 515 | struct apn_ctx *actx; |
| 516 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 517 | actx = talloc_zero(tall_bsc_ctx, struct apn_ctx); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 518 | if (!actx) |
| 519 | return NULL; |
| 520 | actx->name = talloc_strdup(actx, ap_name); |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 521 | actx->imsi_prefix = talloc_strdup(actx, imsi_prefix); |
| 522 | |
| 523 | llist_add_tail(&actx->list, &sgsn_apn_ctxts); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 524 | |
| 525 | return actx; |
| 526 | } |
| 527 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 528 | void sgsn_apn_ctx_free(struct apn_ctx *actx) |
| 529 | { |
| 530 | llist_del(&actx->list); |
| 531 | talloc_free(actx); |
| 532 | } |
| 533 | |
| 534 | struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi) |
| 535 | { |
| 536 | struct apn_ctx *actx; |
| 537 | struct apn_ctx *found_actx = NULL; |
| 538 | size_t imsi_prio = 0; |
| 539 | size_t name_prio = 0; |
| 540 | size_t name_req_len = strlen(name); |
| 541 | |
| 542 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 543 | size_t name_ref_len, imsi_ref_len; |
| 544 | const char *name_ref_start, *name_match_start; |
| 545 | |
| 546 | imsi_ref_len = strlen(actx->imsi_prefix); |
| 547 | if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0) |
| 548 | continue; |
| 549 | |
| 550 | if (imsi_ref_len < imsi_prio) |
| 551 | continue; |
| 552 | |
| 553 | /* IMSI matches */ |
| 554 | |
| 555 | name_ref_start = &actx->name[0]; |
| 556 | if (name_ref_start[0] == '*') { |
| 557 | /* Suffix match */ |
| 558 | name_ref_start += 1; |
| 559 | name_ref_len = strlen(name_ref_start); |
| 560 | if (name_ref_len > name_req_len) |
| 561 | continue; |
| 562 | } else { |
| 563 | name_ref_len = strlen(name_ref_start); |
| 564 | if (name_ref_len != name_req_len) |
| 565 | continue; |
| 566 | } |
| 567 | |
| 568 | name_match_start = name + (name_req_len - name_ref_len); |
| 569 | if (strcasecmp(name_match_start, name_ref_start) != 0) |
| 570 | continue; |
| 571 | |
| 572 | /* IMSI and name match */ |
| 573 | |
| 574 | if (imsi_ref_len == imsi_prio && name_ref_len < name_prio) |
| 575 | /* Lower priority, skip */ |
| 576 | continue; |
| 577 | |
| 578 | imsi_prio = imsi_ref_len; |
| 579 | name_prio = name_ref_len; |
| 580 | found_actx = actx; |
| 581 | } |
| 582 | return found_actx; |
| 583 | } |
| 584 | |
| 585 | struct apn_ctx *sgsn_apn_ctx_by_name(const char *name, const char *imsi_prefix) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 586 | { |
| 587 | struct apn_ctx *actx; |
| 588 | |
| 589 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 590 | if (strcasecmp(name, actx->name) == 0 && |
| 591 | strcasecmp(imsi_prefix, actx->imsi_prefix) == 0) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 592 | return actx; |
| 593 | } |
| 594 | return NULL; |
| 595 | } |
| 596 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 597 | struct apn_ctx *sgsn_apn_ctx_find_alloc(const char *name, const char *imsi_prefix) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 598 | { |
| 599 | struct apn_ctx *actx; |
| 600 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 601 | actx = sgsn_apn_ctx_by_name(name, imsi_prefix); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 602 | if (!actx) |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 603 | actx = sgsn_apn_ctx_alloc(name, imsi_prefix); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 604 | |
| 605 | return actx; |
| 606 | } |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 607 | |
| 608 | uint32_t sgsn_alloc_ptmsi(void) |
| 609 | { |
| 610 | struct sgsn_mm_ctx *mm; |
| 611 | uint32_t ptmsi; |
Jacob Erlbeck | e3aeb1e | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 612 | int max_retries = 100; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 613 | |
| 614 | restart: |
Daniel Willmann | d7f6cdf | 2015-10-12 19:36:33 +0200 | [diff] [blame] | 615 | if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1) |
| 616 | goto failed; |
| 617 | |
Jacob Erlbeck | e3aeb1e | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 618 | /* Enforce that the 2 MSB are set without loosing the distance between |
| 619 | * identical values. Since rand() has no duplicate values within a |
| 620 | * period (because the size of the state is the same like the size of |
| 621 | * the random value), this leads to a distance of period/4 when the |
| 622 | * distribution of the 2 MSB is uniform. This approach fails with a |
| 623 | * probability of (3/4)^max_retries, only 1% of the approaches will |
| 624 | * need more than 16 numbers (even distribution assumed). |
| 625 | * |
| 626 | * Alternatively, a freeze list could be used if another PRNG is used |
| 627 | * or when this approach proves to be not sufficient. |
| 628 | */ |
| 629 | if (ptmsi >= 0xC0000000) { |
| 630 | if (!max_retries--) |
| 631 | goto failed; |
| 632 | goto restart; |
| 633 | } |
| 634 | ptmsi |= 0xC0000000; |
| 635 | |
| 636 | if (ptmsi == GSM_RESERVED_TMSI) { |
| 637 | if (!max_retries--) |
| 638 | goto failed; |
| 639 | goto restart; |
| 640 | } |
| 641 | |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 642 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 643 | if (mm->p_tmsi == ptmsi) { |
| 644 | if (!max_retries--) |
| 645 | goto failed; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 646 | goto restart; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 647 | } |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | return ptmsi; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 651 | |
| 652 | failed: |
| 653 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n"); |
| 654 | return GSM_RESERVED_TMSI; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 655 | } |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 656 | |
| 657 | static void drop_one_pdp(struct sgsn_pdp_ctx *pdp) |
| 658 | { |
| 659 | if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL) |
| 660 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
| 661 | else { |
| 662 | /* FIXME: GPRS paging in case MS is SUSPENDED */ |
Daniel Willmann | c9ac973 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 663 | LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN " |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 664 | "recovery\n"); |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 665 | /* FIXME: how to tell this to libgtp? */ |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 666 | sgsn_pdp_ctx_free(pdp); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | /* High-level function to be called in case a GGSN has disappeared or |
Holger Hans Peter Freyther | edcefd5 | 2014-10-27 10:24:37 +0100 | [diff] [blame] | 671 | * otherwise lost state (recovery procedure) */ |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 672 | int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn) |
| 673 | { |
| 674 | struct sgsn_mm_ctx *mm; |
| 675 | int num = 0; |
| 676 | |
| 677 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 678 | struct sgsn_pdp_ctx *pdp; |
| 679 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 680 | if (pdp->ggsn == ggsn) { |
| 681 | drop_one_pdp(pdp); |
| 682 | num++; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return num; |
| 688 | } |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 689 | |
Jacob Erlbeck | 428f1ec | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 690 | void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 691 | { |
Jacob Erlbeck | 428f1ec | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 692 | OSMO_ASSERT(mmctx != NULL); |
Jacob Erlbeck | 0939c0f | 2014-12-18 09:53:07 +0100 | [diff] [blame] | 693 | LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n"); |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 694 | |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 695 | sgsn_auth_update(mmctx); |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 696 | } |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 697 | |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 698 | static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp) |
| 699 | { |
| 700 | tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len; |
| 701 | tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * The tlv_parsed tp parameter will be modified to insert a |
| 706 | * OSMO_IE_GSM_SUB_QOS in case the data is available in the |
| 707 | * PDP context handling. |
| 708 | */ |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 709 | struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx, |
| 710 | struct tlv_parsed *tp, |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 711 | enum gsm48_gsm_cause *gsm_cause, |
| 712 | char *out_apn_str) |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 713 | { |
| 714 | char req_apn_str[GSM_APN_LENGTH] = {0}; |
| 715 | const struct apn_ctx *apn_ctx = NULL; |
| 716 | const char *selected_apn_str = NULL; |
| 717 | struct sgsn_subscriber_pdp_data *pdp; |
| 718 | struct sgsn_ggsn_ctx *ggsn = NULL; |
| 719 | int allow_any_apn = 0; |
| 720 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 721 | out_apn_str[0] = '\0'; |
| 722 | |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 723 | if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) { |
| 724 | if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) { |
| 725 | LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n"); |
| 726 | *gsm_cause = GSM_CAUSE_INV_MAND_INFO; |
| 727 | return NULL; |
| 728 | } |
| 729 | |
| 730 | gprs_apn_to_str(req_apn_str, |
| 731 | TLVP_VAL(tp, GSM48_IE_GSM_APN), |
| 732 | TLVP_LEN(tp, GSM48_IE_GSM_APN)); |
| 733 | |
| 734 | if (strcmp(req_apn_str, "*") == 0) |
| 735 | req_apn_str[0] = 0; |
| 736 | } |
| 737 | |
Holger Hans Peter Freyther | ab9422e | 2015-05-24 20:51:17 +0800 | [diff] [blame] | 738 | if (mmctx->subscr == NULL) |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 739 | allow_any_apn = 1; |
| 740 | |
| 741 | if (strlen(req_apn_str) == 0 && !allow_any_apn) { |
| 742 | /* No specific APN requested, check for an APN that is both |
| 743 | * granted and configured */ |
| 744 | |
| 745 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 746 | if (strcmp(pdp->apn_str, "*") == 0) |
| 747 | { |
| 748 | allow_any_apn = 1; |
| 749 | selected_apn_str = ""; |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 750 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 751 | continue; |
| 752 | } |
| 753 | if (!llist_empty(&sgsn_apn_ctxts)) { |
| 754 | apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi); |
| 755 | /* Not configured */ |
| 756 | if (apn_ctx == NULL) |
| 757 | continue; |
| 758 | } |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 759 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 760 | selected_apn_str = pdp->apn_str; |
| 761 | break; |
| 762 | } |
| 763 | } else if (!allow_any_apn) { |
| 764 | /* Check whether the given APN is granted */ |
| 765 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 766 | if (strcmp(pdp->apn_str, "*") == 0) { |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 767 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 768 | selected_apn_str = req_apn_str; |
| 769 | allow_any_apn = 1; |
| 770 | continue; |
| 771 | } |
| 772 | if (strcasecmp(pdp->apn_str, req_apn_str) == 0) { |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 773 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 774 | selected_apn_str = req_apn_str; |
| 775 | break; |
| 776 | } |
| 777 | } |
| 778 | } else if (strlen(req_apn_str) != 0) { |
| 779 | /* Any APN is allowed */ |
| 780 | selected_apn_str = req_apn_str; |
| 781 | } else { |
| 782 | /* Prefer the GGSN associated with the wildcard APN */ |
| 783 | selected_apn_str = ""; |
| 784 | } |
| 785 | |
| 786 | if (!allow_any_apn && selected_apn_str == NULL) { |
| 787 | /* Access not granted */ |
| 788 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 789 | "The requested APN '%s' is not allowed\n", |
| 790 | req_apn_str); |
| 791 | *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB; |
| 792 | return NULL; |
| 793 | } |
| 794 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 795 | /* copy the selected apn_str */ |
Holger Hans Peter Freyther | 3332d0c | 2015-06-02 09:33:31 +0200 | [diff] [blame] | 796 | if (selected_apn_str) |
| 797 | strcpy(out_apn_str, selected_apn_str); |
| 798 | else |
| 799 | out_apn_str[0] = '\0'; |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 800 | |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 801 | if (apn_ctx == NULL && selected_apn_str) |
| 802 | apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi); |
| 803 | |
| 804 | if (apn_ctx != NULL) { |
| 805 | ggsn = apn_ctx->ggsn; |
| 806 | } else if (llist_empty(&sgsn_apn_ctxts)) { |
| 807 | /* No configuration -> use GGSN 0 */ |
| 808 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 809 | } else if (allow_any_apn && |
| 810 | (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) { |
| 811 | /* No APN given and no default configuration -> Use GGSN 0 */ |
| 812 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 813 | } else { |
| 814 | /* No matching configuration found */ |
| 815 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 816 | "The selected APN '%s' has not been configured\n", |
| 817 | selected_apn_str); |
| 818 | *gsm_cause = GSM_CAUSE_MISSING_APN; |
| 819 | return NULL; |
| 820 | } |
| 821 | |
Holger Hans Peter Freyther | eaa5851 | 2015-05-25 14:35:10 +0800 | [diff] [blame] | 822 | if (!ggsn) { |
| 823 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 824 | "No static GGSN configured. Selected APN '%s'\n", |
| 825 | selected_apn_str); |
| 826 | return NULL; |
| 827 | } |
| 828 | |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 829 | LOGMMCTXP(LOGL_INFO, mmctx, |
| 830 | "Found GGSN %d for APN '%s' (requested '%s')\n", |
| 831 | ggsn->id, selected_apn_str ? selected_apn_str : "---", |
| 832 | req_apn_str); |
| 833 | |
| 834 | return ggsn; |
| 835 | } |
| 836 | |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 837 | static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme) |
| 838 | { |
| 839 | struct sgsn_mm_ctx *mmctx = NULL; |
| 840 | |
| 841 | llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 842 | if (llme == mmctx->gb.llme) { |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 843 | gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE); |
| 844 | return; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* No MM context found */ |
| 849 | LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n", |
| 850 | llme->tlli); |
Max | a86f5c0 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 851 | gprs_llgmm_unassign(llme); |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static void sgsn_llme_check_cb(void *data_) |
| 855 | { |
| 856 | struct gprs_llc_llme *llme, *llme_tmp; |
| 857 | struct timespec now_tp; |
| 858 | time_t now, age; |
| 859 | time_t max_age = gprs_max_time_to_idle(); |
| 860 | |
| 861 | int rc; |
| 862 | |
| 863 | rc = clock_gettime(CLOCK_MONOTONIC, &now_tp); |
| 864 | OSMO_ASSERT(rc >= 0); |
| 865 | now = now_tp.tv_sec; |
| 866 | |
| 867 | LOGP(DGPRS, LOGL_DEBUG, |
| 868 | "Checking for inactive LLMEs, time = %u\n", (unsigned)now); |
| 869 | |
| 870 | llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) { |
| 871 | if (llme->age_timestamp == GPRS_LLME_RESET_AGE) |
| 872 | llme->age_timestamp = now; |
| 873 | |
| 874 | age = now - llme->age_timestamp; |
| 875 | |
| 876 | if (age > max_age || age < 0) { |
| 877 | LOGP(DGPRS, LOGL_INFO, |
| 878 | "Inactivity timeout for TLLI 0x%08x, age %d\n", |
| 879 | llme->tlli, (int)age); |
| 880 | sgsn_llme_cleanup_free(llme); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 885 | } |
| 886 | |
| 887 | void sgsn_inst_init() |
| 888 | { |
| 889 | sgsn->llme_timer.cb = sgsn_llme_check_cb; |
| 890 | sgsn->llme_timer.data = NULL; |
| 891 | |
| 892 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 893 | } |
| 894 | |