blob: dd781244c268dc0849a5fdfe4ccc8e1db53c2f5a [file] [log] [blame]
Pau Espin Pedrol58101ea2023-01-09 12:29:27 +01001#pragma once
Harald Welte9b455bf2010-03-14 15:45:01 +08002
Harald Welteeaa614c2010-05-02 11:26:34 +02003#include <stdint.h>
Harald Welted193cb32010-05-17 22:58:03 +02004#include <netinet/in.h>
Pau Espin Pedrol6ec5dc22019-08-28 16:52:56 +02005#include <inttypes.h>
Harald Welted193cb32010-05-17 22:58:03 +02006
Alexander Couzensf7198d72018-05-22 18:29:14 +02007#include <osmocom/core/fsm.h>
Holger Hans Peter Freyther26d0fe32012-01-06 17:29:06 +01008#include <osmocom/core/timer.h>
9
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010010#include <osmocom/gsm/gsm48.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020011
Harald Welte496aee42010-06-30 19:59:55 +020012#include <osmocom/crypt/gprs_cipher.h>
Harald Welted3fa84d2016-04-20 17:50:17 +020013#include <osmocom/gsm/protocol/gsm_23_003.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020014#include <osmocom/crypt/auth.h>
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +010015
Pau Espin Pedrolffd6e372023-01-05 17:45:25 +010016#include <osmocom/sgsn/apn.h>
Pau Espin Pedrol58101ea2023-01-09 12:29:27 +010017#include <osmocom/sgsn/auth.h>
Pau Espin Pedrolc1cf4af2023-01-05 15:43:00 +010018#include <osmocom/sgsn/gprs_subscriber.h>
19
Harald Welte2720e732010-05-17 00:44:57 +020020#define GSM_EXTENSION_LENGTH 15
21
Harald Welte807a5d82010-06-01 11:53:01 +020022struct gprs_llc_lle;
Holger Hans Peter Freythera2730302014-03-23 18:08:26 +010023struct ctrl_handle;
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +010024struct gprs_subscr;
Pau Espin Pedrol5f4736a2023-01-04 21:30:28 +010025struct sgsn_ggsn_ctx;
Pau Espin Pedrol05190c32023-01-05 20:13:13 +010026struct sgsn_pdp_ctx;
Harald Welte807a5d82010-06-01 11:53:01 +020027
Jacob Erlbeck277b71e2015-02-02 18:03:05 +010028enum gsm48_gsm_cause;
29
Harald Weltec2e8cc42010-05-31 20:23:38 +020030enum gprs_mm_ctr {
Harald Welte8acd88f2010-05-18 10:57:45 +020031 GMM_CTR_PKTS_SIG_IN,
32 GMM_CTR_PKTS_SIG_OUT,
33 GMM_CTR_PKTS_UDATA_IN,
34 GMM_CTR_PKTS_UDATA_OUT,
35 GMM_CTR_BYTES_UDATA_IN,
36 GMM_CTR_BYTES_UDATA_OUT,
37 GMM_CTR_PDP_CTX_ACT,
38 GMM_CTR_SUSPEND,
39 GMM_CTR_PAGING_PS,
40 GMM_CTR_PAGING_CS,
41 GMM_CTR_RA_UPDATE,
42};
43
Harald Weltec2e8cc42010-05-31 20:23:38 +020044enum gprs_t3350_mode {
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +010045 GMM_T3350_MODE_NONE,
Harald Weltec2e8cc42010-05-31 20:23:38 +020046 GMM_T3350_MODE_ATT,
47 GMM_T3350_MODE_RAU,
48 GMM_T3350_MODE_PTMSI_REALL,
49};
50
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +080051enum sgsn_ggsn_lookup_state {
52 SGSN_GGSN_2DIGIT,
53 SGSN_GGSN_3DIGIT,
54};
55
56struct sgsn_ggsn_lookup {
57 int state;
58
59 struct sgsn_mm_ctx *mmctx;
60
61 /* APN string */
62 char apn_str[GSM_APN_LENGTH];
63
64 /* the original data */
65 struct msgb *orig_msg;
66 struct tlv_parsed tp;
67
68 /* for dealing with re-transmissions */
69 uint8_t nsapi;
70 uint8_t sapi;
71 uint8_t ti;
72};
73
Harald Weltef97ee042015-12-25 19:12:21 +010074enum sgsn_ran_type {
75 /* GPRS/EDGE via Gb */
76 MM_CTX_T_GERAN_Gb,
77 /* UMTS via Iu */
78 MM_CTX_T_UTRAN_Iu,
Pau Espin Pedrol05140b82019-08-13 18:38:24 +020079#if 0
80 /* GPRS/EDGE via Iu, not supported */
Harald Weltef97ee042015-12-25 19:12:21 +010081 MM_CTX_T_GERAN_Iu,
Pau Espin Pedrol05140b82019-08-13 18:38:24 +020082#endif
Harald Weltef97ee042015-12-25 19:12:21 +010083};
Pau Espin Pedrolfd815bb2019-08-30 18:32:42 +020084extern const struct value_string sgsn_ran_type_names[];
Harald Weltef97ee042015-12-25 19:12:21 +010085
86struct service_info {
87 uint8_t type;
88 uint16_t pdp_status;
89};
90
Neels Hofmeyra7a39472017-07-05 15:19:52 +020091struct ranap_ue_conn_ctx;
Harald Weltef97ee042015-12-25 19:12:21 +010092
Harald Welte9b455bf2010-03-14 15:45:01 +080093/* According to TS 03.60, Table 5: SGSN MM and PDP Contexts */
Harald Weltef533e132010-05-01 16:45:46 +020094/* Extended by 3GPP TS 23.060, Table 6: SGSN MM and PDP Contexts */
Harald Welte9b455bf2010-03-14 15:45:01 +080095struct sgsn_mm_ctx {
96 struct llist_head list;
97
Harald Weltef97ee042015-12-25 19:12:21 +010098 enum sgsn_ran_type ran_type;
99
Harald Welted3fa84d2016-04-20 17:50:17 +0200100 char imsi[GSM23003_IMSI_MAX_DIGITS+1];
Pau Espin Pedrol31c46572019-09-02 16:45:27 +0200101 struct osmo_fsm_inst *gmm_fsm;
Harald Welteeaa614c2010-05-02 11:26:34 +0200102 uint32_t p_tmsi;
Harald Weltec2e8cc42010-05-31 20:23:38 +0200103 uint32_t p_tmsi_old; /* old P-TMSI before new is confirmed */
Harald Welteeaa614c2010-05-02 11:26:34 +0200104 uint32_t p_tmsi_sig;
Harald Welted3fa84d2016-04-20 17:50:17 +0200105 char imei[GSM23003_IMEISV_NUM_DIGITS+1];
Harald Weltef533e132010-05-01 16:45:46 +0200106 /* Opt: Software Version Numbber / TS 23.195 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800107 char msisdn[GSM_EXTENSION_LENGTH];
108 struct gprs_ra_id ra;
Harald Weltef97ee042015-12-25 19:12:21 +0100109 struct {
110 uint16_t cell_id; /* Gb only */
111 uint32_t cell_id_age; /* Gb only */
112 uint8_t radio_prio_sms;
113
114 /* Additional bits not present in the GSM TS */
115 uint16_t nsei;
116 uint16_t bvci;
117 struct gprs_llc_llme *llme;
118 uint32_t tlli;
119 uint32_t tlli_new;
Alexander Couzens39cbecd2017-02-03 22:16:05 +0100120
Pau Espin Pedrol0b722402019-08-29 19:59:07 +0200121 /* TS 23.060 6.1.1 Mobility Management States (A/Gb mode) */
Pau Espin Pedrol02514bc2019-08-30 16:14:22 +0200122 struct osmo_fsm_inst *mm_state_fsm;
Harald Weltef97ee042015-12-25 19:12:21 +0100123 } gb;
124 struct {
125 int new_key;
126 uint16_t sac; /* Iu: Service Area Code */
127 uint32_t sac_age; /* Iu: Service Area Code age */
128 /* CSG ID */
129 /* CSG Membership */
130 /* Access Mode */
131 /* Seelected CN Operator ID (TS 23.251) */
132 /* CSG Subscription Data */
133 /* LIPA Allowed */
134 /* Voice Support Match Indicator */
Neels Hofmeyra7a39472017-07-05 15:19:52 +0200135 struct ranap_ue_conn_ctx *ue_ctx;
Harald Weltef97ee042015-12-25 19:12:21 +0100136 struct service_info service;
Pau Espin Pedrol0b722402019-08-29 19:59:07 +0200137 /* TS 23.060 6.1.2 Mobility Management States (Iu mode) */
Pau Espin Pedrolccd12522019-08-30 17:06:36 +0200138 struct osmo_fsm_inst *mm_state_fsm;
Harald Weltef97ee042015-12-25 19:12:21 +0100139 } iu;
Alexander Couzensf7198d72018-05-22 18:29:14 +0200140 struct {
141 struct osmo_fsm_inst *fsm;
142
143 /* when a second attach req arrives while in this procedure,
144 * the fsm needs to compare it against old to decide what to do */
145 struct msgb *attach_req;
146 uint32_t id_type;
Alexander Couzens97390672018-09-18 18:16:53 +0200147 unsigned int auth_reattempt; /* tracking UMTS resync auth attempts */
Alexander Couzensf7198d72018-05-22 18:29:14 +0200148 } gmm_att_req;
Harald Welte9b455bf2010-03-14 15:45:01 +0800149 /* VLR number */
Harald Welteeaa614c2010-05-02 11:26:34 +0200150 uint32_t new_sgsn_addr;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100151 /* Authentication Triplet */
152 struct gsm_auth_tuple auth_triplet;
Harald Welte9b455bf2010-03-14 15:45:01 +0800153 /* Kc */
Harald Weltef533e132010-05-01 16:45:46 +0200154 /* Iu: CK, IK, KSI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800155 /* CKSN */
156 enum gprs_ciph_algo ciph_algo;
Eric2f898262021-05-19 18:57:50 +0200157 uint8_t ue_cipher_mask;
Max4011e722016-07-05 15:19:12 +0200158 /* Auth & Ciphering Request reference from 3GPP TS 24.008 § 10.5.5.19: */
159 uint8_t ac_ref_nr_used;
Harald Weltef97ee042015-12-25 19:12:21 +0100160
Harald Welte9b455bf2010-03-14 15:45:01 +0800161 struct {
Harald Welteeaa614c2010-05-02 11:26:34 +0200162 uint8_t len;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400163 uint8_t buf[50]; /* GSM 04.08 10.5.5.12a, extended in TS 24.008 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800164 } ms_radio_access_capa;
Harald Weltef97ee042015-12-25 19:12:21 +0100165 /* Supported Codecs (SRVCC) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800166 struct {
Harald Welteeaa614c2010-05-02 11:26:34 +0200167 uint8_t len;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400168 uint8_t buf[8]; /* GSM 04.08 10.5.5.12, extended in TS 24.008 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800169 } ms_network_capa;
Harald Weltef97ee042015-12-25 19:12:21 +0100170 /* UE Netowrk Capability (E-UTRAN) */
Harald Welteeaa614c2010-05-02 11:26:34 +0200171 uint16_t drx_parms;
Harald Weltef97ee042015-12-25 19:12:21 +0100172 /* Active Time value for PSM */
Harald Welte9b455bf2010-03-14 15:45:01 +0800173 int mnrg; /* MS reported to HLR? */
174 int ngaf; /* MS reported to MSC/VLR? */
175 int ppf; /* paging for GPRS + non-GPRS? */
Harald Weltef97ee042015-12-25 19:12:21 +0100176 /* Subscribed Charging Characteristics */
177 /* Trace Reference */
178 /* Trace Type */
179 /* Trigger ID */
180 /* OMC Identity */
Harald Welte9b455bf2010-03-14 15:45:01 +0800181 /* SMS Parameters */
182 int recovery;
Harald Weltef97ee042015-12-25 19:12:21 +0100183 /* Access Restriction */
184 /* GPRS CSI (CAMEL) */
185 /* MG-CSI (CAMEL) */
186 /* Subscribed UE-AMBR */
187 /* UE-AMBR */
188 /* APN Subscribed */
Harald Welte9b455bf2010-03-14 15:45:01 +0800189
190 struct llist_head pdp_list;
191
Harald Welte8acd88f2010-05-18 10:57:45 +0200192 struct rate_ctr_group *ctrg;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200193 struct osmo_timer_list timer;
Harald Weltec2e8cc42010-05-31 20:23:38 +0200194 unsigned int T; /* Txxxx number */
195 unsigned int num_T_exp; /* number of consecutive T expirations */
196
197 enum gprs_t3350_mode t3350_mode;
198 uint8_t t3370_id_type;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100199 uint8_t pending_req; /* the request's message type */
200 /* TODO: There isn't much semantic difference between t3350_mode
201 * (refers to the timer) and pending_req (refers to the procedure),
202 * where mm->T == 3350 => mm->t3350_mode == f(mm->pending_req). Check
203 * whether one of them can be dropped. */
Jacob Erlbeck0c06f982014-10-29 22:12:20 +0100204
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200205 enum sgsn_auth_state auth_state;
Neels Hofmeyraa4ed672018-04-22 19:29:41 +0200206 enum osmo_sub_auth_type sec_ctx;
Jacob Erlbeck02ab91e2014-11-12 09:53:45 +0100207
Holger Hans Peter Freyther8ee13e22015-05-18 10:00:03 +0200208 /* the string representation of the current hlr */
209 char hlr[GSM_EXTENSION_LENGTH];
210
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800211 /* the current GGSN look-up operation */
212 struct sgsn_ggsn_lookup *ggsn_lookup;
213
Neels Hofmeyr0e5d8072017-01-10 00:49:56 +0100214 struct gprs_subscr *subscr;
Harald Welte9b455bf2010-03-14 15:45:01 +0800215};
216
Neels Hofmeyraa4ed672018-04-22 19:29:41 +0200217static inline bool sgsn_mm_ctx_is_authenticated(struct sgsn_mm_ctx *ctx)
218{
219 switch (ctx->sec_ctx) {
220 case OSMO_AUTH_TYPE_GSM:
221 case OSMO_AUTH_TYPE_UMTS:
222 return true;
223 default:
224 return false;
225 }
226}
227
Daniel Willmannb15ceec2014-09-03 15:57:49 +0200228#define LOGMMCTXP(level, mm, fmt, args...) \
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200229 LOGP(DMM, level, "MM(%s/%08x) " fmt, (mm) ? (mm)->imsi : "---", \
230 (mm) ? (mm)->p_tmsi : GSM_RESERVED_TMSI, ## args)
Daniel Willmannb15ceec2014-09-03 15:57:49 +0200231
Pau Espin Pedrol5b6c4b82019-08-14 16:08:15 +0200232#ifdef BUILD_IU
233#define LOGIUP(ue, level, fmt, args...) \
234 LOGP(DMM, level, "UE(0x%x){%s} " fmt, ue->conn_id, osmo_rai_name(&(ue)->ra_id), ## args)
235#else
236#define LOGIUP(ue, level, fmt, args...) \
237 LOGP(DMM, level, "UE(%p){NOTSUPPORTED} " fmt, ue, ## args)
238#endif
239
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +0100240#define LOGGBP(llme, category, level, fmt, args...) \
241 LOGP(category, level, "LLME(%08x/%08x){%s} " fmt, (llme)->old_tlli, \
Pau Espin Pedrol5b6c4b82019-08-14 16:08:15 +0200242 (llme)->tlli, get_value_string_or_null(gprs_llc_llme_state_names, (llme)->state), ## args);
243
244#define LOGGBIUP(llme, msg, level, fmt, args...) \
245 do { \
246 struct ranap_ue_conn_ctx * _ue; \
247 if (llme) { \
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +0100248 LOGGBP(llme, DMM, level, fmt, ## args); \
Pau Espin Pedrol5b6c4b82019-08-14 16:08:15 +0200249 } else if ((msg) && (_ue = MSG_IU_UE_CTX(msg))) { \
250 LOGIUP(_ue, level, fmt, ## args); \
251 } else { OSMO_ASSERT(0); } \
252 } while (0)
253
Harald Welte9b455bf2010-03-14 15:45:01 +0800254/* look-up a SGSN MM context based on TLLI + RAI */
Harald Welteeaa614c2010-05-02 11:26:34 +0200255struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800256 const struct gprs_ra_id *raid);
Harald Welteeaa614c2010-05-02 11:26:34 +0200257struct sgsn_mm_ctx *sgsn_mm_ctx_by_ptmsi(uint32_t tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800258struct sgsn_mm_ctx *sgsn_mm_ctx_by_imsi(const char *imsi);
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200259struct sgsn_mm_ctx *sgsn_mm_ctx_by_ue_ctx(const void *uectx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800260
Jacob Erlbeck5ac4aad2016-01-04 18:43:38 +0100261/* look-up by matching TLLI and P-TMSI (think twice before using this) */
262struct sgsn_mm_ctx *sgsn_mm_ctx_by_tlli_and_ptmsi(uint32_t tlli,
263 const struct gprs_ra_id *raid);
264
Harald Welte9b455bf2010-03-14 15:45:01 +0800265/* Allocate a new SGSN MM context */
Alexander Couzens2b5fb8e2017-02-04 06:01:00 +0100266struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_gb(uint32_t tlli,
Harald Welte9b455bf2010-03-14 15:45:01 +0800267 const struct gprs_ra_id *raid);
Daniel Willmann6292c8d2016-05-21 17:35:57 +0200268struct sgsn_mm_ctx *sgsn_mm_ctx_alloc_iu(void *uectx);
269
Jacob Erlbecke671d252015-01-26 14:43:07 +0100270void sgsn_mm_ctx_cleanup_free(struct sgsn_mm_ctx *ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800271
Jacob Erlbeck277b71e2015-02-02 18:03:05 +0100272struct sgsn_ggsn_ctx *sgsn_mm_ctx_find_ggsn_ctx(struct sgsn_mm_ctx *mmctx,
273 struct tlv_parsed *tp,
Holger Hans Peter Freyther39c430e2015-05-25 12:26:49 +0800274 enum gsm48_gsm_cause *gsm_cause,
275 char *apn_str);
Harald Welted193cb32010-05-17 22:58:03 +0200276
Harald Welte96df6062010-06-03 06:37:26 +0200277/* look up PDP context by MM context and NSAPI */
Harald Welted193cb32010-05-17 22:58:03 +0200278struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_nsapi(const struct sgsn_mm_ctx *mm,
279 uint8_t nsapi);
Harald Welte96df6062010-06-03 06:37:26 +0200280/* look up PDP context by MM context and transaction ID */
Harald Welte77289c22010-05-18 14:32:29 +0200281struct sgsn_pdp_ctx *sgsn_pdp_ctx_by_tid(const struct sgsn_mm_ctx *mm,
282 uint8_t tid);
Harald Welte96df6062010-06-03 06:37:26 +0200283
Harald Welted193cb32010-05-17 22:58:03 +0200284extern struct llist_head sgsn_mm_ctxts;
Harald Welted193cb32010-05-17 22:58:03 +0200285
Harald Welte6463c072010-05-18 17:04:55 +0200286uint32_t sgsn_alloc_ptmsi(void);
287
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200288/* Called on subscriber data updates */
Jacob Erlbeck555b2e52015-01-26 13:52:42 +0100289void sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx);