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> |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 28 | #include <osmocom/core/backtrace.h> |
Harald Welte | cfb6b28 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 29 | #include <osmocom/gprs/gprs_ns.h> |
| 30 | #include <osmocom/gprs/gprs_bssgp.h> |
| 31 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 32 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 33 | #include <openbsc/debug.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 34 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 35 | #include <openbsc/sgsn.h> |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 36 | #include <openbsc/gsm_04_08_gprs.h> |
| 37 | #include <openbsc/gprs_gmm.h> |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 38 | #include "openbsc/gprs_llc.h" |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 39 | |
| 40 | extern struct sgsn_instance *sgsn; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 41 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 42 | LLIST_HEAD(sgsn_mm_ctxts); |
| 43 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 44 | LLIST_HEAD(sgsn_apn_ctxts); |
| 45 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 46 | |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 47 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 48 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 49 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 50 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 51 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 52 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 53 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 54 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 55 | { "suspend", "SUSPEND Count " }, |
| 56 | { "paging.ps", "Paging Packet Switched " }, |
| 57 | { "paging.cs", "Paging Circuit Switched " }, |
| 58 | { "ra_update", "Routing Area Update " }, |
| 59 | }; |
| 60 | |
| 61 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 62 | .group_name_prefix = "sgsn.mmctx", |
| 63 | .group_description = "SGSN MM Context Statistics", |
| 64 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 65 | .ctr_desc = mmctx_ctr_description, |
| 66 | }; |
| 67 | |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 68 | static const struct rate_ctr_desc pdpctx_ctr_description[] = { |
| 69 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 70 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 71 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 72 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 73 | }; |
| 74 | |
| 75 | static const struct rate_ctr_group_desc pdpctx_ctrg_desc = { |
| 76 | .group_name_prefix = "sgsn.pdpctx", |
| 77 | .group_description = "SGSN PDP Context Statistics", |
| 78 | .num_ctr = ARRAY_SIZE(pdpctx_ctr_description), |
| 79 | .ctr_desc = pdpctx_ctr_description, |
| 80 | }; |
| 81 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 82 | static int ra_id_equals(const struct gprs_ra_id *id1, |
| 83 | const struct gprs_ra_id *id2) |
| 84 | { |
| 85 | return (id1->mcc == id2->mcc && id1->mnc == id2->mnc && |
| 86 | id1->lac == id2->lac && id1->rac == id2->rac); |
| 87 | } |
| 88 | |
Harald Welte | 51f78ee | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 89 | /* See 03.02 Chapter 2.6 */ |
| 90 | static inline uint32_t tlli_foreign(uint32_t tlli) |
| 91 | { |
| 92 | return ((tlli | 0x80000000) & ~0x40000000); |
| 93 | } |
| 94 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 95 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 96 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 97 | const struct gprs_ra_id *raid) |
| 98 | { |
| 99 | struct sgsn_mm_ctx *ctx; |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 100 | int tlli_type; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 101 | |
| 102 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 103 | if (tlli == ctx->tlli && |
| 104 | ra_id_equals(raid, &ctx->ra)) |
| 105 | return ctx; |
| 106 | } |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 107 | |
| 108 | tlli_type = gprs_tlli_type(tlli); |
Harald Welte | 51f78ee | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 109 | switch (tlli_type) { |
| 110 | case TLLI_LOCAL: |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 111 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | 7e01d8e | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 112 | if ((ctx->p_tmsi | 0xC0000000) == tlli || |
| 113 | (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) { |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 114 | ctx->tlli = tlli; |
| 115 | return ctx; |
| 116 | } |
| 117 | } |
Harald Welte | 51f78ee | 2010-12-23 23:34:43 +0100 | [diff] [blame] | 118 | break; |
| 119 | case TLLI_FOREIGN: |
| 120 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 121 | if (tlli == tlli_foreign(ctx->tlli) && |
| 122 | ra_id_equals(raid, &ctx->ra)) |
| 123 | return ctx; |
| 124 | } |
| 125 | break; |
| 126 | default: |
| 127 | break; |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 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)); |
| 168 | ctx->tlli = tlli; |
| 169 | ctx->mm_state = GMM_DEREGISTERED; |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 170 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | ded83ec | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 171 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 172 | |
| 173 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 174 | |
| 175 | return ctx; |
| 176 | } |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 177 | |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 178 | /* this is a hard _free_ function, it doesn't clean up the PDP contexts |
| 179 | * in libgtp! */ |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 180 | void sgsn_mm_ctx_free(struct sgsn_mm_ctx *mm) |
| 181 | { |
| 182 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 183 | |
Jacob Erlbeck | 11493a2 | 2014-10-20 16:05:55 +0200 | [diff] [blame] | 184 | if (osmo_timer_pending(&mm->timer)) { |
| 185 | LOGMMCTXP(LOGL_INFO, mm, "Cancelling MM timer %u\n", mm->T); |
| 186 | osmo_timer_del(&mm->timer); |
| 187 | } |
| 188 | |
Harald Welte | c6e196f | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 189 | /* Unlink from global list of MM contexts */ |
| 190 | llist_del(&mm->list); |
| 191 | |
| 192 | /* Free all PDP contexts */ |
| 193 | llist_for_each_entry_safe(pdp, pdp2, &mm->pdp_list, list) |
| 194 | sgsn_pdp_ctx_free(pdp); |
| 195 | |
| 196 | rate_ctr_group_free(mm->ctrg); |
| 197 | |
| 198 | talloc_free(mm); |
| 199 | } |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 200 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 201 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 202 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 203 | uint8_t nsapi) |
| 204 | { |
| 205 | struct sgsn_pdp_ctx *pdp; |
| 206 | |
| 207 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 208 | if (pdp->nsapi == nsapi) |
| 209 | return pdp; |
| 210 | } |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 214 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 215 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 216 | uint8_t tid) |
| 217 | { |
| 218 | struct sgsn_pdp_ctx *pdp; |
| 219 | |
| 220 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 221 | if (pdp->ti == tid) |
| 222 | return pdp; |
| 223 | } |
| 224 | return NULL; |
| 225 | } |
| 226 | |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 227 | /* 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] | 228 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 229 | uint8_t nsapi) |
| 230 | { |
| 231 | struct sgsn_pdp_ctx *pdp; |
| 232 | |
| 233 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 234 | if (pdp) |
| 235 | return NULL; |
| 236 | |
| 237 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 238 | if (!pdp) |
| 239 | return NULL; |
| 240 | |
| 241 | pdp->mm = mm; |
| 242 | pdp->nsapi = nsapi; |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 243 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 244 | llist_add(&pdp->list, &mm->pdp_list); |
| 245 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 246 | |
| 247 | return pdp; |
| 248 | } |
| 249 | |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 250 | #include <pdp.h> |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 251 | /* |
| 252 | * This function will not trigger any GSM DEACT PDP ACK messages, so you |
| 253 | * probably want to call sgsn_delete_pdp_ctx() instead if the connection |
| 254 | * isn't detached already. |
| 255 | */ |
| 256 | void sgsn_pdp_ctx_terminate(struct sgsn_pdp_ctx *pdp) |
| 257 | { |
| 258 | OSMO_ASSERT(pdp->mm != NULL); |
| 259 | |
| 260 | /* There might still be pending callbacks in libgtp. So the parts of |
| 261 | * this object relevant to GTP need to remain intact in this case. */ |
| 262 | |
| 263 | LOGPDPCTXP(LOGL_INFO, pdp, "Forcing release of PDP context\n"); |
| 264 | |
| 265 | /* Force the deactivation of the SNDCP layer */ |
| 266 | sndcp_sm_deactivate_ind(&pdp->mm->llme->lle[pdp->sapi], pdp->nsapi); |
| 267 | |
| 268 | /* Detach from MM context */ |
| 269 | llist_del(&pdp->list); |
| 270 | pdp->mm = NULL; |
| 271 | |
| 272 | sgsn_delete_pdp_ctx(pdp); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Don't call this function directly unless you know what you are doing. |
| 277 | * In normal conditions use sgsn_delete_pdp_ctx and in unspecified or |
| 278 | * implementation dependent abnormal ones sgsn_pdp_ctx_terminate. |
| 279 | */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 280 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 281 | { |
Harald Welte | 17a40a6 | 2010-06-28 18:57:21 +0200 | [diff] [blame] | 282 | rate_ctr_group_free(pdp->ctrg); |
Jacob Erlbeck | d781c7a | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 283 | if (pdp->mm) |
| 284 | llist_del(&pdp->list); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 285 | llist_del(&pdp->g_list); |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 286 | |
| 287 | /* _if_ we still have a library handle, at least set it to NULL |
| 288 | * to avoid any dereferences of the now-deleted PDP context from |
| 289 | * sgsn_libgtp:cb_data_ind() */ |
| 290 | if (pdp->lib) { |
| 291 | struct pdp_t *lib = pdp->lib; |
Daniel Willmann | c9ac973 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 292 | LOGPDPCTXP(LOGL_NOTICE, pdp, "freeing PDP context that still " |
Harald Welte | 095b3de | 2012-07-14 12:15:19 +0200 | [diff] [blame] | 293 | "has a libgtp handle attached to it, this shouldn't " |
| 294 | "happen!\n"); |
| 295 | osmo_generate_backtrace(); |
| 296 | lib->priv = NULL; |
| 297 | } |
| 298 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 299 | talloc_free(pdp); |
| 300 | } |
| 301 | |
| 302 | /* GGSN contexts */ |
| 303 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 304 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 305 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 306 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 307 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 308 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 309 | if (!ggc) |
| 310 | return NULL; |
| 311 | |
| 312 | ggc->id = id; |
| 313 | ggc->gtp_version = 1; |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 314 | ggc->remote_restart_ctr = -1; |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 315 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 316 | ggc->gsn = sgsn->gsn; |
Harald Welte | e58eee5 | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 317 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 318 | |
| 319 | return ggc; |
| 320 | } |
| 321 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 322 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 323 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 324 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 325 | |
| 326 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 327 | if (id == ggc->id) |
| 328 | return ggc; |
| 329 | } |
| 330 | return NULL; |
| 331 | } |
| 332 | |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 333 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_addr(struct in_addr *addr) |
| 334 | { |
| 335 | struct sgsn_ggsn_ctx *ggc; |
| 336 | |
| 337 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 338 | if (!memcmp(addr, &ggc->remote_addr, sizeof(*addr))) |
| 339 | return ggc; |
| 340 | } |
| 341 | return NULL; |
| 342 | } |
| 343 | |
| 344 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 345 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 346 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 347 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 348 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 349 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 350 | if (!ggc) |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 351 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 352 | return ggc; |
| 353 | } |
| 354 | |
| 355 | /* APN contexts */ |
| 356 | |
| 357 | #if 0 |
| 358 | struct apn_ctx *apn_ctx_alloc(const char *ap_name) |
| 359 | { |
| 360 | struct apn_ctx *actx; |
| 361 | |
| 362 | actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx); |
| 363 | if (!actx) |
| 364 | return NULL; |
| 365 | actx->name = talloc_strdup(actx, ap_name); |
| 366 | |
| 367 | return actx; |
| 368 | } |
| 369 | |
| 370 | struct apn_ctx *apn_ctx_by_name(const char *name) |
| 371 | { |
| 372 | struct apn_ctx *actx; |
| 373 | |
| 374 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 375 | if (!strcmp(name, actx->name)) |
| 376 | return actx; |
| 377 | } |
| 378 | return NULL; |
| 379 | } |
| 380 | |
| 381 | struct apn_ctx *apn_ctx_find_alloc(const char *name) |
| 382 | { |
| 383 | struct apn_ctx *actx; |
| 384 | |
| 385 | actx = apn_ctx_by_name(name); |
| 386 | if (!actx) |
| 387 | actx = apn_ctx_alloc(name); |
| 388 | |
| 389 | return actx; |
| 390 | } |
| 391 | #endif |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 392 | |
| 393 | uint32_t sgsn_alloc_ptmsi(void) |
| 394 | { |
| 395 | struct sgsn_mm_ctx *mm; |
| 396 | uint32_t ptmsi; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 397 | int max_retries = 23; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 398 | |
| 399 | restart: |
Holger Hans Peter Freyther | b3a4dd0 | 2014-08-05 15:20:23 +0200 | [diff] [blame] | 400 | ptmsi = rand() | 0xC0000000; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 401 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 402 | if (mm->p_tmsi == ptmsi) { |
| 403 | if (!max_retries--) |
| 404 | goto failed; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 405 | goto restart; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 406 | } |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | return ptmsi; |
Jacob Erlbeck | 788de3e | 2014-09-19 09:28:42 +0200 | [diff] [blame] | 410 | |
| 411 | failed: |
| 412 | LOGP(DGPRS, LOGL_ERROR, "Failed to allocate a P-TMSI\n"); |
| 413 | return GSM_RESERVED_TMSI; |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 414 | } |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 415 | |
| 416 | static void drop_one_pdp(struct sgsn_pdp_ctx *pdp) |
| 417 | { |
| 418 | if (pdp->mm->mm_state == GMM_REGISTERED_NORMAL) |
| 419 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
| 420 | else { |
| 421 | /* FIXME: GPRS paging in case MS is SUSPENDED */ |
Daniel Willmann | c9ac973 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 422 | LOGPDPCTXP(LOGL_NOTICE, pdp, "Hard-dropping PDP ctx due to GGSN " |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 423 | "recovery\n"); |
Harald Welte | d8765b9 | 2012-07-14 12:04:04 +0200 | [diff] [blame] | 424 | /* FIXME: how to tell this to libgtp? */ |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 425 | sgsn_pdp_ctx_free(pdp); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /* 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] | 430 | * otherwise lost state (recovery procedure) */ |
Harald Welte | 94ecef3 | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 431 | int drop_all_pdp_for_ggsn(struct sgsn_ggsn_ctx *ggsn) |
| 432 | { |
| 433 | struct sgsn_mm_ctx *mm; |
| 434 | int num = 0; |
| 435 | |
| 436 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 437 | struct sgsn_pdp_ctx *pdp; |
| 438 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 439 | if (pdp->ggsn == ggsn) { |
| 440 | drop_one_pdp(pdp); |
| 441 | num++; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | return num; |
| 447 | } |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 448 | |
| 449 | int sgsn_force_reattach_oldmsg(struct msgb *oldmsg) |
| 450 | { |
Jacob Erlbeck | 6e15b73 | 2014-10-31 12:20:49 +0100 | [diff] [blame] | 451 | return gsm0408_gprs_force_reattach_oldmsg(oldmsg); |
Jacob Erlbeck | ccaa22a | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Jacob Erlbeck | c64af7a | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 454 | void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx, |
| 455 | struct sgsn_subscriber_data *sd) |
| 456 | { |
| 457 | OSMO_ASSERT(mmctx); |
| 458 | |
| 459 | if (sd->auth_state != mmctx->auth_state) |
| 460 | sgsn_auth_update(mmctx, sd); |
| 461 | } |