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 |
| 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 | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Harald Welte | 75bb820 | 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 | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 28 | #include <osmocore/rate_ctr.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 29 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 30 | #include <openbsc/debug.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 31 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | f67a5f9 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 32 | #include <openbsc/gprs_ns.h> |
| 33 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 34 | #include <openbsc/sgsn.h> |
| 35 | |
| 36 | extern struct sgsn_instance *sgsn; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 37 | |
Harald Welte | c1f6bfe | 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 | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 42 | |
Harald Welte | 8a035af | 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 | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 64 | static const struct rate_ctr_desc pdpctx_ctr_description[] = { |
| 65 | { "udata.packets.in", "User Data Messages ( In)" }, |
| 66 | { "udata.packets.out", "User Data Messages (Out)" }, |
| 67 | { "udata.bytes.in", "User Data Bytes ( In)" }, |
| 68 | { "udata.bytes.out", "User Data Bytes (Out)" }, |
| 69 | }; |
| 70 | |
| 71 | static const struct rate_ctr_group_desc pdpctx_ctrg_desc = { |
| 72 | .group_name_prefix = "sgsn.pdpctx", |
| 73 | .group_description = "SGSN PDP Context Statistics", |
| 74 | .num_ctr = ARRAY_SIZE(pdpctx_ctr_description), |
| 75 | .ctr_desc = pdpctx_ctr_description, |
| 76 | }; |
| 77 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 78 | static int ra_id_equals(const struct gprs_ra_id *id1, |
| 79 | const struct gprs_ra_id *id2) |
| 80 | { |
| 81 | return (id1->mcc == id2->mcc && id1->mnc == id2->mnc && |
| 82 | id1->lac == id2->lac && id1->rac == id2->rac); |
| 83 | } |
| 84 | |
| 85 | /* look-up a SGSN MM context based on TLLI + RAI */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 86 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 87 | const struct gprs_ra_id *raid) |
| 88 | { |
| 89 | struct sgsn_mm_ctx *ctx; |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 90 | int tlli_type; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 91 | |
| 92 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 93 | if (tlli == ctx->tlli && |
| 94 | ra_id_equals(raid, &ctx->ra)) |
| 95 | return ctx; |
| 96 | } |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 97 | |
| 98 | tlli_type = gprs_tlli_type(tlli); |
| 99 | if (tlli_type == TLLI_LOCAL) { |
| 100 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | 7e01d8e | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 101 | if ((ctx->p_tmsi | 0xC0000000) == tlli || |
| 102 | (ctx->p_tmsi_old && (ctx->p_tmsi_old | 0xC0000000) == tlli)) { |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 103 | ctx->tlli = tlli; |
| 104 | return ctx; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 112 | 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] | 113 | { |
| 114 | struct sgsn_mm_ctx *ctx; |
| 115 | |
| 116 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
Harald Welte | 7e01d8e | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 117 | if (p_tmsi == ctx->p_tmsi || |
| 118 | (ctx->p_tmsi_old && ctx->p_tmsi_old == p_tmsi)) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 119 | return ctx; |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi) |
| 125 | { |
| 126 | struct sgsn_mm_ctx *ctx; |
| 127 | |
| 128 | llist_for_each_entry(ctx, &sgsn_mm_ctxts, list) { |
| 129 | if (!strcmp(imsi, ctx->imsi)) |
| 130 | return ctx; |
| 131 | } |
| 132 | return NULL; |
| 133 | |
| 134 | } |
| 135 | |
| 136 | /* Allocate a new SGSN MM context */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 137 | struct sgsn_mm_ctx *sgsn_mm_ctx_alloc(uint32_t tlli, |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 138 | const struct gprs_ra_id *raid) |
| 139 | { |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 140 | struct sgsn_mm_ctx *ctx; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 141 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 142 | ctx = talloc_zero(tall_bsc_ctx, struct sgsn_mm_ctx); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 143 | if (!ctx) |
| 144 | return NULL; |
| 145 | |
| 146 | memcpy(&ctx->ra, raid, sizeof(ctx->ra)); |
| 147 | ctx->tlli = tlli; |
| 148 | ctx->mm_state = GMM_DEREGISTERED; |
Harald Welte | 8a035af | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 149 | ctx->ctrg = rate_ctr_group_alloc(ctx, &mmctx_ctrg_desc, tlli); |
Harald Welte | ded83ec | 2010-05-18 12:44:45 +0200 | [diff] [blame] | 150 | INIT_LLIST_HEAD(&ctx->pdp_list); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 151 | |
| 152 | llist_add(&ctx->list, &sgsn_mm_ctxts); |
| 153 | |
| 154 | return ctx; |
| 155 | } |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 156 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 157 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 158 | /* look up PDP context by MM context and NSAPI */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 159 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm, |
| 160 | uint8_t nsapi) |
| 161 | { |
| 162 | struct sgsn_pdp_ctx *pdp; |
| 163 | |
| 164 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 165 | if (pdp->nsapi == nsapi) |
| 166 | return pdp; |
| 167 | } |
| 168 | return NULL; |
| 169 | } |
| 170 | |
Harald Welte | ff70e0e | 2010-06-03 06:37:26 +0200 | [diff] [blame] | 171 | /* look up PDP context by MM context and transaction ID */ |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 172 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm, |
| 173 | uint8_t tid) |
| 174 | { |
| 175 | struct sgsn_pdp_ctx *pdp; |
| 176 | |
| 177 | llist_for_each_entry(pdp, &mm->pdp_list, list) { |
| 178 | if (pdp->ti == tid) |
| 179 | return pdp; |
| 180 | } |
| 181 | return NULL; |
| 182 | } |
| 183 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 184 | struct sgsn_pdp_ctx *sgsn_pdp_ctx_alloc(struct sgsn_mm_ctx *mm, |
| 185 | uint8_t nsapi) |
| 186 | { |
| 187 | struct sgsn_pdp_ctx *pdp; |
| 188 | |
| 189 | pdp = sgsn_pdp_ctx_by_nsapi(mm, nsapi); |
| 190 | if (pdp) |
| 191 | return NULL; |
| 192 | |
| 193 | pdp = talloc_zero(tall_bsc_ctx, struct sgsn_pdp_ctx); |
| 194 | if (!pdp) |
| 195 | return NULL; |
| 196 | |
| 197 | pdp->mm = mm; |
| 198 | pdp->nsapi = nsapi; |
Harald Welte | 0fe506b | 2010-06-10 00:20:12 +0200 | [diff] [blame] | 199 | pdp->ctrg = rate_ctr_group_alloc(pdp, &pdpctx_ctrg_desc, nsapi); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 200 | llist_add(&pdp->list, &mm->pdp_list); |
| 201 | llist_add(&pdp->g_list, &sgsn_pdp_ctxts); |
| 202 | |
| 203 | return pdp; |
| 204 | } |
| 205 | |
| 206 | void sgsn_pdp_ctx_free(struct sgsn_pdp_ctx *pdp) |
| 207 | { |
| 208 | llist_del(&pdp->list); |
| 209 | llist_del(&pdp->g_list); |
| 210 | talloc_free(pdp); |
| 211 | } |
| 212 | |
| 213 | /* GGSN contexts */ |
| 214 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 215 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 216 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 217 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 218 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 219 | ggc = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_ctx); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 220 | if (!ggc) |
| 221 | return NULL; |
| 222 | |
| 223 | ggc->id = id; |
| 224 | ggc->gtp_version = 1; |
Harald Welte | ebe8a6d | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 225 | /* if we are called from config file parse, this gsn doesn't exist yet */ |
| 226 | ggc->gsn = sgsn->gsn; |
Harald Welte | e58eee5 | 2010-05-18 18:39:00 +0200 | [diff] [blame] | 227 | llist_add(&ggc->list, &sgsn_ggsn_ctxts); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 228 | |
| 229 | return ggc; |
| 230 | } |
| 231 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 232 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_by_id(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 233 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 234 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 235 | |
| 236 | llist_for_each_entry(ggc, &sgsn_ggsn_ctxts, list) { |
| 237 | if (id == ggc->id) |
| 238 | return ggc; |
| 239 | } |
| 240 | return NULL; |
| 241 | } |
| 242 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 243 | struct sgsn_ggsn_ctx *sgsn_ggsn_ctx_find_alloc(uint32_t id) |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 244 | { |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 245 | struct sgsn_ggsn_ctx *ggc; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 246 | |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 247 | ggc = sgsn_ggsn_ctx_by_id(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 248 | if (!ggc) |
Harald Welte | eb471c9 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 249 | ggc = sgsn_ggsn_ctx_alloc(id); |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 250 | return ggc; |
| 251 | } |
| 252 | |
| 253 | /* APN contexts */ |
| 254 | |
| 255 | #if 0 |
| 256 | struct apn_ctx *apn_ctx_alloc(const char *ap_name) |
| 257 | { |
| 258 | struct apn_ctx *actx; |
| 259 | |
| 260 | actx = talloc_zero(talloc_bsc_ctx, struct apn_ctx); |
| 261 | if (!actx) |
| 262 | return NULL; |
| 263 | actx->name = talloc_strdup(actx, ap_name); |
| 264 | |
| 265 | return actx; |
| 266 | } |
| 267 | |
| 268 | struct apn_ctx *apn_ctx_by_name(const char *name) |
| 269 | { |
| 270 | struct apn_ctx *actx; |
| 271 | |
| 272 | llist_for_each_entry(actx, &sgsn_apn_ctxts, list) { |
| 273 | if (!strcmp(name, actx->name)) |
| 274 | return actx; |
| 275 | } |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | struct apn_ctx *apn_ctx_find_alloc(const char *name) |
| 280 | { |
| 281 | struct apn_ctx *actx; |
| 282 | |
| 283 | actx = apn_ctx_by_name(name); |
| 284 | if (!actx) |
| 285 | actx = apn_ctx_alloc(name); |
| 286 | |
| 287 | return actx; |
| 288 | } |
| 289 | #endif |
Harald Welte | 64df8ed | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 290 | |
| 291 | uint32_t sgsn_alloc_ptmsi(void) |
| 292 | { |
| 293 | struct sgsn_mm_ctx *mm; |
| 294 | uint32_t ptmsi; |
| 295 | |
| 296 | restart: |
| 297 | ptmsi = rand(); |
| 298 | llist_for_each_entry(mm, &sgsn_mm_ctxts, list) { |
| 299 | if (mm->p_tmsi == ptmsi) |
| 300 | goto restart; |
| 301 | } |
| 302 | |
| 303 | return ptmsi; |
| 304 | } |