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