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" |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 42 | |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 43 | #include <time.h> |
| 44 | |
Daniel Willmann | d7f6cdf | 2015-10-12 19:36:33 +0200 | [diff] [blame] | 45 | #include <openssl/rand.h> |
| 46 | |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 47 | #define GPRS_LLME_CHECK_TICK 30 |
| 48 | |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 49 | extern struct sgsn_instance *sgsn; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 50 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 51 | LLIST_HEAD(sgsn_mm_ctxts); |
| 52 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 53 | LLIST_HEAD(sgsn_apn_ctxts); |
| 54 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 55 | |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 56 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 57 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 58 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 59 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 60 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 61 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 62 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 63 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 64 | { "suspend", "SUSPEND Count " }, |
| 65 | { "paging.ps", "Paging Packet Switched " }, |
| 66 | { "paging.cs", "Paging Circuit Switched " }, |
| 67 | { "ra_update", "Routing Area Update " }, |
| 68 | }; |
| 69 | |
| 70 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 71 | .group_name_prefix = "sgsn.mmctx", |
| 72 | .group_description = "SGSN MM Context Statistics", |
| 73 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 74 | .ctr_desc = mmctx_ctr_description, |
Jacob Erlbeck | d31596b | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 75 | .class_id = OSMO_STATS_CLASS_SUBSCRIBER, |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 78 | static const struct rate_ctr_desc pdpctx_ctr_description[] = { |
| 79 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 80 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 81 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 82 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 83 | }; |
| 84 | |
| 85 | static const struct rate_ctr_group_desc pdpctx_ctrg_desc = { |
| 86 | .group_name_prefix = "sgsn.pdpctx", |
| 87 | .group_description = "SGSN PDP Context Statistics", |
| 88 | .num_ctr = ARRAY_SIZE(pdpctx_ctr_description), |
| 89 | .ctr_desc = pdpctx_ctr_description, |
Jacob Erlbeck | d31596b | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 90 | .class_id = OSMO_STATS_CLASS_SUBSCRIBER, |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 93 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 94 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 95 | const struct gprs_ra_id *raid) |
| 96 | { |
| 97 | struct sgsn_mm_ctx *ctx; |
| 98 | |
| 99 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 100 | if ((tlli == ctx->gb.tlli || tlli == ctx->gb.tlli_new) && |
Jacob Erlbeck | d6466c9 | 2016-01-04 18:43:34 +0100 | [diff] [blame] | 101 | gprs_ra_id_equals(raid, &ctx->ra)) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 102 | return ctx; |
| 103 | } |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 104 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
| 107 | |
Jacob Erlbeck | 556aa91 | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 108 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli, |
| 109 | const struct gprs_ra_id *raid) |
| 110 | { |
| 111 | struct sgsn_mm_ctx *ctx; |
| 112 | int tlli_type; |
| 113 | |
| 114 | /* TODO: Also check the P_TMSI signature to be safe. That signature |
| 115 | * should be different (at least with a sufficiently high probability) |
| 116 | * after SGSN restarts and for multiple SGSN instances. |
| 117 | */ |
| 118 | |
| 119 | tlli_type = gprs_tlli_type(tlli); |
| 120 | if (tlli_type != TLLI_FOREIGN && tlli_type != TLLI_LOCAL) |
| 121 | return NULL; |
| 122 | |
| 123 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 124 | if ((gprs_tmsi2tlli(ctx->p_tmsi, tlli_type) == tlli || |
| 125 | gprs_tmsi2tlli(ctx->p_tmsi_old, tlli_type) == tlli) && |
| 126 | gprs_ra_id_equals(raid, &ctx->ra)) |
| 127 | return ctx; |
| 128 | } |
| 129 | |
| 130 | return NULL; |
| 131 | } |
| 132 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 133 | 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] | 134 | { |
| 135 | struct sgsn_mm_ctx *ctx; |
| 136 | |
| 137 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | 7e01d8e | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 138 | if (p_tmsi == ctx->p_tmsi || |
| 139 | (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi)) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 140 | return ctx; |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 146 | { |
| 147 | struct sgsn_mm_ctx *ctx; |
| 148 | |
| 149 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 150 | if (!strcmp(imsi, ctx->imsi)) |
| 151 | return ctx; |
| 152 | } |
| 153 | return NULL; |
| 154 | |
| 155 | } |
| 156 | |
| 157 | /* Allocate a new SGSN MM context */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 158 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 159 | const struct gprs_ra_id *raid) |
| 160 | { |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 161 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 162 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 163 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 164 | if (!ctx) |
| 165 | return NULL; |
| 166 | |
| 167 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
Harald Welte | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 168 | ctx->ran_type = MM_CTX_T_GERAN_Gb; |
| 169 | ctx->gb.tlli = tlli; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 170 | ctx->mm_state = GMM_DEREGISTERED; |
Jacob Erlbeck | 4d12767 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 171 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 172 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | ded83ec | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 173 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 174 | |
| 175 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 176 | |
| 177 | return ctx; |
| 178 | } |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 179 | |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 180 | /* this is a hard _free_ function, it doesn't clean up the PDP contexts |
| 181 | * in libgtp! */ |
Holger Hans Peter Freyther | ea2825a | 2015-05-03 11:46:58 +0200 | [diff] [blame] | 182 | static void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm) |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 183 | { |
| 184 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 185 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 186 | /* Unlink from global list of MM contexts */ |
| 187 | llist_del(&mm->list); |
| 188 | |
| 189 | /* Free all PDP contexts */ |
| 190 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) |
| 191 | sgsn_pdp_ctx_free(pdp); |
| 192 | |
| 193 | rate_ctr_group_free(mm->ctrg); |
| 194 | |
| 195 | talloc_free(mm); |
| 196 | } |
| 197 | |
| 198 | void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *mm) |
| 199 | { |
Daniel Willmann | 8ddce12 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 200 | struct gprs_llc_llme *llme = NULL; |
Harald Welte | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 201 | uint32_t tlli = mm->gb.tlli; |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 202 | struct sgsn_pdp_ctx *pdp, *pdp2; |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 203 | struct sgsn_signal_data sig_data; |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 204 | |
Daniel Willmann | 8ddce12 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 205 | if (mm->ran_type == MM_CTX_T_GERAN_Gb) |
| 206 | llme = mm->gb.llme; |
| 207 | else |
| 208 | OSMO_ASSERT(mm->gb.llme == NULL); |
| 209 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 210 | /* Forget about ongoing look-ups */ |
| 211 | if (mm->ggsn_lookup) { |
| 212 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 213 | "Cleaning mmctx with on-going query.\n"); |
| 214 | mm->ggsn_lookup->mmctx = NULL; |
| 215 | mm->ggsn_lookup = NULL; |
| 216 | } |
| 217 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 218 | /* delete all existing PDP contexts for this MS */ |
| 219 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) { |
| 220 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 221 | "Dropping PDP context for NSAPI=%u\n", pdp->nsapi); |
| 222 | sgsn_pdp_ctx_terminate(pdp); |
| 223 | } |
| 224 | |
Jacob Erlbeck | 11493a2 | 2014-10-20 16:05:55 +0200 | [diff] [blame] | 225 | if (osmo_timer_pending(&mm->timer)) { |
| 226 | LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T); |
| 227 | osmo_timer_del(&mm->timer); |
| 228 | } |
| 229 | |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 230 | memset(&sig_data, 0, sizeof(sig_data)); |
| 231 | sig_data.mm = mm; |
| 232 | osmo_signal_dispatch(SS_SGSN, S_SGSN_MM_FREE, &sig_data); |
| 233 | |
| 234 | |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 235 | /* Detach from subscriber which is possibly freed then */ |
| 236 | if (mm->subscr) { |
Jacob Erlbeck | 480e820 | 2015-01-26 13:41:11 +0100 | [diff] [blame] | 237 | struct gsm_subscriber *subscr = subscr_get(mm->subscr); |
Jacob Erlbeck | e71ab2f | 2015-01-26 11:07:24 +0100 | [diff] [blame] | 238 | gprs_subscr_cleanup(subscr); |
Jacob Erlbeck | 7a7d881 | 2015-01-23 13:52:55 +0100 | [diff] [blame] | 239 | subscr_put(subscr); |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 242 | sgsn_mm_ctx_free(mm); |
| 243 | mm = NULL; |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 244 | |
Daniel Willmann | 8ddce12 | 2016-05-21 00:48:49 +0200 | [diff] [blame] | 245 | if (llme) { |
| 246 | /* TLLI unassignment, must be called after sgsn_mm_ctx_free */ |
| 247 | gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
| 248 | } |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 249 | } |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 250 | |
Jacob Erlbeck | 70c177a | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 251 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 252 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 253 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 254 | uint8_t nsapi) |
| 255 | { |
| 256 | struct sgsn_pdp_ctx *pdp; |
| 257 | |
| 258 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 259 | if (pdp->nsapi == nsapi) |
| 260 | return pdp; |
| 261 | } |
| 262 | return NULL; |
| 263 | } |
| 264 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 265 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 266 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 267 | uint8_t tid) |
| 268 | { |
| 269 | struct sgsn_pdp_ctx *pdp; |
| 270 | |
| 271 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 272 | if (pdp->ti == tid) |
| 273 | return pdp; |
| 274 | } |
| 275 | return NULL; |
| 276 | } |
| 277 | |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 278 | /* 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] | 279 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 280 | uint8_t nsapi) |
| 281 | { |
| 282 | struct sgsn_pdp_ctx *pdp; |
| 283 | |
| 284 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 285 | if (pdp) |
| 286 | return NULL; |
| 287 | |
| 288 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 289 | if (!pdp) |
| 290 | return NULL; |
| 291 | |
| 292 | pdp->mm = mm; |
| 293 | pdp->nsapi = nsapi; |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 294 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 295 | llist_add(&pdp->list, &mm->pdp_list); |
| 296 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 297 | |
| 298 | return pdp; |
| 299 | } |
| 300 | |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 301 | #include <pdp.h> |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 302 | /* |
| 303 | * This function will not trigger any GSM DEACT PDP ACK messages, so you |
| 304 | * probably want to call sgsn_delete_pdp_ctx() instead if the connection |
| 305 | * isn't detached already. |
| 306 | */ |
| 307 | void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp) |
| 308 | { |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 309 | struct sgsn_signal_data sig_data; |
| 310 | |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 311 | OSMO_ASSERT(pdp->mm != NULL); |
| 312 | |
| 313 | /* There might still be pending callbacks in libgtp. So the parts of |
| 314 | * this object relevant to GTP need to remain intact in this case. */ |
| 315 | |
| 316 | LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n"); |
| 317 | |
Daniel Willmann | 711adc2 | 2016-05-20 22:36:23 +0200 | [diff] [blame] | 318 | if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) { |
| 319 | /* Force the deactivation of the SNDCP layer */ |
| 320 | sndcp_sm_deactivate_ind(&pdp->mm->gb.llme->lle[pdp->sapi], pdp->nsapi); |
| 321 | } |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 322 | |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 323 | memset(&sig_data, 0, sizeof(sig_data)); |
| 324 | sig_data.pdp = pdp; |
| 325 | osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_TERMINATE, &sig_data); |
| 326 | |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 327 | /* Detach from MM context */ |
| 328 | llist_del(&pdp->list); |
| 329 | pdp->mm = NULL; |
| 330 | |
| 331 | sgsn_delete_pdp_ctx(pdp); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Don't call this function directly unless you know what you are doing. |
| 336 | * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or |
| 337 | * implementation dependent abnormal ones sgsn_pdp_ctx_terminate. |
| 338 | */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 339 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 340 | { |
Holger Hans Peter Freyther | f14098a | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 341 | struct sgsn_signal_data sig_data; |
| 342 | |
| 343 | memset(&sig_data, 0, sizeof(sig_data)); |
| 344 | sig_data.pdp = pdp; |
| 345 | osmo_signal_dispatch(SS_SGSN, S_SGSN_PDP_FREE, &sig_data); |
| 346 | |
Harald Welte | 17a40a6 | 2010-06-28 18:57:21 +0200 | [diff] [blame] | 347 | rate_ctr_group_free(pdp->ctrg); |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 348 | if (pdp->mm) |
| 349 | llist_del(&pdp->list); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 350 | llist_del(&pdp->g_list); |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 351 | |
| 352 | /* _if_ we still have a library handle, at least set it to NULL |
| 353 | * to avoid any dereferences of the now-deleted PDP context from |
| 354 | * sgsn_libgtp:cb_data_ind() */ |
| 355 | if (pdp->lib) { |
| 356 | struct pdp_t *lib = pdp->lib; |
Daniel Willmann | c9ac973 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 357 | LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still " |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 358 | "has a libgtp handle attached to it, this shouldn't " |
| 359 | "happen!\n"); |
| 360 | osmo_generate_backtrace(); |
| 361 | lib->priv = NULL; |
| 362 | } |
| 363 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 364 | if (pdp->destroy_ggsn) |
| 365 | sgsn_ggsn_ctx_free(pdp->ggsn); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 366 | talloc_free(pdp); |
| 367 | } |
| 368 | |
| 369 | /* GGSN contexts */ |
| 370 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 371 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 372 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 373 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 374 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 375 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 376 | if (!ggc) |
| 377 | return NULL; |
| 378 | |
| 379 | ggc->id = id; |
| 380 | ggc->gtp_version = 1; |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 381 | ggc->remote_restart_ctr = -1; |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 382 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 383 | ggc->gsn = sgsn->gsn; |
Harald Welte | e58eee5 | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 384 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 385 | |
| 386 | return ggc; |
| 387 | } |
| 388 | |
Jacob Erlbeck | e3c935d | 2015-02-03 19:53:15 +0100 | [diff] [blame] | 389 | void sgsn_ggsn_ctx_free(struct sgsn_ggsn_ctx *ggc) |
| 390 | { |
| 391 | llist_del(&ggc->list); |
| 392 | talloc_free(ggc); |
| 393 | } |
| 394 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 395 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 396 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 397 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 398 | |
| 399 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 400 | if (id == ggc->id) |
| 401 | return ggc; |
| 402 | } |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 406 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr) |
| 407 | { |
| 408 | struct sgsn_ggsn_ctx *ggc; |
| 409 | |
| 410 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 411 | if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr))) |
| 412 | return ggc; |
| 413 | } |
| 414 | return NULL; |
| 415 | } |
| 416 | |
| 417 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 418 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 419 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 420 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 421 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 422 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 423 | if (!ggc) |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 424 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 425 | return ggc; |
| 426 | } |
| 427 | |
| 428 | /* APN contexts */ |
| 429 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 430 | 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] | 431 | { |
| 432 | struct apn_ctx *actx; |
| 433 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 434 | actx = talloc_zero(tall_bsc_ctx, struct apn_ctx); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 435 | if (!actx) |
| 436 | return NULL; |
| 437 | actx->name = talloc_strdup(actx, ap_name); |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 438 | actx->imsi_prefix = talloc_strdup(actx, imsi_prefix); |
| 439 | |
| 440 | llist_add_tail(&actx->list, &sgsn_apn_ctxts); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 441 | |
| 442 | return actx; |
| 443 | } |
| 444 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 445 | void sgsn_apn_ctx_free(struct apn_ctx *actx) |
| 446 | { |
| 447 | llist_del(&actx->list); |
| 448 | talloc_free(actx); |
| 449 | } |
| 450 | |
| 451 | struct apn_ctx *sgsn_apn_ctx_match(const char *name, const char *imsi) |
| 452 | { |
| 453 | struct apn_ctx *actx; |
| 454 | struct apn_ctx *found_actx = NULL; |
| 455 | size_t imsi_prio = 0; |
| 456 | size_t name_prio = 0; |
| 457 | size_t name_req_len = strlen(name); |
| 458 | |
| 459 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 460 | size_t name_ref_len, imsi_ref_len; |
| 461 | const char *name_ref_start, *name_match_start; |
| 462 | |
| 463 | imsi_ref_len = strlen(actx->imsi_prefix); |
| 464 | if (strncmp(actx->imsi_prefix, imsi, imsi_ref_len) != 0) |
| 465 | continue; |
| 466 | |
| 467 | if (imsi_ref_len < imsi_prio) |
| 468 | continue; |
| 469 | |
| 470 | /* IMSI matches */ |
| 471 | |
| 472 | name_ref_start = &actx->name[0]; |
| 473 | if (name_ref_start[0] == '*') { |
| 474 | /* Suffix match */ |
| 475 | name_ref_start += 1; |
| 476 | name_ref_len = strlen(name_ref_start); |
| 477 | if (name_ref_len > name_req_len) |
| 478 | continue; |
| 479 | } else { |
| 480 | name_ref_len = strlen(name_ref_start); |
| 481 | if (name_ref_len != name_req_len) |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | name_match_start = name + (name_req_len - name_ref_len); |
| 486 | if (strcasecmp(name_match_start, name_ref_start) != 0) |
| 487 | continue; |
| 488 | |
| 489 | /* IMSI and name match */ |
| 490 | |
| 491 | if (imsi_ref_len == imsi_prio && name_ref_len < name_prio) |
| 492 | /* Lower priority, skip */ |
| 493 | continue; |
| 494 | |
| 495 | imsi_prio = imsi_ref_len; |
| 496 | name_prio = name_ref_len; |
| 497 | found_actx = actx; |
| 498 | } |
| 499 | return found_actx; |
| 500 | } |
| 501 | |
| 502 | 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] | 503 | { |
| 504 | struct apn_ctx *actx; |
| 505 | |
| 506 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 507 | if (strcasecmp(name, actx->name) == 0 && |
| 508 | strcasecmp(imsi_prefix, actx->imsi_prefix) == 0) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 509 | return actx; |
| 510 | } |
| 511 | return NULL; |
| 512 | } |
| 513 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 514 | 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] | 515 | { |
| 516 | struct apn_ctx *actx; |
| 517 | |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 518 | actx = sgsn_apn_ctx_by_name(name, imsi_prefix); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 519 | if (!actx) |
Jacob Erlbeck | 9b3ca64 | 2015-02-03 13:47:53 +0100 | [diff] [blame] | 520 | actx = sgsn_apn_ctx_alloc(name, imsi_prefix); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 521 | |
| 522 | return actx; |
| 523 | } |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 524 | |
| 525 | uint32_t sgsn_alloc_ptmsi(void) |
| 526 | { |
| 527 | struct sgsn_mm_ctx *mm; |
| 528 | uint32_t ptmsi; |
Jacob Erlbeck | e3aeb1e | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 529 | int max_retries = 100; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 530 | |
| 531 | restart: |
Daniel Willmann | d7f6cdf | 2015-10-12 19:36:33 +0200 | [diff] [blame] | 532 | if (RAND_bytes((uint8_t *) &ptmsi, sizeof(ptmsi)) != 1) |
| 533 | goto failed; |
| 534 | |
Jacob Erlbeck | e3aeb1e | 2015-01-15 18:51:31 +0100 | [diff] [blame] | 535 | /* Enforce that the 2 MSB are set without loosing the distance between |
| 536 | * identical values. Since rand() has no duplicate values within a |
| 537 | * period (because the size of the state is the same like the size of |
| 538 | * the random value), this leads to a distance of period/4 when the |
| 539 | * distribution of the 2 MSB is uniform. This approach fails with a |
| 540 | * probability of (3/4)^max_retries, only 1% of the approaches will |
| 541 | * need more than 16 numbers (even distribution assumed). |
| 542 | * |
| 543 | * Alternatively, a freeze list could be used if another PRNG is used |
| 544 | * or when this approach proves to be not sufficient. |
| 545 | */ |
| 546 | if (ptmsi >= 0xC0000000) { |
| 547 | if (!max_retries--) |
| 548 | goto failed; |
| 549 | goto restart; |
| 550 | } |
| 551 | ptmsi |= 0xC0000000; |
| 552 | |
| 553 | if (ptmsi == GSM_RESERVED_TMSI) { |
| 554 | if (!max_retries--) |
| 555 | goto failed; |
| 556 | goto restart; |
| 557 | } |
| 558 | |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 559 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 560 | if (mm->p_tmsi == ptmsi) { |
| 561 | if (!max_retries--) |
| 562 | goto failed; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 563 | goto restart; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 564 | } |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | return ptmsi; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 568 | |
| 569 | failed: |
| 570 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n"); |
| 571 | return GSM_RESERVED_TMSI; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 572 | } |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 573 | |
| 574 | static void drop_one_pdp(struct sgsn_pdp_ctx *pdp) |
| 575 | { |
| 576 | if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL) |
| 577 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
| 578 | else { |
| 579 | /* FIXME: GPRS paging in case MS is SUSPENDED */ |
Daniel Willmann | c9ac973 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 580 | LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN " |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 581 | "recovery\n"); |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 582 | /* FIXME: how to tell this to libgtp? */ |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 583 | sgsn_pdp_ctx_free(pdp); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /* 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] | 588 | * otherwise lost state (recovery procedure) */ |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 589 | int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn) |
| 590 | { |
| 591 | struct sgsn_mm_ctx *mm; |
| 592 | int num = 0; |
| 593 | |
| 594 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 595 | struct sgsn_pdp_ctx *pdp; |
| 596 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 597 | if (pdp->ggsn == ggsn) { |
| 598 | drop_one_pdp(pdp); |
| 599 | num++; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | return num; |
| 605 | } |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 606 | |
| 607 | int sgsn_force_reattach_oldmsg(struct msgb *oldmsg) |
| 608 | { |
Jacob Erlbeck | 6e15b73 | 2014-10-31 12:20:49 +0100 | [diff] [blame] | 609 | return gsm0408_gprs_force_reattach_oldmsg(oldmsg); |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 610 | } |
| 611 | |
Jacob Erlbeck | 428f1ec | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 612 | void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx) |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 613 | { |
Jacob Erlbeck | 428f1ec | 2015-01-26 13:52:42 +0100 | [diff] [blame] | 614 | OSMO_ASSERT(mmctx != NULL); |
Jacob Erlbeck | 0939c0f | 2014-12-18 09:53:07 +0100 | [diff] [blame] | 615 | LOGMMCTXP(LOGL_INFO, mmctx, "Subscriber data update\n"); |
Jacob Erlbeck | d04f7cc | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 616 | |
Jacob Erlbeck | b2acd74 | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 617 | sgsn_auth_update(mmctx); |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 618 | } |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 619 | |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 620 | static void insert_qos(struct tlv_parsed *tp, struct sgsn_subscriber_pdp_data *pdp) |
| 621 | { |
| 622 | tp->lv[OSMO_IE_GSM_SUB_QOS].len = pdp->qos_subscribed_len; |
| 623 | tp->lv[OSMO_IE_GSM_SUB_QOS].val = pdp->qos_subscribed; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * The tlv_parsed tp parameter will be modified to insert a |
| 628 | * OSMO_IE_GSM_SUB_QOS in case the data is available in the |
| 629 | * PDP context handling. |
| 630 | */ |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 631 | struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx, |
| 632 | struct tlv_parsed *tp, |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 633 | enum gsm48_gsm_cause *gsm_cause, |
| 634 | char *out_apn_str) |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 635 | { |
| 636 | char req_apn_str[GSM_APN_LENGTH] = {0}; |
| 637 | const struct apn_ctx *apn_ctx = NULL; |
| 638 | const char *selected_apn_str = NULL; |
| 639 | struct sgsn_subscriber_pdp_data *pdp; |
| 640 | struct sgsn_ggsn_ctx *ggsn = NULL; |
| 641 | int allow_any_apn = 0; |
| 642 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 643 | out_apn_str[0] = '\0'; |
| 644 | |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 645 | if (TLVP_PRESENT(tp, GSM48_IE_GSM_APN)) { |
| 646 | if (TLVP_LEN(tp, GSM48_IE_GSM_APN) >= GSM_APN_LENGTH - 1) { |
| 647 | LOGMMCTXP(LOGL_ERROR, mmctx, "APN IE too long\n"); |
| 648 | *gsm_cause = GSM_CAUSE_INV_MAND_INFO; |
| 649 | return NULL; |
| 650 | } |
| 651 | |
| 652 | gprs_apn_to_str(req_apn_str, |
| 653 | TLVP_VAL(tp, GSM48_IE_GSM_APN), |
| 654 | TLVP_LEN(tp, GSM48_IE_GSM_APN)); |
| 655 | |
| 656 | if (strcmp(req_apn_str, "*") == 0) |
| 657 | req_apn_str[0] = 0; |
| 658 | } |
| 659 | |
Holger Hans Peter Freyther | ab9422e | 2015-05-24 20:51:17 +0800 | [diff] [blame] | 660 | if (mmctx->subscr == NULL) |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 661 | allow_any_apn = 1; |
| 662 | |
| 663 | if (strlen(req_apn_str) == 0 && !allow_any_apn) { |
| 664 | /* No specific APN requested, check for an APN that is both |
| 665 | * granted and configured */ |
| 666 | |
| 667 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 668 | if (strcmp(pdp->apn_str, "*") == 0) |
| 669 | { |
| 670 | allow_any_apn = 1; |
| 671 | selected_apn_str = ""; |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 672 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 673 | continue; |
| 674 | } |
| 675 | if (!llist_empty(&sgsn_apn_ctxts)) { |
| 676 | apn_ctx = sgsn_apn_ctx_match(req_apn_str, mmctx->imsi); |
| 677 | /* Not configured */ |
| 678 | if (apn_ctx == NULL) |
| 679 | continue; |
| 680 | } |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 681 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 682 | selected_apn_str = pdp->apn_str; |
| 683 | break; |
| 684 | } |
| 685 | } else if (!allow_any_apn) { |
| 686 | /* Check whether the given APN is granted */ |
| 687 | llist_for_each_entry(pdp, &mmctx->subscr->sgsn_data->pdp_list, list) { |
| 688 | if (strcmp(pdp->apn_str, "*") == 0) { |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 689 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 690 | selected_apn_str = req_apn_str; |
| 691 | allow_any_apn = 1; |
| 692 | continue; |
| 693 | } |
| 694 | if (strcasecmp(pdp->apn_str, req_apn_str) == 0) { |
Holger Hans Peter Freyther | 532b09d | 2015-04-23 11:33:35 -0400 | [diff] [blame] | 695 | insert_qos(tp, pdp); |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 696 | selected_apn_str = req_apn_str; |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | } else if (strlen(req_apn_str) != 0) { |
| 701 | /* Any APN is allowed */ |
| 702 | selected_apn_str = req_apn_str; |
| 703 | } else { |
| 704 | /* Prefer the GGSN associated with the wildcard APN */ |
| 705 | selected_apn_str = ""; |
| 706 | } |
| 707 | |
| 708 | if (!allow_any_apn && selected_apn_str == NULL) { |
| 709 | /* Access not granted */ |
| 710 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 711 | "The requested APN '%s' is not allowed\n", |
| 712 | req_apn_str); |
| 713 | *gsm_cause = GSM_CAUSE_REQ_SERV_OPT_NOTSUB; |
| 714 | return NULL; |
| 715 | } |
| 716 | |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 717 | /* copy the selected apn_str */ |
Holger Hans Peter Freyther | 3332d0c | 2015-06-02 09:33:31 +0200 | [diff] [blame] | 718 | if (selected_apn_str) |
| 719 | strcpy(out_apn_str, selected_apn_str); |
| 720 | else |
| 721 | out_apn_str[0] = '\0'; |
Holger Hans Peter Freyther | 2b1d49a | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 722 | |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 723 | if (apn_ctx == NULL && selected_apn_str) |
| 724 | apn_ctx = sgsn_apn_ctx_match(selected_apn_str, mmctx->imsi); |
| 725 | |
| 726 | if (apn_ctx != NULL) { |
| 727 | ggsn = apn_ctx->ggsn; |
| 728 | } else if (llist_empty(&sgsn_apn_ctxts)) { |
| 729 | /* No configuration -> use GGSN 0 */ |
| 730 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 731 | } else if (allow_any_apn && |
| 732 | (selected_apn_str == NULL || strlen(selected_apn_str) == 0)) { |
| 733 | /* No APN given and no default configuration -> Use GGSN 0 */ |
| 734 | ggsn = sgsn_ggsn_ctx_by_id(0); |
| 735 | } else { |
| 736 | /* No matching configuration found */ |
| 737 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 738 | "The selected APN '%s' has not been configured\n", |
| 739 | selected_apn_str); |
| 740 | *gsm_cause = GSM_CAUSE_MISSING_APN; |
| 741 | return NULL; |
| 742 | } |
| 743 | |
Holger Hans Peter Freyther | eaa5851 | 2015-05-25 14:35:10 +0800 | [diff] [blame] | 744 | if (!ggsn) { |
| 745 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 746 | "No static GGSN configured. Selected APN '%s'\n", |
| 747 | selected_apn_str); |
| 748 | return NULL; |
| 749 | } |
| 750 | |
Jacob Erlbeck | 3400f11 | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 751 | LOGMMCTXP(LOGL_INFO, mmctx, |
| 752 | "Found GGSN %d for APN '%s' (requested '%s')\n", |
| 753 | ggsn->id, selected_apn_str ? selected_apn_str : "---", |
| 754 | req_apn_str); |
| 755 | |
| 756 | return ggsn; |
| 757 | } |
| 758 | |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 759 | static void sgsn_llme_cleanup_free(struct gprs_llc_llme *llme) |
| 760 | { |
| 761 | struct sgsn_mm_ctx *mmctx = NULL; |
| 762 | |
| 763 | llist_for_each_entry(mmctx, &sgsn_mm_ctxts, list) { |
Harald Welte | c28dc2f | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 764 | if (llme == mmctx->gb.llme) { |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 765 | gsm0408_gprs_access_cancelled(mmctx, SGSN_ERROR_CAUSE_NONE); |
| 766 | return; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /* No MM context found */ |
| 771 | LOGP(DGPRS, LOGL_INFO, "Deleting orphaned LLME, TLLI 0x%08x\n", |
| 772 | llme->tlli); |
Max | a86f5c0 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 773 | gprs_llgmm_unassign(llme); |
Jacob Erlbeck | e6140d0 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static void sgsn_llme_check_cb(void *data_) |
| 777 | { |
| 778 | struct gprs_llc_llme *llme, *llme_tmp; |
| 779 | struct timespec now_tp; |
| 780 | time_t now, age; |
| 781 | time_t max_age = gprs_max_time_to_idle(); |
| 782 | |
| 783 | int rc; |
| 784 | |
| 785 | rc = clock_gettime(CLOCK_MONOTONIC, &now_tp); |
| 786 | OSMO_ASSERT(rc >= 0); |
| 787 | now = now_tp.tv_sec; |
| 788 | |
| 789 | LOGP(DGPRS, LOGL_DEBUG, |
| 790 | "Checking for inactive LLMEs, time = %u\n", (unsigned)now); |
| 791 | |
| 792 | llist_for_each_entry_safe(llme, llme_tmp, &gprs_llc_llmes, list) { |
| 793 | if (llme->age_timestamp == GPRS_LLME_RESET_AGE) |
| 794 | llme->age_timestamp = now; |
| 795 | |
| 796 | age = now - llme->age_timestamp; |
| 797 | |
| 798 | if (age > max_age || age < 0) { |
| 799 | LOGP(DGPRS, LOGL_INFO, |
| 800 | "Inactivity timeout for TLLI 0x%08x, age %d\n", |
| 801 | llme->tlli, (int)age); |
| 802 | sgsn_llme_cleanup_free(llme); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 807 | } |
| 808 | |
| 809 | void sgsn_inst_init() |
| 810 | { |
| 811 | sgsn->llme_timer.cb = sgsn_llme_check_cb; |
| 812 | sgsn->llme_timer.data = NULL; |
| 813 | |
| 814 | osmo_timer_schedule(&sgsn->llme_timer, GPRS_LLME_CHECK_TICK, 0); |
| 815 | } |
| 816 | |