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 |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 24 | |
| 25 | #include <osmocore/linuxlist.h> |
| 26 | #include <osmocore/talloc.h> |
| 27 | #include <osmocore/timer.h> |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 28 | #include <osmocore/rate_ctr.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 31 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | cb99163 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 32 | #include <openbsc/gprs_ns.h> |
| 33 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 34 | #include <openbsc/sgsn.h> |
| 35 | |
| 36 | extern struct sgsn_instance *sgsn; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 37 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 38 | LLIST_HEAD(sgsn_mm_ctxts); |
| 39 | LLIST_HEAD(sgsn_ggsn_ctxts); |
| 40 | LLIST_HEAD(sgsn_apn_ctxts); |
| 41 | LLIST_HEAD(sgsn_pdp_ctxts); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 42 | |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 43 | static const struct rate_ctr_desc mmctx_ctr_description[] = { |
| 44 | { "sign.packets.in", "Signalling Messages ( In)" }, |
| 45 | { "sign.packets.out", "Signalling Messages (Out)" }, |
| 46 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 47 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 48 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 49 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 50 | { "pdp_ctx_act", "PDP Context Activations " }, |
| 51 | { "suspend", "SUSPEND Count " }, |
| 52 | { "paging.ps", "Paging Packet Switched " }, |
| 53 | { "paging.cs", "Paging Circuit Switched " }, |
| 54 | { "ra_update", "Routing Area Update " }, |
| 55 | }; |
| 56 | |
| 57 | static const struct rate_ctr_group_desc mmctx_ctrg_desc = { |
| 58 | .group_name_prefix = "sgsn.mmctx", |
| 59 | .group_description = "SGSN MM Context Statistics", |
| 60 | .num_ctr = ARRAY_SIZE(mmctx_ctr_description), |
| 61 | .ctr_desc = mmctx_ctr_description, |
| 62 | }; |
| 63 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 64 | static int ra_id_equals(const struct gprs_ra_id *id1, |
| 65 | const struct gprs_ra_id *id2) |
| 66 | { |
| 67 | return (id1->mcc == id2->mcc && id1->mnc == id2->mnc && |
| 68 | id1->lac == id2->lac && id1->rac == id2->rac); |
| 69 | } |
| 70 | |
| 71 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 72 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 73 | const struct gprs_ra_id *raid) |
| 74 | { |
| 75 | struct sgsn_mm_ctx *ctx; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 76 | int tlli_type; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 77 | |
| 78 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 79 | if (tlli == ctx->tlli && |
| 80 | ra_id_equals(raid, &ctx->ra)) |
| 81 | return ctx; |
| 82 | } |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 83 | |
| 84 | tlli_type = gprs_tlli_type(tlli); |
| 85 | if (tlli_type == TLLI_LOCAL) { |
| 86 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 87 | if ((ctx->p_tmsi | 0xC0000000) == tlli) { |
| 88 | ctx->tlli = tlli; |
| 89 | return ctx; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 94 | return NULL; |
| 95 | } |
| 96 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 97 | 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] | 98 | { |
| 99 | struct sgsn_mm_ctx *ctx; |
| 100 | |
| 101 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 102 | if (p_tmsi == ctx->p_tmsi) |
| 103 | return ctx; |
| 104 | } |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 109 | { |
| 110 | struct sgsn_mm_ctx *ctx; |
| 111 | |
| 112 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 113 | if (!strcmp(imsi, ctx->imsi)) |
| 114 | return ctx; |
| 115 | } |
| 116 | return NULL; |
| 117 | |
| 118 | } |
| 119 | |
| 120 | /* Allocate a new SGSN MM context */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 121 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 122 | const struct gprs_ra_id *raid) |
| 123 | { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 124 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 125 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 126 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 127 | if (!ctx) |
| 128 | return NULL; |
| 129 | |
| 130 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
| 131 | ctx->tlli = tlli; |
| 132 | ctx->mm_state = GMM_DEREGISTERED; |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 133 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | 6ffbaab | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 134 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 135 | |
| 136 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 137 | |
| 138 | return ctx; |
| 139 | } |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 140 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 141 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 142 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 143 | uint8_t nsapi) |
| 144 | { |
| 145 | struct sgsn_pdp_ctx *pdp; |
| 146 | |
| 147 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 148 | if (pdp->nsapi == nsapi) |
| 149 | return pdp; |
| 150 | } |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 154 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 155 | uint8_t tid) |
| 156 | { |
| 157 | struct sgsn_pdp_ctx *pdp; |
| 158 | |
| 159 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 160 | if (pdp->ti == tid) |
| 161 | return pdp; |
| 162 | } |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 167 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 168 | uint8_t nsapi) |
| 169 | { |
| 170 | struct sgsn_pdp_ctx *pdp; |
| 171 | |
| 172 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 173 | if (pdp) |
| 174 | return NULL; |
| 175 | |
| 176 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 177 | if (!pdp) |
| 178 | return NULL; |
| 179 | |
| 180 | pdp->mm = mm; |
| 181 | pdp->nsapi = nsapi; |
| 182 | llist_add(&pdp->list, &mm->pdp_list); |
| 183 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 184 | |
| 185 | return pdp; |
| 186 | } |
| 187 | |
| 188 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 189 | { |
| 190 | llist_del(&pdp->list); |
| 191 | llist_del(&pdp->g_list); |
| 192 | talloc_free(pdp); |
| 193 | } |
| 194 | |
| 195 | /* GGSN contexts */ |
| 196 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 197 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 198 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 199 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 200 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 201 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 202 | if (!ggc) |
| 203 | return NULL; |
| 204 | |
| 205 | ggc->id = id; |
| 206 | ggc->gtp_version = 1; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 207 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 208 | ggc->gsn = sgsn->gsn; |
Harald Welte | 119c2ba | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 209 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 210 | |
| 211 | return ggc; |
| 212 | } |
| 213 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 214 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 215 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 216 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 217 | |
| 218 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 219 | if (id == ggc->id) |
| 220 | return ggc; |
| 221 | } |
| 222 | return NULL; |
| 223 | } |
| 224 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 225 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 226 | { |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 227 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 228 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 229 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 230 | if (!ggc) |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 231 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 232 | return ggc; |
| 233 | } |
| 234 | |
| 235 | /* APN contexts */ |
| 236 | |
| 237 | #if 0 |
| 238 | struct apn_ctx *apn_ctx_alloc(const char *ap_name) |
| 239 | { |
| 240 | struct apn_ctx *actx; |
| 241 | |
| 242 | actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx); |
| 243 | if (!actx) |
| 244 | return NULL; |
| 245 | actx->name = talloc_strdup(actx, ap_name); |
| 246 | |
| 247 | return actx; |
| 248 | } |
| 249 | |
| 250 | struct apn_ctx *apn_ctx_by_name(const char *name) |
| 251 | { |
| 252 | struct apn_ctx *actx; |
| 253 | |
| 254 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 255 | if (!strcmp(name, actx->name)) |
| 256 | return actx; |
| 257 | } |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | struct apn_ctx *apn_ctx_find_alloc(const char *name) |
| 262 | { |
| 263 | struct apn_ctx *actx; |
| 264 | |
| 265 | actx = apn_ctx_by_name(name); |
| 266 | if (!actx) |
| 267 | actx = apn_ctx_alloc(name); |
| 268 | |
| 269 | return actx; |
| 270 | } |
| 271 | #endif |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 272 | |
| 273 | uint32_t sgsn_alloc_ptmsi(void) |
| 274 | { |
| 275 | struct sgsn_mm_ctx *mm; |
| 276 | uint32_t ptmsi; |
| 277 | |
| 278 | restart: |
| 279 | ptmsi = rand(); |
| 280 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 281 | if (mm->p_tmsi == ptmsi) |
| 282 | goto restart; |
| 283 | } |
| 284 | |
| 285 | return ptmsi; |
| 286 | } |