Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface |
| 2 | * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */ |
| 3 | |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 4 | /* (C) 2009-2015 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 28 | #include <errno.h> |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 29 | #include <stdbool.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 30 | #include <netinet/in.h> |
Harald Welte | 421cba4 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 32 | #include <netdb.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 33 | |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 34 | #include <openssl/rand.h> |
| 35 | |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 36 | #include "bscconfig.h" |
| 37 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 38 | #include <openbsc/db.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 39 | #include <osmocom/core/msgb.h> |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 40 | #include <osmocom/gsm/tlv.h> |
| 41 | #include <osmocom/gsm/gsm_utils.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 42 | #include <osmocom/core/signal.h> |
| 43 | #include <osmocom/core/talloc.h> |
| 44 | #include <osmocom/core/rate_ctr.h> |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 45 | #include <osmocom/core/utils.h> |
Daniel Willmann | 770f3e3 | 2016-05-21 00:31:12 +0200 | [diff] [blame] | 46 | #include <osmocom/crypt/auth.h> |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 47 | #include <osmocom/gsm/apn.h> |
Harald Welte | 53373bc | 2016-04-20 17:11:43 +0200 | [diff] [blame] | 48 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Harald Welte | d605709 | 2010-05-02 12:57:45 +0200 | [diff] [blame] | 49 | |
Harald Welte | ea34a4e | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 50 | #include <osmocom/gprs/gprs_bssgp.h> |
| 51 | |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 52 | #ifdef BUILD_IU |
| 53 | #include <osmocom/ranap/ranap_ies_defs.h> |
Daniel Willmann | af24172 | 2016-05-21 00:35:41 +0200 | [diff] [blame] | 54 | #include <osmocom/ranap/ranap_msg_factory.h> |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 55 | #include <osmocom/ranap/iu_client.h> |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 58 | #include <openbsc/debug.h> |
| 59 | #include <openbsc/gsm_data.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 60 | #include <openbsc/gsm_subscriber.h> |
| 61 | #include <openbsc/gsm_04_08.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 62 | #include <openbsc/paging.h> |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 63 | #include <openbsc/transaction.h> |
| 64 | #include <openbsc/gprs_llc.h> |
| 65 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 66 | #include <openbsc/gprs_gmm.h> |
Jacob Erlbeck | 8de9c48 | 2015-01-23 11:44:39 +0100 | [diff] [blame] | 67 | #include <openbsc/gprs_utils.h> |
Neels Hofmeyr | 0e5d807 | 2017-01-10 00:49:56 +0100 | [diff] [blame] | 68 | #include <openbsc/gprs_subscriber.h> |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 69 | #include <openbsc/sgsn.h> |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 70 | #include <openbsc/signal.h> |
Philipp Maier | d015cbd | 2017-02-27 17:58:31 +0100 | [diff] [blame] | 71 | #include <openbsc/gprs_sndcp.h> |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 72 | |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 73 | #include <pdp.h> |
| 74 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 75 | #define PTMSI_ALLOC |
| 76 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 77 | extern struct sgsn_instance *sgsn; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 78 | |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 79 | static const struct tlv_definition gsm48_gmm_att_tlvdef = { |
| 80 | .def = { |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 81 | [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 }, |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 82 | [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 }, |
| 83 | [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 }, |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 84 | [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 }, |
| 85 | [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 }, |
| 86 | [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 }, |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 87 | [GSM48_IE_GMM_AUTH_RES_EXT] = { TLV_TYPE_TLV, 0 }, |
| 88 | [GSM48_IE_GMM_AUTH_FAIL_PAR] = { TLV_TYPE_TLV, 0 }, |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 89 | [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 }, |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 90 | [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 }, |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 91 | [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 }, |
| 92 | [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 }, |
| 93 | [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 }, |
| 94 | [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 }, |
| 95 | }, |
| 96 | }; |
| 97 | |
| 98 | static const struct tlv_definition gsm48_sm_att_tlvdef = { |
| 99 | .def = { |
| 100 | [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 }, |
| 101 | [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 }, |
| 102 | [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 }, |
| 103 | [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 }, |
| 104 | [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 }, |
| 105 | [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 }, |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 106 | [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 }, |
| 107 | [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 }, |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 108 | [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 }, |
| 109 | }, |
| 110 | }; |
| 111 | |
Alexander Couzens | 17a29ef | 2017-01-31 18:13:15 +0100 | [diff] [blame] | 112 | static const struct value_string gprs_pmm_state_names[] = { |
| 113 | { PMM_DETACHED, "PMM DETACH" }, |
| 114 | { PMM_CONNECTED, "PMM CONNECTED" }, |
| 115 | { PMM_IDLE, "PMM IDLE" }, |
| 116 | { MM_IDLE, "MM IDLE" }, |
| 117 | { MM_READY, "MM READY" }, |
| 118 | { MM_STANDBY, "MM STANDBY" }, |
Neels Hofmeyr | 5616cdd | 2017-03-01 15:01:28 +0100 | [diff] [blame] | 119 | { 0, NULL } |
Alexander Couzens | 17a29ef | 2017-01-31 18:13:15 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Jacob Erlbeck | 4adb136 | 2014-12-02 09:47:26 +0100 | [diff] [blame] | 122 | static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx); |
| 123 | |
Daniel Willmann | 21b269f | 2016-05-11 12:43:47 +0200 | [diff] [blame] | 124 | static void mmctx_change_gtpu_endpoints_to_sgsn(struct sgsn_mm_ctx *mm_ctx) |
| 125 | { |
| 126 | struct sgsn_pdp_ctx *pdp; |
| 127 | llist_for_each_entry(pdp, &mm_ctx->pdp_list, list) { |
| 128 | sgsn_pdp_upd_gtp_u(pdp, |
| 129 | &sgsn->cfg.gtp_listenaddr.sin_addr, |
| 130 | sizeof(sgsn->cfg.gtp_listenaddr.sin_addr)); |
| 131 | } |
| 132 | } |
| 133 | |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 134 | void mmctx_set_pmm_state(struct sgsn_mm_ctx *ctx, enum gprs_pmm_state state) |
| 135 | { |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 136 | if (ctx->ran_type != MM_CTX_T_UTRAN_Iu) |
| 137 | return; |
| 138 | |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 139 | if (ctx->pmm_state == state) |
| 140 | return; |
| 141 | |
Alexander Couzens | 17a29ef | 2017-01-31 18:13:15 +0100 | [diff] [blame] | 142 | LOGMMCTXP(LOGL_INFO, ctx, "Changing PMM state from %s to %s\n", |
| 143 | get_value_string(gprs_pmm_state_names, ctx->pmm_state), |
| 144 | get_value_string(gprs_pmm_state_names, state)); |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 145 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 146 | switch (state) { |
| 147 | case PMM_IDLE: |
| 148 | /* TODO: start RA Upd timer */ |
| 149 | mmctx_change_gtpu_endpoints_to_sgsn(ctx); |
| 150 | break; |
| 151 | case PMM_CONNECTED: |
| 152 | break; |
| 153 | default: |
| 154 | break; |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | ctx->pmm_state = state; |
| 158 | } |
| 159 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 160 | void mmctx_set_mm_state(struct sgsn_mm_ctx *ctx, enum gprs_pmm_state state) |
| 161 | { |
| 162 | if (ctx->ran_type != MM_CTX_T_GERAN_Gb) |
| 163 | return; |
| 164 | |
| 165 | if (ctx->pmm_state == state) |
| 166 | return; |
| 167 | |
Alexander Couzens | 17a29ef | 2017-01-31 18:13:15 +0100 | [diff] [blame] | 168 | LOGMMCTXP(LOGL_INFO, ctx, "Changing MM state from %s to %s\n", |
| 169 | get_value_string(gprs_pmm_state_names, ctx->pmm_state), |
| 170 | get_value_string(gprs_pmm_state_names, state)); |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 171 | |
| 172 | ctx->pmm_state = state; |
| 173 | } |
| 174 | |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 175 | #ifdef BUILD_IU |
| 176 | int sgsn_ranap_rab_ass_resp(struct sgsn_mm_ctx *ctx, RANAP_RAB_SetupOrModifiedItemIEs_t *setup_ies); |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 177 | int sgsn_ranap_iu_event(struct ranap_ue_conn_ctx *ctx, enum ranap_iu_event_type type, void *data) |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 178 | { |
| 179 | struct sgsn_mm_ctx *mm; |
| 180 | int rc = -1; |
| 181 | |
| 182 | mm = sgsn_mm_ctx_by_ue_ctx(ctx); |
Neels Hofmeyr | ed739b9 | 2017-07-20 14:41:34 +0200 | [diff] [blame] | 183 | |
| 184 | #define REQUIRE_MM \ |
| 185 | if (!mm) { \ |
| 186 | LOGP(DRANAP, LOGL_NOTICE, "Cannot find mm ctx for IU event %d\n", type); \ |
| 187 | return rc; \ |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | switch (type) { |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 191 | case RANAP_IU_EVENT_RAB_ASSIGN: |
Neels Hofmeyr | ed739b9 | 2017-07-20 14:41:34 +0200 | [diff] [blame] | 192 | REQUIRE_MM |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 193 | rc = sgsn_ranap_rab_ass_resp(mm, (RANAP_RAB_SetupOrModifiedItemIEs_t *)data); |
| 194 | break; |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 195 | case RANAP_IU_EVENT_IU_RELEASE: |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 196 | /* fall thru */ |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 197 | case RANAP_IU_EVENT_LINK_INVALIDATED: |
| 198 | /* Clean up ranap_ue_conn_ctx here */ |
Neels Hofmeyr | ed739b9 | 2017-07-20 14:41:34 +0200 | [diff] [blame] | 199 | if (mm) |
| 200 | LOGMMCTXP(LOGL_INFO, mm, "IU release for imsi %s\n", mm->imsi); |
| 201 | else |
| 202 | LOGMMCTXP(LOGL_INFO, mm, "IU release for UE conn 0x%x\n", |
| 203 | ctx->conn_id); |
| 204 | if (mm && mm->pmm_state == PMM_CONNECTED) |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 205 | mmctx_set_pmm_state(mm, PMM_IDLE); |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 206 | rc = 0; |
| 207 | break; |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 208 | case RANAP_IU_EVENT_SECURITY_MODE_COMPLETE: |
Neels Hofmeyr | ed739b9 | 2017-07-20 14:41:34 +0200 | [diff] [blame] | 209 | REQUIRE_MM |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 210 | /* Continue authentication here */ |
| 211 | mm->iu.ue_ctx->integrity_active = 1; |
| 212 | rc = gsm48_gmm_authorize(mm); |
| 213 | break; |
| 214 | default: |
| 215 | LOGP(DRANAP, LOGL_NOTICE, "Unknown event received: %i\n", type); |
| 216 | rc = -1; |
| 217 | break; |
| 218 | } |
| 219 | return rc; |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 224 | /* Our implementation, should be kept in SGSN */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 225 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 226 | static void mmctx_timer_cb(void *_mm); |
| 227 | |
| 228 | static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T, |
| 229 | unsigned int seconds) |
| 230 | { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 231 | if (osmo_timer_pending(&mm->timer)) |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 232 | LOGMMCTXP(LOGL_ERROR, mm, "Starting MM timer %u while old " |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 233 | "timer %u pending\n", T, mm->T); |
| 234 | mm->T = T; |
| 235 | mm->num_T_exp = 0; |
| 236 | |
| 237 | /* FIXME: we should do this only once ? */ |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 238 | osmo_timer_setup(&mm->timer, mmctx_timer_cb, mm); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 239 | osmo_timer_schedule(&mm->timer, seconds, 0); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T) |
| 243 | { |
| 244 | if (mm->T != T) |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 245 | LOGMMCTXP(LOGL_ERROR, mm, "Stopping MM timer %u but " |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 246 | "%u is running\n", T, mm->T); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 247 | osmo_timer_del(&mm->timer); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 250 | time_t gprs_max_time_to_idle(void) |
| 251 | { |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 252 | return sgsn->cfg.timers.T3314 + (sgsn->cfg.timers.T3312 + 4 * 60); |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Neels Hofmeyr | 723f7c7 | 2016-08-11 19:12:54 +0200 | [diff] [blame] | 255 | /* Send a message through the underlying layer. |
| 256 | * For param encryptable, see 3GPP TS 24.008 § 4.7.1.2 and |
| 257 | * gsm48_hdr_gmm_cipherable(). Pass false for not cipherable messages. */ |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 258 | static int gsm48_gmm_sendmsg(struct msgb *msg, int command, |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 259 | struct sgsn_mm_ctx *mm, bool encryptable) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 260 | { |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 261 | if (mm) { |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 262 | rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]); |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 263 | #ifdef BUILD_IU |
| 264 | if (mm->ran_type == MM_CTX_T_UTRAN_Iu) |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 265 | return ranap_iu_tx(msg, GPRS_SAPI_GMM); |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 266 | #endif |
| 267 | } |
| 268 | |
| 269 | #ifdef BUILD_IU |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 270 | /* In Iu mode, msg->dst contains the ranap_ue_conn_ctx pointer, in Gb mode |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 271 | * dst is empty. */ |
| 272 | /* FIXME: have a more explicit indicator for Iu messages */ |
| 273 | if (msg->dst) |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 274 | return ranap_iu_tx(msg, GPRS_SAPI_GMM); |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 275 | #endif |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 276 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 277 | /* caller needs to provide TLLI, BVCI and NSEI */ |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 278 | return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm, encryptable); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 279 | } |
| 280 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 281 | /* copy identifiers from old message to new message, this |
| 282 | * is required so lower layers can route it correctly */ |
| 283 | static void gmm_copy_id(struct msgb *msg, const struct msgb *old) |
| 284 | { |
| 285 | msgb_tlli(msg) = msgb_tlli(old); |
| 286 | msgb_bvci(msg) = msgb_bvci(old); |
| 287 | msgb_nsei(msg) = msgb_nsei(old); |
Daniel Willmann | d75864f | 2016-05-21 00:11:39 +0200 | [diff] [blame] | 288 | msg->dst = old->dst; |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 291 | /* Store BVCI/NSEI in MM context */ |
| 292 | static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg) |
| 293 | { |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 294 | mm->gb.bvci = msgb_bvci(msg); |
| 295 | mm->gb.nsei = msgb_nsei(msg); |
Daniel Willmann | d75864f | 2016-05-21 00:11:39 +0200 | [diff] [blame] | 296 | /* In case a Iu connection is reconnected we need to update the ue ctx */ |
| 297 | mm->iu.ue_ctx = msg->dst; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 298 | if (mm->ran_type == MM_CTX_T_UTRAN_Iu |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 299 | && mm->iu.ue_ctx) { |
| 300 | #ifdef BUILD_IU |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 301 | mm->iu.ue_ctx->rab_assign_addr_enc = |
| 302 | sgsn->cfg.iu.rab_assign_addr_enc; |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 303 | #endif |
| 304 | } |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* Store BVCI/NSEI in MM context */ |
| 308 | static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm) |
| 309 | { |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 310 | msgb_tlli(msg) = mm->gb.tlli; |
| 311 | msgb_bvci(msg) = mm->gb.bvci; |
| 312 | msgb_nsei(msg) = mm->gb.nsei; |
Daniel Willmann | d75864f | 2016-05-21 00:11:39 +0200 | [diff] [blame] | 313 | msg->dst = mm->iu.ue_ctx; |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 316 | static void mm_ctx_cleanup_free(struct sgsn_mm_ctx *ctx, const char *log_text) |
| 317 | { |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 318 | LOGMMCTXP(LOGL_INFO, ctx, "Cleaning MM context due to %s\n", log_text); |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 319 | |
| 320 | /* Mark MM state as deregistered */ |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 321 | ctx->gmm_state = GMM_DEREGISTERED; |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 322 | mmctx_set_pmm_state(ctx, PMM_DETACHED); |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 323 | mmctx_set_pmm_state(ctx, MM_IDLE); |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 324 | |
Jacob Erlbeck | e671d25 | 2015-01-26 14:43:07 +0100 | [diff] [blame] | 325 | sgsn_mm_ctx_cleanup_free(ctx); |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 328 | /* Chapter 9.4.18 */ |
| 329 | static int _tx_status(struct msgb *msg, uint8_t cause, |
| 330 | struct sgsn_mm_ctx *mmctx, int sm) |
| 331 | { |
| 332 | struct gsm48_hdr *gh; |
| 333 | |
| 334 | /* MMCTX might be NULL! */ |
| 335 | |
| 336 | DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n", |
Jacob Erlbeck | afcf230 | 2015-01-05 17:30:02 +0100 | [diff] [blame] | 337 | get_value_string(gsm48_gmm_cause_names, cause)); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 338 | |
| 339 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 340 | if (sm) { |
| 341 | gh->proto_discr = GSM48_PDISC_SM_GPRS; |
| 342 | gh->msg_type = GSM48_MT_GSM_STATUS; |
| 343 | } else { |
| 344 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 345 | gh->msg_type = GSM48_MT_GMM_STATUS; |
| 346 | } |
| 347 | gh->data[0] = cause; |
| 348 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 349 | return gsm48_gmm_sendmsg(msg, 0, mmctx, true); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 350 | } |
Jacob Erlbeck | 7067142 | 2014-10-29 22:57:30 +0100 | [diff] [blame] | 351 | |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 352 | static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause) |
| 353 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 354 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 GMM STATUS"); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 355 | |
| 356 | mmctx2msgid(msg, mmctx); |
| 357 | return _tx_status(msg, cause, mmctx, 0); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 358 | } |
Jacob Erlbeck | 7067142 | 2014-10-29 22:57:30 +0100 | [diff] [blame] | 359 | |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 360 | static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause) |
| 361 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 362 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SM STATUS"); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 363 | |
| 364 | mmctx2msgid(msg, mmctx); |
| 365 | return _tx_status(msg, cause, mmctx, 1); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 368 | static int _tx_detach_req(struct msgb *msg, uint8_t detach_type, uint8_t cause, |
| 369 | struct sgsn_mm_ctx *mmctx) |
| 370 | { |
| 371 | struct gsm48_hdr *gh; |
| 372 | |
| 373 | /* MMCTX might be NULL! */ |
| 374 | |
| 375 | DEBUGP(DMM, "<- GPRS MM DETACH REQ (type: %s, cause: %s)\n", |
| 376 | get_value_string(gprs_det_t_mt_strs, detach_type), |
Jacob Erlbeck | afcf230 | 2015-01-05 17:30:02 +0100 | [diff] [blame] | 377 | get_value_string(gsm48_gmm_cause_names, cause)); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 378 | |
| 379 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 380 | |
| 381 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 382 | gh->msg_type = GSM48_MT_GMM_DETACH_REQ; |
| 383 | gh->data[0] = detach_type & 0x07; |
| 384 | |
| 385 | msgb_tv_put(msg, GSM48_IE_GMM_CAUSE, cause); |
| 386 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 387 | return gsm48_gmm_sendmsg(msg, 0, mmctx, true); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static int gsm48_tx_gmm_detach_req(struct sgsn_mm_ctx *mmctx, |
| 391 | uint8_t detach_type, uint8_t cause) |
| 392 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 393 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DET REQ"); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 394 | |
| 395 | mmctx2msgid(msg, mmctx); |
| 396 | return _tx_detach_req(msg, detach_type, cause, mmctx); |
| 397 | } |
| 398 | |
| 399 | static int gsm48_tx_gmm_detach_req_oldmsg(struct msgb *oldmsg, |
| 400 | uint8_t detach_type, uint8_t cause) |
| 401 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 402 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DET OLD"); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 403 | |
| 404 | gmm_copy_id(msg, oldmsg); |
| 405 | return _tx_detach_req(msg, detach_type, cause, NULL); |
| 406 | } |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 407 | |
Harald Welte | 86fda90 | 2010-05-03 10:54:35 +0200 | [diff] [blame] | 408 | static struct gsm48_qos default_qos = { |
| 409 | .delay_class = 4, /* best effort */ |
| 410 | .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT, |
| 411 | .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps, |
| 412 | .preced_class = GSM48_QOS_PC_NORMAL, |
| 413 | .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT, |
| 414 | .traf_class = GSM48_QOS_TC_INTERACTIVE, |
| 415 | .deliv_order = GSM48_QOS_DO_UNORDERED, |
| 416 | .deliv_err_sdu = GSM48_QOS_ERRSDU_YES, |
| 417 | .max_sdu_size = GSM48_QOS_MAXSDU_1520, |
| 418 | .max_bitrate_up = GSM48_QOS_MBRATE_63k, |
| 419 | .max_bitrate_down = GSM48_QOS_MBRATE_63k, |
| 420 | .resid_ber = GSM48_QOS_RBER_5e_2, |
| 421 | .sdu_err_ratio = GSM48_QOS_SERR_1e_2, |
| 422 | .handling_prio = 3, |
| 423 | .xfer_delay = 0x10, /* 200ms */ |
| 424 | .guar_bitrate_up = GSM48_QOS_MBRATE_0k, |
| 425 | .guar_bitrate_down = GSM48_QOS_MBRATE_0k, |
| 426 | .sig_ind = 0, /* not optimised for signalling */ |
| 427 | .max_bitrate_down_ext = 0, /* use octet 9 */ |
| 428 | .guar_bitrate_down_ext = 0, /* use octet 13 */ |
| 429 | }; |
| 430 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 431 | /* Chapter 9.4.2: Attach accept */ |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 432 | static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 433 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 434 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ATT ACK"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 435 | struct gsm48_hdr *gh; |
| 436 | struct gsm48_attach_ack *aa; |
Holger Hans Peter Freyther | 1768a57 | 2014-04-04 12:40:34 +0200 | [diff] [blame] | 437 | uint8_t *mid; |
| 438 | #if 0 |
| 439 | uint8_t *ptsig; |
| 440 | #endif |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 441 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 442 | LOGMMCTXP(LOGL_INFO, mm, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 443 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_ATTACH_ACKED]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 444 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 445 | mmctx2msgid(msg, mm); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 446 | |
| 447 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 448 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 449 | gh->msg_type = GSM48_MT_GMM_ATTACH_ACK; |
| 450 | |
| 451 | aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa)); |
| 452 | aa->force_stby = 0; /* not indicated */ |
| 453 | aa->att_result = 1; /* GPRS only */ |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 454 | aa->ra_upd_timer = gprs_secs_to_tmr_floor(sgsn->cfg.timers.T3312); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 455 | aa->radio_prio = 4; /* lowest */ |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 456 | gsm48_construct_ra(aa->ra_id.digits, &mm->ra); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 457 | |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 458 | #if 0 |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 459 | /* Optional: P-TMSI signature */ |
| 460 | msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG); |
| 461 | ptsig = msgb_put(msg, 3); |
| 462 | ptsig[0] = mm->p_tmsi_sig >> 16; |
| 463 | ptsig[1] = mm->p_tmsi_sig >> 8; |
| 464 | ptsig[2] = mm->p_tmsi_sig & 0xff; |
| 465 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 466 | #endif |
Jacob Erlbeck | 8de9c48 | 2015-01-23 11:44:39 +0100 | [diff] [blame] | 467 | /* Optional: Negotiated Ready timer value |
| 468 | * (fixed 44s, default value, GSM 04.08, table 11.4a) to safely limit |
| 469 | * the inactivity time READY->STANDBY. |
| 470 | */ |
| 471 | msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY, |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 472 | gprs_secs_to_tmr_floor(sgsn->cfg.timers.T3314)); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 473 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 474 | #ifdef PTMSI_ALLOC |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 475 | /* Optional: Allocated P-TMSI */ |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 476 | mid = msgb_put(msg, GSM48_MID_TMSI_LEN); |
| 477 | gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi); |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 478 | mid[0] = GSM48_IE_GMM_ALLOC_PTMSI; |
| 479 | #endif |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 480 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 481 | /* Optional: MS-identity (combined attach) */ |
| 482 | /* Optional: GMM cause (partial attach result for combined attach) */ |
| 483 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 484 | return gsm48_gmm_sendmsg(msg, 0, mm, true); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* Chapter 9.4.5: Attach reject */ |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 488 | static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause, |
| 489 | const struct sgsn_mm_ctx *mm) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 490 | { |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 491 | struct gsm48_hdr *gh; |
| 492 | |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 493 | LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS ATTACH REJECT: %s\n", |
| 494 | get_value_string(gsm48_gmm_cause_names, gmm_cause)); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 495 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_ATTACH_REJECTED]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 496 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 497 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 498 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 499 | gh->msg_type = GSM48_MT_GMM_ATTACH_REJ; |
| 500 | gh->data[0] = gmm_cause; |
| 501 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 502 | return gsm48_gmm_sendmsg(msg, 0, NULL, false); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 503 | } |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 504 | static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg, |
| 505 | uint8_t gmm_cause) |
| 506 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 507 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ATT REJ OLD"); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 508 | gmm_copy_id(msg, old_msg); |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 509 | return _tx_gmm_att_rej(msg, gmm_cause, NULL); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 510 | } |
| 511 | static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm, |
| 512 | uint8_t gmm_cause) |
| 513 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 514 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ATT REJ"); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 515 | mmctx2msgid(msg, mm); |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 516 | return _tx_gmm_att_rej(msg, gmm_cause, mm); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 517 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 518 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 519 | /* Chapter 9.4.6.2 Detach accept */ |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 520 | static int _tx_detach_ack(struct msgb *msg, uint8_t force_stby, |
| 521 | struct sgsn_mm_ctx *mm) |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 522 | { |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 523 | struct gsm48_hdr *gh; |
| 524 | |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 525 | /* MMCTX might be NULL! */ |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 526 | |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 527 | DEBUGP(DMM, "<- GPRS MM DETACH ACC (force-standby: %d)\n", force_stby); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 528 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_DETACH_ACKED]); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 529 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 530 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 531 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 532 | gh->msg_type = GSM48_MT_GMM_DETACH_ACK; |
| 533 | gh->data[0] = force_stby; |
| 534 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 535 | return gsm48_gmm_sendmsg(msg, 0, mm, true); |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 536 | } |
| 537 | |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 538 | static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby) |
| 539 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 540 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACK"); |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 541 | |
| 542 | mmctx2msgid(msg, mm); |
| 543 | return _tx_detach_ack(msg, force_stby, mm); |
| 544 | } |
| 545 | |
| 546 | static int gsm48_tx_gmm_det_ack_oldmsg(struct msgb *oldmsg, uint8_t force_stby) |
| 547 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 548 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DET ACK OLD"); |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 549 | |
| 550 | gmm_copy_id(msg, oldmsg); |
| 551 | return _tx_detach_ack(msg, force_stby, NULL); |
| 552 | } |
| 553 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 554 | /* Transmit Chapter 9.4.12 Identity Request */ |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 555 | static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 556 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 557 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 558 | struct gsm48_hdr *gh; |
| 559 | |
Harald Welte | b9845f9 | 2015-08-16 18:07:48 +0200 | [diff] [blame] | 560 | LOGMMCTXP(LOGL_DEBUG, mm, "<- GPRS IDENTITY REQUEST: mi_type=%s\n", |
| 561 | gsm48_mi_type_name(id_type)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 562 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 563 | mmctx2msgid(msg, mm); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 564 | |
| 565 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 566 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 567 | gh->msg_type = GSM48_MT_GMM_ID_REQ; |
| 568 | /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */ |
| 569 | gh->data[0] = id_type & 0xf; |
| 570 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 571 | return gsm48_gmm_sendmsg(msg, 1, mm, false); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 572 | } |
| 573 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 574 | /* determine if the MS/UE supports R99 or later */ |
| 575 | static bool mmctx_is_r99(const struct sgsn_mm_ctx *mm) |
| 576 | { |
| 577 | if (mm->ms_network_capa.len < 1) |
| 578 | return false; |
| 579 | if (mm->ms_network_capa.buf[0] & 0x01) |
| 580 | return true; |
| 581 | return false; |
| 582 | } |
| 583 | |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 584 | /* 3GPP TS 24.008 Section 9.4.9: Authentication and Ciphering Request */ |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 585 | static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, |
| 586 | const struct osmo_auth_vector *vec, |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 587 | uint8_t key_seq, bool force_standby) |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 588 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 589 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH CIPH REQ"); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 590 | struct gsm48_hdr *gh; |
| 591 | struct gsm48_auth_ciph_req *acreq; |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 592 | uint8_t *m_rand, *m_cksn, rbyte; |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 593 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 594 | LOGMMCTXP(LOGL_INFO, mm, "<- GPRS AUTH AND CIPHERING REQ (rand = %s", |
| 595 | osmo_hexdump(vec->rand, sizeof(vec->rand))); |
| 596 | if (mmctx_is_r99(mm) && vec |
| 597 | && (vec->auth_types & OSMO_AUTH_TYPE_UMTS)) { |
| 598 | LOGPC(DMM, LOGL_INFO, ", autn = %s)\n", |
| 599 | osmo_hexdump(vec->autn, sizeof(vec->autn))); |
| 600 | } else |
| 601 | LOGPC(DMM, LOGL_INFO, ")\n"); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 602 | |
| 603 | mmctx2msgid(msg, mm); |
| 604 | |
| 605 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 606 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 607 | gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ; |
| 608 | |
| 609 | acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq)); |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 610 | acreq->ciph_alg = mm->ciph_algo & 0xf; |
| 611 | /* § 10.5.5.10: */ |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 612 | acreq->imeisv_req = 0x1; |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 613 | /* § 10.5.5.7: */ |
| 614 | acreq->force_stby = force_standby; |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 615 | /* 3GPP TS 24.008 § 10.5.5.19: */ |
| 616 | if (RAND_bytes(&rbyte, 1) != 1) { |
| 617 | LOGP(DMM, LOGL_NOTICE, "RAND_bytes failed for A&C ref, falling " |
| 618 | "back to rand()\n"); |
| 619 | acreq->ac_ref_nr = rand(); |
| 620 | } else |
| 621 | acreq->ac_ref_nr = rbyte; |
| 622 | mm->ac_ref_nr_used = acreq->ac_ref_nr; |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 623 | |
| 624 | /* Only if authentication is requested we need to set RAND + CKSN */ |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 625 | if (vec) { |
| 626 | m_rand = msgb_put(msg, sizeof(vec->rand) + 1); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 627 | m_rand[0] = GSM48_IE_GMM_AUTH_RAND; |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 628 | memcpy(m_rand + 1, vec->rand, sizeof(vec->rand)); |
| 629 | |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 630 | /* § 10.5.1.2: */ |
Jacob Erlbeck | 65d8273 | 2014-12-01 10:36:17 +0100 | [diff] [blame] | 631 | m_cksn = msgb_put(msg, 1); |
| 632 | m_cksn[0] = (GSM48_IE_GMM_CIPH_CKSN << 4) | (key_seq & 0x07); |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 633 | |
| 634 | /* A Release99 or higher MS/UE must be able to handle |
| 635 | * the optional AUTN IE. If a classic GSM SIM is |
| 636 | * inserted, it will simply ignore AUTN and just use |
| 637 | * RAND */ |
| 638 | if (mmctx_is_r99(mm) && |
| 639 | (vec->auth_types & OSMO_AUTH_TYPE_UMTS)) { |
| 640 | msgb_tlv_put(msg, GSM48_IE_GMM_AUTN, |
| 641 | sizeof(vec->autn), vec->autn); |
| 642 | } |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 643 | } |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 644 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 645 | return gsm48_gmm_sendmsg(msg, 1, mm, false); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /* Section 9.4.11: Authentication and Ciphering Reject */ |
| 649 | static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm) |
| 650 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 651 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH CIPH REJ"); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 652 | struct gsm48_hdr *gh; |
| 653 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 654 | LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS AUTH AND CIPH REJECT\n"); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 655 | |
| 656 | mmctx2msgid(msg, mm); |
| 657 | |
| 658 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 659 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 660 | gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ; |
| 661 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 662 | return gsm48_gmm_sendmsg(msg, 0, mm, false); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 663 | } |
| 664 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 665 | /* check if the received authentication response matches */ |
| 666 | static bool check_auth_resp(struct sgsn_mm_ctx *ctx, |
| 667 | bool is_utran, |
| 668 | const struct osmo_auth_vector *vec, |
| 669 | const uint8_t *res, uint8_t res_len) |
| 670 | { |
| 671 | const uint8_t *expect_res; |
| 672 | uint8_t expect_res_len; |
| 673 | enum osmo_sub_auth_type expect_type; |
| 674 | const char *expect_str; |
| 675 | |
| 676 | if (!vec) |
| 677 | return true; /* really!? */ |
| 678 | |
| 679 | /* On UTRAN (3G) we always expect UMTS AKA. On GERAN (2G) we sent AUTN |
| 680 | * and expect UMTS AKA if there is R99 capability and our vector |
| 681 | * supports UMTS AKA, otherwise we expect GSM AKA. */ |
| 682 | if (is_utran |
| 683 | || (mmctx_is_r99(ctx) && (vec->auth_types & OSMO_AUTH_TYPE_UMTS))) { |
| 684 | expect_type = OSMO_AUTH_TYPE_UMTS; |
| 685 | expect_str = "UMTS RES"; |
| 686 | expect_res = vec->res; |
| 687 | expect_res_len = vec->res_len; |
| 688 | } else { |
| 689 | expect_type = OSMO_AUTH_TYPE_GSM; |
| 690 | expect_str = "GSM SRES"; |
| 691 | expect_res = vec->sres; |
| 692 | expect_res_len = sizeof(vec->sres); |
| 693 | } |
| 694 | |
| 695 | if (!(vec->auth_types & expect_type)) { |
| 696 | LOGMMCTXP(LOGL_ERROR, ctx, "Auth error: auth vector does" |
| 697 | " not provide the expected auth type:" |
| 698 | " expected %s = 0x%x, auth_types are 0x%x\n", |
| 699 | expect_str, expect_type, vec->auth_types); |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | if (!res) |
| 704 | goto auth_mismatch; |
| 705 | |
| 706 | if (res_len != expect_res_len) |
| 707 | goto auth_mismatch; |
| 708 | |
| 709 | if (memcmp(res, expect_res, res_len) != 0) |
| 710 | goto auth_mismatch; |
| 711 | |
| 712 | /* Authorized! */ |
| 713 | return true; |
| 714 | |
| 715 | auth_mismatch: |
| 716 | LOGMMCTXP(LOGL_ERROR, ctx, "Auth mismatch: expected %s = %s\n", |
| 717 | expect_str, osmo_hexdump_nospc(expect_res, expect_res_len)); |
| 718 | return false; |
| 719 | } |
| 720 | |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 721 | /* Section 9.4.10: Authentication and Ciphering Response */ |
| 722 | static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx, |
| 723 | struct msgb *msg) |
| 724 | { |
| 725 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
| 726 | struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data; |
| 727 | struct tlv_parsed tp; |
Jacob Erlbeck | 8ff3fb0 | 2014-12-01 12:44:24 +0100 | [diff] [blame] | 728 | struct gsm_auth_tuple *at; |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 729 | const char *res_name = "(no response)"; |
| 730 | uint8_t res[16]; |
| 731 | uint8_t res_len; |
Jacob Erlbeck | 8ff3fb0 | 2014-12-01 12:44:24 +0100 | [diff] [blame] | 732 | int rc; |
| 733 | |
| 734 | LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS AUTH AND CIPH RESPONSE\n"); |
| 735 | |
| 736 | if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) { |
| 737 | LOGMMCTXP(LOGL_NOTICE, ctx, |
| 738 | "Unexpected Auth & Ciph Response (ignored)\n"); |
| 739 | return 0; |
| 740 | } |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 741 | |
Max | 4011e72 | 2016-07-05 15:19:12 +0200 | [diff] [blame] | 742 | if (acr->ac_ref_nr != ctx->ac_ref_nr_used) { |
| 743 | LOGMMCTXP(LOGL_NOTICE, ctx, "Reference mismatch for Auth & Ciph" |
| 744 | " Response: %u received, %u expected\n", |
| 745 | acr->ac_ref_nr, ctx->ac_ref_nr_used); |
| 746 | return 0; |
| 747 | } |
| 748 | |
Jacob Erlbeck | bd0cf11 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 749 | /* Stop T3360 */ |
| 750 | mmctx_timer_stop(ctx, 3360); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 751 | |
Holger Hans Peter Freyther | 1768a57 | 2014-04-04 12:40:34 +0200 | [diff] [blame] | 752 | tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data, |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 753 | (msg->data + msg->len) - acr->data, 0, 0); |
| 754 | |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 755 | if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) || |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 756 | !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV) || |
| 757 | TLVP_LEN(&tp,GSM48_IE_GMM_AUTH_SRES) != 4) { |
Jacob Erlbeck | 8ff3fb0 | 2014-12-01 12:44:24 +0100 | [diff] [blame] | 758 | /* TODO: missing mandatory IE, return STATUS or REJ? */ |
| 759 | LOGMMCTXP(LOGL_ERROR, ctx, "Missing mandantory IE\n"); |
| 760 | return -EINVAL; |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 761 | } |
| 762 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 763 | /* Start with the good old 4-byte SRES */ |
| 764 | memcpy(res, TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES), 4); |
| 765 | res_len = 4; |
| 766 | res_name = "GSM SRES"; |
| 767 | |
| 768 | /* Append extended RES as part of UMTS AKA, if any */ |
| 769 | if (TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_RES_EXT)) { |
| 770 | unsigned int l = TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_RES_EXT); |
| 771 | if (l > sizeof(res)-4) |
| 772 | l = sizeof(res)-4; |
| 773 | memcpy(res+4, TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_RES_EXT), l); |
| 774 | res_len += l; |
| 775 | res_name = "UMTS RES"; |
| 776 | } |
Jacob Erlbeck | 8ff3fb0 | 2014-12-01 12:44:24 +0100 | [diff] [blame] | 777 | |
| 778 | at = &ctx->auth_triplet; |
| 779 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 780 | LOGMMCTXP(LOGL_DEBUG, ctx, "checking auth: received %s = %s\n", |
| 781 | res_name, osmo_hexdump(res, res_len)); |
| 782 | rc = check_auth_resp(ctx, false, &at->vec, res, res_len); |
| 783 | if (!rc) { |
Jacob Erlbeck | 8ff3fb0 | 2014-12-01 12:44:24 +0100 | [diff] [blame] | 784 | rc = gsm48_tx_gmm_auth_ciph_rej(ctx); |
| 785 | mm_ctx_cleanup_free(ctx, "GPRS AUTH AND CIPH REJECT"); |
| 786 | return rc; |
| 787 | } |
| 788 | |
| 789 | ctx->is_authenticated = 1; |
| 790 | |
Daniel Willmann | 3ecfbbb | 2016-05-21 00:16:55 +0200 | [diff] [blame] | 791 | if (ctx->ran_type == MM_CTX_T_UTRAN_Iu) |
| 792 | ctx->iu.new_key = 1; |
| 793 | |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 794 | /* FIXME: enable LLC cipheirng */ |
Jacob Erlbeck | 4adb136 | 2014-12-02 09:47:26 +0100 | [diff] [blame] | 795 | |
| 796 | /* Check if we can let the mobile station enter */ |
| 797 | return gsm48_gmm_authorize(ctx); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 800 | /* Section 9.4.10: Authentication and Ciphering Failure */ |
| 801 | static int gsm48_rx_gmm_auth_ciph_fail(struct sgsn_mm_ctx *ctx, |
| 802 | struct msgb *msg) |
| 803 | { |
| 804 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
| 805 | struct tlv_parsed tp; |
| 806 | const uint8_t gmm_cause = gh->data[0]; |
| 807 | const uint8_t *auts; |
| 808 | int rc; |
| 809 | |
| 810 | LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS AUTH AND CIPH FAILURE (cause = %s)\n", |
| 811 | get_value_string(gsm48_gmm_cause_names, gmm_cause)); |
| 812 | |
| 813 | tlv_parse(&tp, &gsm48_gmm_att_tlvdef, gh->data+1, msg->len - 1, 0, 0); |
| 814 | |
| 815 | /* Only if GMM cause is present and the AUTS is provided, we can |
| 816 | * start re-sync procedure */ |
| 817 | if (gmm_cause == GMM_CAUSE_SYNC_FAIL && |
| 818 | TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_FAIL_PAR)) { |
| 819 | if (TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_FAIL_PAR) != 14) { |
| 820 | LOGMMCTXP(LOGL_ERROR, ctx, "AUTS IE has wrong size:" |
| 821 | " expected %d, got %u\n", 14, |
| 822 | TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_FAIL_PAR)); |
| 823 | return -EINVAL; |
| 824 | } |
| 825 | auts = TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_FAIL_PAR); |
| 826 | |
| 827 | LOGMMCTXP(LOGL_INFO, ctx, |
| 828 | "R99 AUTHENTICATION SYNCH (AUTS = %s)\n", |
| 829 | osmo_hexdump_nospc(auts, 14)); |
| 830 | |
| 831 | /* make sure we'll refresh the auth_triplet in |
| 832 | * sgsn_auth_update() */ |
| 833 | ctx->auth_triplet.key_seq = GSM_KEY_SEQ_INVAL; |
| 834 | |
| 835 | /* make sure we'll retry authentication after the resync */ |
| 836 | ctx->auth_state = SGSN_AUTH_UMTS_RESYNC; |
| 837 | |
| 838 | /* Send AUTS to HLR and wait for new Auth Info Result */ |
| 839 | rc = gprs_subscr_request_auth_info(ctx, auts, |
| 840 | ctx->auth_triplet.vec.rand); |
| 841 | if (!rc) |
| 842 | return 0; |
| 843 | /* on error, fall through to send a reject */ |
| 844 | LOGMMCTXP(LOGL_ERROR, ctx, |
| 845 | "Sending AUTS to HLR failed (rc = %d)\n", rc); |
| 846 | } |
| 847 | |
| 848 | LOGMMCTXP(LOGL_NOTICE, ctx, "Authentication failed\n"); |
| 849 | rc = gsm48_tx_gmm_auth_ciph_rej(ctx); |
| 850 | mm_ctx_cleanup_free(ctx, "GPRS AUTH FAILURE"); |
| 851 | return rc; |
| 852 | } |
| 853 | |
Holger Hans Peter Freyther | 20de3ae | 2015-05-05 22:52:40 +0200 | [diff] [blame] | 854 | static void extract_subscr_msisdn(struct sgsn_mm_ctx *ctx) |
| 855 | { |
| 856 | struct gsm_mncc_number called; |
| 857 | uint8_t msisdn[sizeof(ctx->subscr->sgsn_data->msisdn) + 1]; |
| 858 | |
| 859 | /* Convert MSISDN from encoded to string.. */ |
| 860 | if (!ctx->subscr) |
| 861 | return; |
| 862 | |
| 863 | if (ctx->subscr->sgsn_data->msisdn_len < 1) |
| 864 | return; |
| 865 | |
| 866 | /* prepare the data for the decoder */ |
| 867 | memset(&called, 0, sizeof(called)); |
| 868 | msisdn[0] = ctx->subscr->sgsn_data->msisdn_len; |
| 869 | memcpy(&msisdn[1], ctx->subscr->sgsn_data->msisdn, |
| 870 | ctx->subscr->sgsn_data->msisdn_len); |
| 871 | |
| 872 | /* decode the string now */ |
| 873 | gsm48_decode_called(&called, msisdn); |
| 874 | |
| 875 | /* Prepend a '+' for international numbers */ |
| 876 | if (called.plan == 1 && called.type == 1) { |
| 877 | ctx->msisdn[0] = '+'; |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 878 | osmo_strlcpy(&ctx->msisdn[1], called.number, |
| 879 | sizeof(ctx->msisdn)); |
Holger Hans Peter Freyther | 20de3ae | 2015-05-05 22:52:40 +0200 | [diff] [blame] | 880 | } else { |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 881 | osmo_strlcpy(ctx->msisdn, called.number, sizeof(ctx->msisdn)); |
Holger Hans Peter Freyther | 20de3ae | 2015-05-05 22:52:40 +0200 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
Holger Hans Peter Freyther | 8ee13e2 | 2015-05-18 10:00:03 +0200 | [diff] [blame] | 885 | static void extract_subscr_hlr(struct sgsn_mm_ctx *ctx) |
| 886 | { |
| 887 | struct gsm_mncc_number called; |
| 888 | uint8_t hlr_number[sizeof(ctx->subscr->sgsn_data->hlr) + 1]; |
| 889 | |
| 890 | if (!ctx->subscr) |
| 891 | return; |
| 892 | |
| 893 | if (ctx->subscr->sgsn_data->hlr_len < 1) |
| 894 | return; |
| 895 | |
| 896 | /* prepare the data for the decoder */ |
| 897 | memset(&called, 0, sizeof(called)); |
| 898 | hlr_number[0] = ctx->subscr->sgsn_data->hlr_len; |
| 899 | memcpy(&hlr_number[1], ctx->subscr->sgsn_data->hlr, |
| 900 | ctx->subscr->sgsn_data->hlr_len); |
| 901 | |
| 902 | /* decode the string now */ |
| 903 | gsm48_decode_called(&called, hlr_number); |
| 904 | |
| 905 | if (called.plan != 1) { |
| 906 | LOGMMCTXP(LOGL_ERROR, ctx, |
| 907 | "Numbering plan(%d) not allowed\n", |
| 908 | called.plan); |
| 909 | return; |
| 910 | } |
| 911 | |
| 912 | if (called.type != 1) { |
| 913 | LOGMMCTXP(LOGL_ERROR, ctx, |
| 914 | "Numbering type(%d) not allowed\n", |
| 915 | called.type); |
| 916 | return; |
| 917 | } |
| 918 | |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 919 | osmo_strlcpy(ctx->hlr, called.number, sizeof(ctx->hlr)); |
Holger Hans Peter Freyther | 8ee13e2 | 2015-05-18 10:00:03 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 922 | #ifdef BUILD_IU |
| 923 | /* Chapter 9.4.21: Service accept */ |
| 924 | static int gsm48_tx_gmm_service_ack(struct sgsn_mm_ctx *mm) |
| 925 | { |
| 926 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SERVICE ACK"); |
| 927 | struct gsm48_hdr *gh; |
| 928 | |
| 929 | LOGMMCTXP(LOGL_INFO, mm, "<- GPRS SERVICE ACCEPT (P-TMSI=0x%08x)\n", mm->p_tmsi); |
| 930 | |
| 931 | mmctx2msgid(msg, mm); |
| 932 | |
| 933 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 934 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 935 | gh->msg_type = GSM48_MT_GMM_SERVICE_ACK; |
| 936 | |
| 937 | /* Optional: PDP context status */ |
| 938 | /* Optional: MBMS context status */ |
| 939 | |
| 940 | return gsm48_gmm_sendmsg(msg, 0, mm, false); |
| 941 | } |
| 942 | #endif |
| 943 | |
| 944 | /* Chapter 9.4.22: Service reject */ |
| 945 | static int _tx_gmm_service_rej(struct msgb *msg, uint8_t gmm_cause, |
| 946 | const struct sgsn_mm_ctx *mm) |
| 947 | { |
| 948 | struct gsm48_hdr *gh; |
| 949 | |
| 950 | LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS SERVICE REJECT: %s\n", |
| 951 | get_value_string(gsm48_gmm_cause_names, gmm_cause)); |
| 952 | |
| 953 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 954 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 955 | gh->msg_type = GSM48_MT_GMM_SERVICE_REJ; |
| 956 | gh->data[0] = gmm_cause; |
| 957 | |
| 958 | return gsm48_gmm_sendmsg(msg, 0, NULL, true); |
| 959 | } |
| 960 | static int gsm48_tx_gmm_service_rej_oldmsg(const struct msgb *old_msg, |
| 961 | uint8_t gmm_cause) |
| 962 | { |
| 963 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SERVICE REJ OLD"); |
| 964 | gmm_copy_id(msg, old_msg); |
| 965 | return _tx_gmm_service_rej(msg, gmm_cause, NULL); |
| 966 | } |
| 967 | #if 0 |
| 968 | -- currently unused -- |
| 969 | static int gsm48_tx_gmm_service_rej(struct sgsn_mm_ctx *mm, |
| 970 | uint8_t gmm_cause) |
| 971 | { |
| 972 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SERVICE REJ"); |
| 973 | mmctx2msgid(msg, mm); |
| 974 | return _tx_gmm_service_rej(msg, gmm_cause, mm); |
| 975 | } |
| 976 | #endif |
| 977 | |
| 978 | static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm); |
| 979 | |
| 980 | #ifdef BUILD_IU |
Neels Hofmeyr | ecd8c04 | 2017-07-20 14:41:40 +0200 | [diff] [blame] | 981 | /* Send RAB activation requests for all PDP contexts */ |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 982 | void activate_pdp_rabs(struct sgsn_mm_ctx *ctx) |
| 983 | { |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 984 | struct sgsn_pdp_ctx *pdp; |
Neels Hofmeyr | ecd8c04 | 2017-07-20 14:41:40 +0200 | [diff] [blame] | 985 | if (ctx->ran_type != MM_CTX_T_UTRAN_Iu) |
| 986 | return; |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 987 | llist_for_each_entry(pdp, &ctx->pdp_list, list) { |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 988 | iu_rab_act_ps(pdp->nsapi, pdp); |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | #endif |
| 992 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 993 | /* Check if we can already authorize a subscriber */ |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 994 | static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 995 | { |
Daniel Willmann | 3ecfbbb | 2016-05-21 00:16:55 +0200 | [diff] [blame] | 996 | #ifdef BUILD_IU |
| 997 | int rc; |
| 998 | #endif |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 999 | #ifndef PTMSI_ALLOC |
| 1000 | struct sgsn_signal_data sig_data; |
| 1001 | #endif |
| 1002 | |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1003 | /* Request IMSI and IMEI from the MS if they are unknown */ |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1004 | if (!strlen(ctx->imei)) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1005 | ctx->t3370_id_type = GSM_MI_TYPE_IMEI; |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 1006 | mmctx_timer_start(ctx, 3370, sgsn->cfg.timers.T3370); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 1007 | return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1008 | } |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1009 | if (!strlen(ctx->imsi)) { |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1010 | ctx->t3370_id_type = GSM_MI_TYPE_IMSI; |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 1011 | mmctx_timer_start(ctx, 3370, sgsn->cfg.timers.T3370); |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 1012 | return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1013 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1014 | |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1015 | /* All information required for authentication is available */ |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1016 | ctx->t3370_id_type = GSM_MI_TYPE_NONE; |
| 1017 | |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1018 | if (ctx->auth_state == SGSN_AUTH_UNKNOWN) { |
| 1019 | /* Request authorization, this leads to a call to |
Jacob Erlbeck | f951a01 | 2014-11-07 14:17:44 +0100 | [diff] [blame] | 1020 | * sgsn_auth_update which in turn calls |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1021 | * gsm0408_gprs_access_granted or gsm0408_gprs_access_denied */ |
| 1022 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 1023 | sgsn_auth_request(ctx); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1024 | /* Note that gsm48_gmm_authorize can be called recursively via |
| 1025 | * sgsn_auth_request iff ctx->auth_info changes to AUTH_ACCEPTED |
| 1026 | */ |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
Jacob Erlbeck | 4adb136 | 2014-12-02 09:47:26 +0100 | [diff] [blame] | 1030 | if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && !ctx->is_authenticated) { |
| 1031 | struct gsm_auth_tuple *at = &ctx->auth_triplet; |
| 1032 | |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 1033 | mmctx_timer_start(ctx, 3360, sgsn->cfg.timers.T3360); |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 1034 | return gsm48_tx_gmm_auth_ciph_req(ctx, &at->vec, at->key_seq, |
| 1035 | false); |
Jacob Erlbeck | 4adb136 | 2014-12-02 09:47:26 +0100 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && ctx->is_authenticated && |
| 1039 | ctx->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) { |
| 1040 | /* Check again for authorization */ |
| 1041 | sgsn_auth_request(ctx); |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1045 | if (ctx->auth_state != SGSN_AUTH_ACCEPTED) { |
| 1046 | LOGMMCTXP(LOGL_NOTICE, ctx, |
| 1047 | "authorization is denied, aborting procedure\n"); |
| 1048 | return -EACCES; |
Jacob Erlbeck | 0c06f98 | 2014-10-29 22:12:20 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* The MS is authorized */ |
Daniel Willmann | 3ecfbbb | 2016-05-21 00:16:55 +0200 | [diff] [blame] | 1052 | #ifdef BUILD_IU |
| 1053 | if (ctx->ran_type == MM_CTX_T_UTRAN_Iu && !ctx->iu.ue_ctx->integrity_active) { |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 1054 | rc = ranap_iu_tx_sec_mode_cmd(ctx->iu.ue_ctx, &ctx->auth_triplet.vec, 0, ctx->iu.new_key); |
Daniel Willmann | 3ecfbbb | 2016-05-21 00:16:55 +0200 | [diff] [blame] | 1055 | ctx->iu.new_key = 0; |
| 1056 | return rc; |
| 1057 | } |
| 1058 | #endif |
Jacob Erlbeck | 0c06f98 | 2014-10-29 22:12:20 +0100 | [diff] [blame] | 1059 | |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1060 | switch (ctx->pending_req) { |
| 1061 | case 0: |
| 1062 | LOGMMCTXP(LOGL_INFO, ctx, |
| 1063 | "no pending request, authorization completed\n"); |
| 1064 | break; |
| 1065 | case GSM48_MT_GMM_ATTACH_REQ: |
Daniel Willmann | fac9758 | 2016-05-21 00:21:15 +0200 | [diff] [blame] | 1066 | ctx->pending_req = 0; |
Holger Hans Peter Freyther | 20de3ae | 2015-05-05 22:52:40 +0200 | [diff] [blame] | 1067 | |
| 1068 | extract_subscr_msisdn(ctx); |
Holger Hans Peter Freyther | 8ee13e2 | 2015-05-18 10:00:03 +0200 | [diff] [blame] | 1069 | extract_subscr_hlr(ctx); |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1070 | #ifdef PTMSI_ALLOC |
| 1071 | /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */ |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 1072 | mmctx_timer_start(ctx, 3350, sgsn->cfg.timers.T3350); |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1073 | ctx->t3350_mode = GMM_T3350_MODE_ATT; |
Jacob Erlbeck | 0074a77 | 2014-10-28 16:23:46 +0100 | [diff] [blame] | 1074 | #else |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 1075 | memset(&sig_data, 0, sizeof(sig_data)); |
| 1076 | sig_data.mm = mmctx; |
| 1077 | osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data); |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1078 | ctx->gmm_state = GMM_REGISTERED_NORMAL; |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1079 | #endif |
| 1080 | |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1081 | return gsm48_tx_gmm_att_ack(ctx); |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1082 | #ifdef BUILD_IU |
| 1083 | case GSM48_MT_GMM_SERVICE_REQ: |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1084 | ctx->pending_req = 0; |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 1085 | mmctx_set_pmm_state(ctx, PMM_CONNECTED); |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1086 | rc = gsm48_tx_gmm_service_ack(ctx); |
| 1087 | |
Daniel Willmann | af24172 | 2016-05-21 00:35:41 +0200 | [diff] [blame] | 1088 | if (ctx->iu.service.type != GPRS_SERVICE_T_SIGNALLING) |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1089 | activate_pdp_rabs(ctx); |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1090 | |
| 1091 | return rc; |
| 1092 | #endif |
| 1093 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 1094 | ctx->pending_req = 0; |
| 1095 | /* Send RA UPDATE ACCEPT */ |
| 1096 | return gsm48_tx_gmm_ra_upd_ack(ctx); |
| 1097 | |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1098 | default: |
| 1099 | LOGMMCTXP(LOGL_ERROR, ctx, |
| 1100 | "only Attach Request is supported yet, " |
| 1101 | "got request type %u\n", ctx->pending_req); |
| 1102 | break; |
| 1103 | } |
| 1104 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1105 | return 0; |
| 1106 | } |
| 1107 | |
Jacob Erlbeck | 4adb136 | 2014-12-02 09:47:26 +0100 | [diff] [blame] | 1108 | void gsm0408_gprs_authenticate(struct sgsn_mm_ctx *ctx) |
| 1109 | { |
| 1110 | ctx->is_authenticated = 0; |
| 1111 | |
| 1112 | gsm48_gmm_authorize(ctx); |
| 1113 | } |
| 1114 | |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1115 | void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx) |
| 1116 | { |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1117 | switch (ctx->gmm_state) { |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1118 | case GMM_COMMON_PROC_INIT: |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 1119 | LOGMMCTXP(LOGL_NOTICE, ctx, |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1120 | "Authorized, continuing procedure, IMSI=%s\n", |
| 1121 | ctx->imsi); |
| 1122 | /* Continue with the authorization */ |
| 1123 | gsm48_gmm_authorize(ctx); |
| 1124 | break; |
| 1125 | default: |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 1126 | LOGMMCTXP(LOGL_INFO, ctx, |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1127 | "Authorized, ignored, IMSI=%s\n", |
| 1128 | ctx->imsi); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1132 | void gsm0408_gprs_access_denied(struct sgsn_mm_ctx *ctx, int gmm_cause) |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1133 | { |
Jacob Erlbeck | d6267d1 | 2015-01-19 11:10:04 +0100 | [diff] [blame] | 1134 | if (gmm_cause == SGSN_ERROR_CAUSE_NONE) |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1135 | gmm_cause = GMM_CAUSE_GPRS_NOTALLOWED; |
| 1136 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1137 | switch (ctx->gmm_state) { |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1138 | case GMM_COMMON_PROC_INIT: |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1139 | LOGMMCTXP(LOGL_NOTICE, ctx, |
| 1140 | "Not authorized, rejecting ATTACH REQUEST " |
| 1141 | "with cause '%s' (%d)\n", |
| 1142 | get_value_string(gsm48_gmm_cause_names, gmm_cause), |
| 1143 | gmm_cause); |
| 1144 | gsm48_tx_gmm_att_rej(ctx, gmm_cause); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1145 | mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT"); |
| 1146 | break; |
| 1147 | case GMM_REGISTERED_NORMAL: |
| 1148 | case GMM_REGISTERED_SUSPENDED: |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1149 | LOGMMCTXP(LOGL_NOTICE, ctx, |
| 1150 | "Authorization lost, detaching " |
| 1151 | "with cause '%s' (%d)\n", |
| 1152 | get_value_string(gsm48_gmm_cause_names, gmm_cause), |
| 1153 | gmm_cause); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1154 | gsm48_tx_gmm_detach_req( |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1155 | ctx, GPRS_DET_T_MT_REATT_NOTREQ, gmm_cause); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1156 | |
| 1157 | mm_ctx_cleanup_free(ctx, "auth lost"); |
| 1158 | break; |
| 1159 | default: |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1160 | LOGMMCTXP(LOGL_INFO, ctx, |
| 1161 | "Authorization lost, cause is '%s' (%d)\n", |
| 1162 | get_value_string(gsm48_gmm_cause_names, gmm_cause), |
| 1163 | gmm_cause); |
| 1164 | mm_ctx_cleanup_free(ctx, "auth lost"); |
Jacob Erlbeck | 423f8bf | 2014-10-24 18:09:54 +0200 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1168 | void gsm0408_gprs_access_cancelled(struct sgsn_mm_ctx *ctx, int gmm_cause) |
Jacob Erlbeck | 98647ca | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 1169 | { |
Jacob Erlbeck | d6267d1 | 2015-01-19 11:10:04 +0100 | [diff] [blame] | 1170 | if (gmm_cause != SGSN_ERROR_CAUSE_NONE) { |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1171 | LOGMMCTXP(LOGL_INFO, ctx, |
| 1172 | "Cancelled with cause '%s' (%d), deleting context\n", |
| 1173 | get_value_string(gsm48_gmm_cause_names, gmm_cause), |
| 1174 | gmm_cause); |
| 1175 | gsm0408_gprs_access_denied(ctx, gmm_cause); |
| 1176 | return; |
Jacob Erlbeck | 98647ca | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 1177 | } |
Jacob Erlbeck | af3d5c5 | 2015-01-05 17:51:17 +0100 | [diff] [blame] | 1178 | |
| 1179 | LOGMMCTXP(LOGL_INFO, ctx, "Cancelled, deleting context silently\n"); |
Jacob Erlbeck | 98647ca | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 1180 | mm_ctx_cleanup_free(ctx, "access cancelled"); |
| 1181 | } |
| 1182 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1183 | /* Parse Chapter 9.4.13 Identity Response */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1184 | static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1185 | { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 1186 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1187 | uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1188 | char mi_string[GSM48_MI_SIZE]; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1189 | |
| 1190 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1191 | if (!ctx) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1192 | DEBUGP(DMM, "from unknown TLLI 0x%08x?!? This should not happen\n", msgb_tlli(msg)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1193 | return -EINVAL; |
| 1194 | } |
| 1195 | |
Harald Welte | b9845f9 | 2015-08-16 18:07:48 +0200 | [diff] [blame] | 1196 | LOGMMCTXP(LOGL_DEBUG, ctx, "-> GMM IDENTITY RESPONSE: MI(%s)=%s\n", |
| 1197 | gsm48_mi_type_name(mi_type), mi_string); |
Daniel Willmann | f8070f4 | 2014-09-23 18:48:44 +0200 | [diff] [blame] | 1198 | |
Jacob Erlbeck | fb26c60 | 2014-10-22 12:20:26 +0200 | [diff] [blame] | 1199 | if (ctx->t3370_id_type == GSM_MI_TYPE_NONE) { |
| 1200 | LOGMMCTXP(LOGL_NOTICE, ctx, |
Harald Welte | b9845f9 | 2015-08-16 18:07:48 +0200 | [diff] [blame] | 1201 | "Got unexpected IDENTITY RESPONSE: MI(%s)=%s, " |
Jacob Erlbeck | fb26c60 | 2014-10-22 12:20:26 +0200 | [diff] [blame] | 1202 | "ignoring message\n", |
Harald Welte | b9845f9 | 2015-08-16 18:07:48 +0200 | [diff] [blame] | 1203 | gsm48_mi_type_name(mi_type), mi_string); |
Jacob Erlbeck | fb26c60 | 2014-10-22 12:20:26 +0200 | [diff] [blame] | 1204 | return -EINVAL; |
| 1205 | } |
| 1206 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1207 | if (mi_type == ctx->t3370_id_type) |
| 1208 | mmctx_timer_stop(ctx, 3370); |
| 1209 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1210 | switch (mi_type) { |
| 1211 | case GSM_MI_TYPE_IMSI: |
| 1212 | /* we already have a mm context with current TLLI, but no |
| 1213 | * P-TMSI / IMSI yet. What we now need to do is to fill |
| 1214 | * this initial context with data from the HLR */ |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 1215 | if (strlen(ctx->imsi) == 0) { |
| 1216 | /* Check if we already have a MM context for this IMSI */ |
| 1217 | struct sgsn_mm_ctx *ictx; |
| 1218 | ictx = sgsn_mm_ctx_by_imsi(mi_string); |
| 1219 | if (ictx) { |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 1220 | /* Handle it like in gsm48_rx_gmm_det_req, |
| 1221 | * except that no messages are sent to the BSS */ |
| 1222 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1223 | LOGMMCTXP(LOGL_NOTICE, ctx, "Deleting old MM Context for same IMSI " |
| 1224 | "p_tmsi_old=0x%08x\n", |
| 1225 | ictx->p_tmsi); |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 1226 | |
Jacob Erlbeck | 99985b5 | 2014-10-13 10:32:00 +0200 | [diff] [blame] | 1227 | mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use"); |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 1228 | } |
| 1229 | } |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 1230 | osmo_strlcpy(ctx->imsi, mi_string, sizeof(ctx->imsi)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1231 | break; |
| 1232 | case GSM_MI_TYPE_IMEI: |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 1233 | osmo_strlcpy(ctx->imei, mi_string, sizeof(ctx->imei)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1234 | break; |
| 1235 | case GSM_MI_TYPE_IMEISV: |
| 1236 | break; |
| 1237 | } |
| 1238 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1239 | /* Check if we can let the mobile station enter */ |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1240 | return gsm48_gmm_authorize(ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* Section 9.4.1 Attach request */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1244 | static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg, |
| 1245 | struct gprs_llc_llme *llme) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1246 | { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 1247 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Holger Hans Peter Freyther | 1768a57 | 2014-04-04 12:40:34 +0200 | [diff] [blame] | 1248 | uint8_t *cur = gh->data, *msnc, *mi, *ms_ra_acc_cap; |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1249 | uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1250 | uint16_t drx_par; |
| 1251 | uint32_t tmsi; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1252 | char mi_string[GSM48_MI_SIZE]; |
| 1253 | struct gprs_ra_id ra_id; |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 1254 | uint16_t cid = 0; |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1255 | enum gsm48_gmm_cause reject_cause; |
| 1256 | int rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1257 | |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 1258 | LOGMMCTXP(LOGL_INFO, ctx, "-> GMM ATTACH REQUEST "); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 1259 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_ATTACH_REQUEST]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1260 | |
| 1261 | /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either |
| 1262 | * with a foreign TLLI (P-TMSI that was allocated to the MS before), |
| 1263 | * or with random TLLI. */ |
| 1264 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 1265 | /* In Iu mode, msg->dst contains the ranap_ue_conn_ctx pointer, in Gb mode |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 1266 | * dst is empty. */ |
| 1267 | /* FIXME: have a more explicit indicator for Iu messages */ |
| 1268 | if (!msg->dst) { |
| 1269 | /* Gb mode */ |
| 1270 | cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg)); |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 1271 | } else { |
| 1272 | #ifdef BUILD_IU |
| 1273 | ra_id = ((struct ranap_ue_conn_ctx*)msg->dst)->ra_id; |
| 1274 | #else |
| 1275 | LOGMMCTXP(LOGL_ERROR, ctx, "Cannot handle Iu Attach Request, built without Iu support\n"); |
| 1276 | return -ENOTSUP; |
| 1277 | #endif |
| 1278 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1279 | |
| 1280 | /* MS network capability 10.5.5.12 */ |
| 1281 | msnc_len = *cur++; |
| 1282 | msnc = cur; |
Alexander Chemeris | 84402c0 | 2013-07-03 10:12:23 +0400 | [diff] [blame] | 1283 | if (msnc_len > sizeof(ctx->ms_network_capa.buf)) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1284 | goto err_inval; |
| 1285 | cur += msnc_len; |
| 1286 | |
Daniel Willmann | 746c789 | 2016-01-25 16:49:53 +0100 | [diff] [blame] | 1287 | /* TODO: In iu mode - handle follow-on request */ |
| 1288 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1289 | /* aTTACH Type 10.5.5.2 */ |
Daniel Willmann | 746c789 | 2016-01-25 16:49:53 +0100 | [diff] [blame] | 1290 | att_type = *cur++ & 0x07; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1291 | |
| 1292 | /* DRX parameter 10.5.5.6 */ |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1293 | drx_par = *cur++ << 8; |
| 1294 | drx_par |= *cur++; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1295 | |
| 1296 | /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */ |
| 1297 | mi_len = *cur++; |
| 1298 | mi = cur; |
| 1299 | if (mi_len > 8) |
| 1300 | goto err_inval; |
| 1301 | mi_type = *mi & GSM_MI_TYPE_MASK; |
| 1302 | cur += mi_len; |
| 1303 | |
| 1304 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 1305 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1306 | DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, |
| 1307 | get_value_string(gprs_att_t_strs, att_type)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1308 | |
Holger Hans Peter Freyther | 1768a57 | 2014-04-04 12:40:34 +0200 | [diff] [blame] | 1309 | /* Old routing area identification 10.5.5.15. Skip it */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1310 | cur += 6; |
| 1311 | |
| 1312 | /* MS Radio Access Capability 10.5.5.12a */ |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1313 | ms_ra_acc_cap_len = *cur++; |
| 1314 | ms_ra_acc_cap = cur; |
Alexander Chemeris | 84402c0 | 2013-07-03 10:12:23 +0400 | [diff] [blame] | 1315 | if (ms_ra_acc_cap_len > sizeof(ctx->ms_radio_access_capa.buf)) |
Harald Welte | c48ac47 | 2010-07-03 21:20:06 +0200 | [diff] [blame] | 1316 | goto err_inval; |
Harald Welte | 4b2ed35 | 2011-07-27 23:35:38 +0200 | [diff] [blame] | 1317 | cur += ms_ra_acc_cap_len; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1318 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1319 | LOGPC(DMM, LOGL_INFO, "\n"); |
| 1320 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1321 | /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */ |
| 1322 | |
| 1323 | switch (mi_type) { |
| 1324 | case GSM_MI_TYPE_IMSI: |
| 1325 | /* Try to find MM context based on IMSI */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1326 | if (!ctx) |
| 1327 | ctx = sgsn_mm_ctx_by_imsi(mi_string); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1328 | if (!ctx) { |
| 1329 | #if 0 |
| 1330 | return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN); |
| 1331 | #else |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 1332 | if (msg->dst) |
| 1333 | ctx = sgsn_mm_ctx_alloc_iu(msg->dst); |
| 1334 | else |
Alexander Couzens | 2b5fb8e | 2017-02-04 06:01:00 +0100 | [diff] [blame] | 1335 | ctx = sgsn_mm_ctx_alloc_gb(0, &ra_id); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1336 | if (!ctx) { |
| 1337 | reject_cause = GMM_CAUSE_NET_FAIL; |
| 1338 | goto rejected; |
| 1339 | } |
Neels Hofmeyr | 93bafb6 | 2017-01-13 03:12:08 +0100 | [diff] [blame] | 1340 | osmo_strlcpy(ctx->imsi, mi_string, sizeof(ctx->imsi)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1341 | #endif |
| 1342 | } |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1343 | if (ctx->ran_type == MM_CTX_T_GERAN_Gb) { |
| 1344 | ctx->gb.tlli = msgb_tlli(msg); |
| 1345 | ctx->gb.llme = llme; |
| 1346 | } |
Harald Welte | 68b99a4 | 2010-05-18 12:05:42 +0200 | [diff] [blame] | 1347 | msgid2mmctx(ctx, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1348 | break; |
| 1349 | case GSM_MI_TYPE_TMSI: |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 1350 | memcpy(&tmsi, mi+1, 4); |
| 1351 | tmsi = ntohl(tmsi); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1352 | /* Try to find MM context based on P-TMSI */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1353 | if (!ctx) |
| 1354 | ctx = sgsn_mm_ctx_by_ptmsi(tmsi); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1355 | if (!ctx) { |
Harald Welte | c728eea | 2010-12-24 23:07:18 +0100 | [diff] [blame] | 1356 | /* Allocate a context as most of our code expects one. |
| 1357 | * Context will not have an IMSI ultil ID RESP is received */ |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 1358 | if (msg->dst) |
| 1359 | ctx = sgsn_mm_ctx_alloc_iu(msg->dst); |
| 1360 | else |
Alexander Couzens | 2b5fb8e | 2017-02-04 06:01:00 +0100 | [diff] [blame] | 1361 | ctx = sgsn_mm_ctx_alloc_gb(msgb_tlli(msg), &ra_id); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1362 | ctx->p_tmsi = tmsi; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1363 | } |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1364 | if (ctx->ran_type == MM_CTX_T_GERAN_Gb) { |
| 1365 | ctx->gb.tlli = msgb_tlli(msg); |
| 1366 | ctx->gb.llme = llme; |
| 1367 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1368 | msgid2mmctx(ctx, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1369 | break; |
| 1370 | default: |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 1371 | LOGMMCTXP(LOGL_NOTICE, ctx, "Rejecting ATTACH REQUEST with " |
Harald Welte | b9845f9 | 2015-08-16 18:07:48 +0200 | [diff] [blame] | 1372 | "MI type %s\n", gsm48_mi_type_name(mi_type)); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1373 | reject_cause = GMM_CAUSE_MS_ID_NOT_DERIVED; |
| 1374 | goto rejected; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1375 | } |
Harald Welte | 9f1f3ad | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 1376 | /* Update MM Context with currient RA and Cell ID */ |
| 1377 | ctx->ra = ra_id; |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1378 | if (ctx->ran_type == MM_CTX_T_GERAN_Gb) |
| 1379 | ctx->gb.cell_id = cid; |
Daniel Willmann | 770f3e3 | 2016-05-21 00:31:12 +0200 | [diff] [blame] | 1380 | |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1381 | /* Update MM Context with other data */ |
| 1382 | ctx->drx_parms = drx_par; |
Alexander Chemeris | 84402c0 | 2013-07-03 10:12:23 +0400 | [diff] [blame] | 1383 | ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len; |
Harald Welte | 4b2ed35 | 2011-07-27 23:35:38 +0200 | [diff] [blame] | 1384 | memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap, |
| 1385 | ctx->ms_radio_access_capa.len); |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 1386 | ctx->ms_network_capa.len = msnc_len; |
| 1387 | memcpy(ctx->ms_network_capa.buf, msnc, msnc_len); |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 1388 | if (!gprs_ms_net_cap_gea_supported(ctx->ms_network_capa.buf, msnc_len, |
| 1389 | ctx->ciph_algo)) { |
| 1390 | reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 1391 | LOGMMCTXP(LOGL_NOTICE, ctx, "Rejecting ATTACH REQUEST with MI " |
| 1392 | "type %s because MS do not support required %s " |
| 1393 | "encryption\n", gsm48_mi_type_name(mi_type), |
| 1394 | get_value_string(gprs_cipher_names,ctx->ciph_algo)); |
| 1395 | goto rejected; |
| 1396 | } |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1397 | #ifdef PTMSI_ALLOC |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1398 | /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */ |
Jacob Erlbeck | f6e7d99 | 2014-11-06 15:43:10 +0100 | [diff] [blame] | 1399 | /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */ |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1400 | if (ctx->gmm_state != GMM_COMMON_PROC_INIT) { |
Jacob Erlbeck | f6e7d99 | 2014-11-06 15:43:10 +0100 | [diff] [blame] | 1401 | ctx->p_tmsi_old = ctx->p_tmsi; |
| 1402 | ctx->p_tmsi = sgsn_alloc_ptmsi(); |
| 1403 | } |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1404 | ctx->gmm_state = GMM_COMMON_PROC_INIT; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 1405 | #endif |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1406 | |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1407 | if (ctx->ran_type == MM_CTX_T_GERAN_Gb) { |
| 1408 | /* Even if there is no P-TMSI allocated, the MS will |
| 1409 | * switch from foreign TLLI to local TLLI */ |
| 1410 | ctx->gb.tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL); |
| 1411 | |
| 1412 | /* Inform LLC layer about new TLLI but keep old active */ |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 1413 | if (ctx->is_authenticated) |
| 1414 | gprs_llme_copy_key(ctx, ctx->gb.llme); |
| 1415 | |
| 1416 | gprs_llgmm_assign(ctx->gb.llme, ctx->gb.tlli, ctx->gb.tlli_new); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1417 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1418 | |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1419 | ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ; |
| 1420 | return gsm48_gmm_authorize(ctx); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1421 | |
| 1422 | err_inval: |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1423 | LOGPC(DMM, LOGL_INFO, "\n"); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1424 | reject_cause = GMM_CAUSE_SEM_INCORR_MSG; |
| 1425 | |
| 1426 | rejected: |
| 1427 | /* Send ATTACH REJECT */ |
| 1428 | LOGMMCTXP(LOGL_NOTICE, ctx, |
| 1429 | "Rejecting Attach Request with cause '%s' (%d)\n", |
Jacob Erlbeck | afcf230 | 2015-01-05 17:30:02 +0100 | [diff] [blame] | 1430 | get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1431 | rc = gsm48_tx_gmm_att_rej_oldmsg(msg, reject_cause); |
| 1432 | if (ctx) |
| 1433 | mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJ"); |
| 1434 | else |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1435 | gprs_llgmm_unassign(llme); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1436 | |
| 1437 | return rc; |
| 1438 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1441 | /* Section 4.7.4.1 / 9.4.5.2 MO Detach request */ |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1442 | static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1443 | { |
| 1444 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1445 | uint8_t detach_type, power_off; |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 1446 | int rc = 0; |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1447 | |
| 1448 | detach_type = gh->data[0] & 0x7; |
| 1449 | power_off = gh->data[0] & 0x8; |
| 1450 | |
| 1451 | /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */ |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 1452 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_DETACH_REQUEST]); |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1453 | LOGMMCTXP(LOGL_INFO, ctx, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n", |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1454 | msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type), |
| 1455 | power_off ? "Power-off" : ""); |
| 1456 | |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 1457 | /* Only send the Detach Accept (MO) if power off isn't indicated, |
Jacob Erlbeck | b9ab0d4 | 2014-10-21 09:52:05 +0200 | [diff] [blame] | 1458 | * see 04.08, 4.7.4.1.2/3 for details */ |
| 1459 | if (!power_off) { |
| 1460 | /* force_stby = 0 */ |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 1461 | if (ctx) |
| 1462 | rc = gsm48_tx_gmm_det_ack(ctx, 0); |
| 1463 | else |
| 1464 | rc = gsm48_tx_gmm_det_ack_oldmsg(msg, 0); |
Jacob Erlbeck | b9ab0d4 | 2014-10-21 09:52:05 +0200 | [diff] [blame] | 1465 | } |
Harald Welte | bd5c912 | 2010-06-28 22:18:53 +0200 | [diff] [blame] | 1466 | |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 1467 | if (ctx) { |
| 1468 | struct sgsn_signal_data sig_data; |
| 1469 | memset(&sig_data, 0, sizeof(sig_data)); |
| 1470 | sig_data.mm = ctx; |
| 1471 | osmo_signal_dispatch(SS_SGSN, S_SGSN_DETACH, &sig_data); |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 1472 | mm_ctx_cleanup_free(ctx, "GPRS DETACH REQUEST"); |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 1473 | } |
Jacob Erlbeck | 258ce3d | 2014-09-30 13:51:45 +0200 | [diff] [blame] | 1474 | |
Harald Welte | bd5c912 | 2010-06-28 22:18:53 +0200 | [diff] [blame] | 1475 | return rc; |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1478 | /* Chapter 9.4.15: Routing area update accept */ |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 1479 | static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1480 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 1481 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 UPD ACK"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1482 | struct gsm48_hdr *gh; |
| 1483 | struct gsm48_ra_upd_ack *rua; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1484 | uint8_t *mid; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1485 | |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 1486 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_ROUTING_AREA_ACKED]); |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1487 | LOGMMCTXP(LOGL_INFO, mm, "<- ROUTING AREA UPDATE ACCEPT\n"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1488 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 1489 | mmctx2msgid(msg, mm); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1490 | |
| 1491 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 1492 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 1493 | gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK; |
| 1494 | |
| 1495 | rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua)); |
| 1496 | rua->force_stby = 0; /* not indicated */ |
| 1497 | rua->upd_result = 0; /* RA updated */ |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 1498 | rua->ra_upd_timer = gprs_secs_to_tmr_floor(sgsn->cfg.timers.T3312); |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 1499 | |
Harald Welte | 6463c07 | 2010-05-18 17:04:55 +0200 | [diff] [blame] | 1500 | gsm48_construct_ra(rua->ra_id.digits, &mm->ra); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1501 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1502 | #if 0 |
| 1503 | /* Optional: P-TMSI signature */ |
| 1504 | msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG); |
| 1505 | ptsig = msgb_put(msg, 3); |
| 1506 | ptsig[0] = mm->p_tmsi_sig >> 16; |
| 1507 | ptsig[1] = mm->p_tmsi_sig >> 8; |
| 1508 | ptsig[2] = mm->p_tmsi_sig & 0xff; |
| 1509 | #endif |
| 1510 | |
| 1511 | #ifdef PTMSI_ALLOC |
| 1512 | /* Optional: Allocated P-TMSI */ |
| 1513 | mid = msgb_put(msg, GSM48_MID_TMSI_LEN); |
| 1514 | gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi); |
| 1515 | mid[0] = GSM48_IE_GMM_ALLOC_PTMSI; |
| 1516 | #endif |
| 1517 | |
Jacob Erlbeck | 8de9c48 | 2015-01-23 11:44:39 +0100 | [diff] [blame] | 1518 | /* Optional: Negotiated READY timer value */ |
| 1519 | msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY, |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 1520 | gprs_secs_to_tmr_floor(sgsn->cfg.timers.T3314)); |
Jacob Erlbeck | 8de9c48 | 2015-01-23 11:44:39 +0100 | [diff] [blame] | 1521 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1522 | /* Option: MS ID, ... */ |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 1523 | return gsm48_gmm_sendmsg(msg, 0, mm, true); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | /* Chapter 9.4.17: Routing area update reject */ |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1527 | static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1528 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 1529 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 RA UPD REJ"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1530 | struct gsm48_hdr *gh; |
| 1531 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1532 | LOGP(DMM, LOGL_NOTICE, "<- ROUTING AREA UPDATE REJECT\n"); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 1533 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_ROUTING_AREA_REJECT]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1534 | |
Harald Welte | 11d7c10 | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 1535 | gmm_copy_id(msg, old_msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1536 | |
| 1537 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2); |
| 1538 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 1539 | gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ; |
| 1540 | gh->data[0] = cause; |
| 1541 | gh->data[1] = 0; /* ? */ |
| 1542 | |
| 1543 | /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */ |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 1544 | return gsm48_gmm_sendmsg(msg, 0, NULL, false); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1545 | } |
| 1546 | |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1547 | static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx, |
Holger Hans Peter Freyther | 2b17454 | 2011-10-14 23:31:34 +0200 | [diff] [blame] | 1548 | const uint8_t *pdp_status) |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1549 | { |
| 1550 | struct sgsn_pdp_ctx *pdp, *pdp2; |
| 1551 | /* 24.008 4.7.5.1.3: If the PDP context status information element is |
| 1552 | * included in ROUTING AREA UPDATE REQUEST message, then the network |
| 1553 | * shall deactivate all those PDP contexts locally (without peer to |
| 1554 | * peer signalling between the MS and the network), which are not in SM |
| 1555 | * state PDP-INACTIVE on network side but are indicated by the MS as |
| 1556 | * being in state PDP-INACTIVE. */ |
| 1557 | |
| 1558 | llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) { |
Harald Welte | cdf76cf | 2011-08-05 21:23:46 +0200 | [diff] [blame] | 1559 | if (pdp->nsapi < 8) { |
| 1560 | if (!(pdp_status[0] & (1 << pdp->nsapi))) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1561 | LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u " |
Harald Welte | cdf76cf | 2011-08-05 21:23:46 +0200 | [diff] [blame] | 1562 | "due to PDP CTX STATUS IE= 0x%02x%02x\n", |
| 1563 | pdp->nsapi, pdp_status[1], pdp_status[0]); |
| 1564 | sgsn_delete_pdp_ctx(pdp); |
| 1565 | } |
| 1566 | } else { |
| 1567 | if (!(pdp_status[1] & (1 << (pdp->nsapi - 8)))) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1568 | LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u " |
Harald Welte | cdf76cf | 2011-08-05 21:23:46 +0200 | [diff] [blame] | 1569 | "due to PDP CTX STATUS IE= 0x%02x%02x\n", |
| 1570 | pdp->nsapi, pdp_status[1], pdp_status[0]); |
| 1571 | sgsn_delete_pdp_ctx(pdp); |
| 1572 | } |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1577 | /* Chapter 9.4.14: Routing area update request */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1578 | static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg, |
| 1579 | struct gprs_llc_llme *llme) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1580 | { |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 1581 | #ifndef PTMSI_ALLOC |
| 1582 | struct sgsn_signal_data sig_data; |
| 1583 | #endif |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 1584 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1585 | uint8_t *cur = gh->data; |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1586 | uint8_t ms_ra_acc_cap_len; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1587 | struct gprs_ra_id old_ra_id; |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1588 | struct tlv_parsed tp; |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1589 | uint8_t upd_type; |
Philipp Maier | 6934a0f | 2017-01-25 12:19:08 +0100 | [diff] [blame] | 1590 | enum gsm48_gmm_cause reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC; |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1591 | int rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1592 | |
Daniel Willmann | 746c789 | 2016-01-25 16:49:53 +0100 | [diff] [blame] | 1593 | /* TODO: In iu mode - handle follow-on request */ |
| 1594 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1595 | /* Update Type 10.5.5.18 */ |
Daniel Willmann | 746c789 | 2016-01-25 16:49:53 +0100 | [diff] [blame] | 1596 | upd_type = *cur++ & 0x07; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1597 | |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 1598 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_GPRS_ROUTING_AREA_REQUEST]); |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1599 | LOGP(DMM, LOGL_INFO, "-> GMM RA UPDATE REQUEST type=\"%s\"\n", |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1600 | get_value_string(gprs_upd_t_strs, upd_type)); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1601 | |
| 1602 | /* Old routing area identification 10.5.5.15 */ |
| 1603 | gsm48_parse_ra(&old_ra_id, cur); |
| 1604 | cur += 6; |
| 1605 | |
| 1606 | /* MS Radio Access Capability 10.5.5.12a */ |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1607 | ms_ra_acc_cap_len = *cur++; |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1608 | if (ms_ra_acc_cap_len > 52) { |
| 1609 | reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 1610 | goto rejected; |
| 1611 | } |
Harald Welte | 4b2ed35 | 2011-07-27 23:35:38 +0200 | [diff] [blame] | 1612 | cur += ms_ra_acc_cap_len; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1613 | |
| 1614 | /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status, |
| 1615 | * DRX parameter, MS network capability */ |
Holger Hans Peter Freyther | 1768a57 | 2014-04-04 12:40:34 +0200 | [diff] [blame] | 1616 | tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur, |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1617 | (msg->data + msg->len) - cur, 0, 0); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1618 | |
| 1619 | switch (upd_type) { |
| 1620 | case GPRS_UPD_T_RA_LA: |
| 1621 | case GPRS_UPD_T_RA_LA_IMSI_ATT: |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1622 | LOGP(DMM, LOGL_NOTICE, "Update type %i unsupported in Mode III, is your SI13 corrupt?\n", upd_type); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1623 | reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC; |
| 1624 | goto rejected; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1625 | case GPRS_UPD_T_RA: |
| 1626 | case GPRS_UPD_T_PERIODIC: |
| 1627 | break; |
| 1628 | } |
| 1629 | |
Jacob Erlbeck | d35957a | 2016-01-04 18:43:36 +0100 | [diff] [blame] | 1630 | if (!mmctx) { |
| 1631 | /* BSSGP doesn't give us an mmctx */ |
| 1632 | |
| 1633 | /* TODO: Check if there is an MM CTX with old_ra_id and |
| 1634 | * the P-TMSI (if given, reguired for UMTS) or as last resort |
| 1635 | * if the TLLI matches foreign_tlli (P-TMSI). Note that this |
| 1636 | * is an optimization to avoid the RA reject (impl detached) |
| 1637 | * below, which will cause a new attach cycle. */ |
Jacob Erlbeck | 5ac4aad | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 1638 | /* Look-up the MM context based on old RA-ID and TLLI */ |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 1639 | /* In Iu mode, msg->dst contains the ranap_ue_conn_ctx pointer, in Gb |
Daniel Willmann | 61329d4 | 2016-05-21 17:36:18 +0200 | [diff] [blame] | 1640 | * mode dst is empty. */ |
| 1641 | /* FIXME: have a more explicit indicator for Iu messages */ |
| 1642 | if (!msg->dst) { |
| 1643 | mmctx = sgsn_mm_ctx_by_tlli_and_ptmsi(msgb_tlli(msg), &old_ra_id); |
| 1644 | } else if (TLVP_PRESENT(&tp, GSM48_IE_GMM_ALLOC_PTMSI)) { |
| 1645 | #ifdef BUILD_IU |
| 1646 | /* In Iu mode search only for ptmsi */ |
| 1647 | char mi_string[GSM48_MI_SIZE]; |
| 1648 | uint8_t mi_len = TLVP_LEN(&tp, GSM48_IE_GMM_ALLOC_PTMSI); |
| 1649 | uint8_t *mi = TLVP_VAL(&tp, GSM48_IE_GMM_ALLOC_PTMSI); |
| 1650 | uint8_t mi_type = *mi & GSM_MI_TYPE_MASK; |
| 1651 | uint32_t tmsi; |
| 1652 | |
| 1653 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 1654 | |
| 1655 | if (mi_type == GSM_MI_TYPE_TMSI) { |
| 1656 | memcpy(&tmsi, mi+1, 4); |
| 1657 | tmsi = ntohl(tmsi); |
| 1658 | mmctx = sgsn_mm_ctx_by_ptmsi(tmsi); |
| 1659 | } |
| 1660 | #else |
| 1661 | goto rejected; |
| 1662 | #endif |
| 1663 | } |
Jacob Erlbeck | 5ac4aad | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 1664 | if (mmctx) { |
| 1665 | LOGMMCTXP(LOGL_INFO, mmctx, |
| 1666 | "Looked up by matching TLLI and P_TMSI. " |
| 1667 | "BSSGP TLLI: %08x, P-TMSI: %08x (%08x), " |
| 1668 | "TLLI: %08x (%08x), RA: %d-%d-%d-%d\n", |
| 1669 | msgb_tlli(msg), |
| 1670 | mmctx->p_tmsi, mmctx->p_tmsi_old, |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 1671 | mmctx->gb.tlli, mmctx->gb.tlli_new, |
Jacob Erlbeck | 5ac4aad | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 1672 | mmctx->ra.mcc, mmctx->ra.mnc, |
| 1673 | mmctx->ra.lac, mmctx->ra.rac); |
Jacob Erlbeck | d35957a | 2016-01-04 18:43:36 +0100 | [diff] [blame] | 1674 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1675 | mmctx->gmm_state = GMM_COMMON_PROC_INIT; |
Jacob Erlbeck | 5ac4aad | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 1676 | } |
| 1677 | } else if (!gprs_ra_id_equals(&mmctx->ra, &old_ra_id) || |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1678 | mmctx->gmm_state == GMM_DEREGISTERED) |
Jacob Erlbeck | d35957a | 2016-01-04 18:43:36 +0100 | [diff] [blame] | 1679 | { |
| 1680 | /* We cannot use the mmctx */ |
Jacob Erlbeck | 5ac4aad | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 1681 | LOGMMCTXP(LOGL_INFO, mmctx, |
| 1682 | "The MM context cannot be used, RA: %d-%d-%d-%d\n", |
| 1683 | mmctx->ra.mcc, mmctx->ra.mnc, |
| 1684 | mmctx->ra.lac, mmctx->ra.rac); |
| 1685 | mmctx = NULL; |
| 1686 | } |
Jacob Erlbeck | d35957a | 2016-01-04 18:43:36 +0100 | [diff] [blame] | 1687 | |
Jacob Erlbeck | 5ac4aad | 2016-01-04 18:43:38 +0100 | [diff] [blame] | 1688 | if (!mmctx) { |
Harald Welte | 2b2429e | 2015-12-25 22:31:36 +0100 | [diff] [blame] | 1689 | if (llme) { |
| 1690 | /* send a XID reset to re-set all LLC sequence numbers |
| 1691 | * in the MS */ |
| 1692 | LOGMMCTXP(LOGL_NOTICE, mmctx, "LLC XID RESET\n"); |
| 1693 | gprs_llgmm_reset(llme); |
| 1694 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1695 | /* The MS has to perform GPRS attach */ |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1696 | /* Device is still IMSI attached for CS but initiate GPRS ATTACH, |
| 1697 | * see GSM 04.08, 4.7.5.1.4 and G.6 */ |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1698 | reject_cause = GMM_CAUSE_IMPL_DETACHED; |
| 1699 | goto rejected; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1700 | } |
| 1701 | |
Harald Welte | f0901f0 | 2010-12-26 10:39:26 +0100 | [diff] [blame] | 1702 | /* Store new BVCI/NSEI in MM context (FIXME: delay until we ack?) */ |
| 1703 | msgid2mmctx(mmctx, msg); |
| 1704 | /* Bump the statistics of received signalling msgs for this MM context */ |
| 1705 | rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]); |
| 1706 | |
Harald Welte | 9f1f3ad | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 1707 | /* Update the MM context with the new RA-ID */ |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1708 | if (mmctx->ran_type == MM_CTX_T_GERAN_Gb) { |
Daniel Willmann | f9f4387 | 2016-05-20 22:36:23 +0200 | [diff] [blame] | 1709 | bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg)); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1710 | /* Update the MM context with the new (i.e. foreign) TLLI */ |
| 1711 | mmctx->gb.tlli = msgb_tlli(msg); |
| 1712 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1713 | /* FIXME: Update the MM context with the MS radio acc capabilities */ |
| 1714 | /* FIXME: Update the MM context with the MS network capabilities */ |
| 1715 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 1716 | rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]); |
| 1717 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1718 | #ifdef PTMSI_ALLOC |
Jacob Erlbeck | f6e7d99 | 2014-11-06 15:43:10 +0100 | [diff] [blame] | 1719 | /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */ |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1720 | if (mmctx->gmm_state != GMM_COMMON_PROC_INIT) { |
Jacob Erlbeck | f6e7d99 | 2014-11-06 15:43:10 +0100 | [diff] [blame] | 1721 | mmctx->p_tmsi_old = mmctx->p_tmsi; |
| 1722 | mmctx->p_tmsi = sgsn_alloc_ptmsi(); |
| 1723 | } |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1724 | /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */ |
| 1725 | mmctx->t3350_mode = GMM_T3350_MODE_RAU; |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 1726 | mmctx_timer_start(mmctx, 3350, sgsn->cfg.timers.T3350); |
Jacob Erlbeck | 0074a77 | 2014-10-28 16:23:46 +0100 | [diff] [blame] | 1727 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1728 | mmctx->gmm_state = GMM_COMMON_PROC_INIT; |
Jacob Erlbeck | 0074a77 | 2014-10-28 16:23:46 +0100 | [diff] [blame] | 1729 | #else |
| 1730 | /* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */ |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1731 | mmctx->gmm_state = GMM_REGISTERED_NORMAL; |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 1732 | |
| 1733 | memset(&sig_data, 0, sizeof(sig_data)); |
| 1734 | sig_data.mm = mmctx; |
| 1735 | osmo_signal_dispatch(SS_SGSN, S_SGSN_UPDATE, &sig_data); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1736 | #endif |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1737 | if (mmctx->ran_type == MM_CTX_T_GERAN_Gb) { |
| 1738 | /* Even if there is no P-TMSI allocated, the MS will switch from |
| 1739 | * foreign TLLI to local TLLI */ |
| 1740 | mmctx->gb.tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1741 | |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1742 | /* Inform LLC layer about new TLLI but keep old active */ |
| 1743 | gprs_llgmm_assign(mmctx->gb.llme, mmctx->gb.tlli, |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 1744 | mmctx->gb.tlli_new); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1745 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1746 | |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1747 | /* Look at PDP Context Status IE and see if MS's view of |
| 1748 | * activated/deactivated NSAPIs agrees with our view */ |
| 1749 | if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) { |
Holger Hans Peter Freyther | 2b17454 | 2011-10-14 23:31:34 +0200 | [diff] [blame] | 1750 | const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS); |
Harald Welte | 588d44e | 2010-06-09 10:28:29 +0200 | [diff] [blame] | 1751 | process_ms_ctx_status(mmctx, pdp_status); |
| 1752 | } |
| 1753 | |
Daniel Willmann | af24172 | 2016-05-21 00:35:41 +0200 | [diff] [blame] | 1754 | /* Send RA UPDATE ACCEPT. In Iu, the RA upd request can be called from |
| 1755 | * a new Iu connection, so we might need to re-authenticate the |
| 1756 | * connection as well as turn on integrity protection. */ |
| 1757 | mmctx->pending_req = GSM48_MT_GMM_RA_UPD_REQ; |
| 1758 | return gsm48_gmm_authorize(mmctx); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1759 | |
| 1760 | rejected: |
| 1761 | /* Send RA UPDATE REJECT */ |
| 1762 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 1763 | "Rejecting RA Update Request with cause '%s' (%d)\n", |
Jacob Erlbeck | afcf230 | 2015-01-05 17:30:02 +0100 | [diff] [blame] | 1764 | get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1765 | rc = gsm48_tx_gmm_ra_upd_rej(msg, reject_cause); |
| 1766 | if (mmctx) |
| 1767 | mm_ctx_cleanup_free(mmctx, "GPRS RA UPDATE REJ"); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1768 | else { |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1769 | if (llme) |
| 1770 | gprs_llgmm_unassign(llme); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1771 | } |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1772 | |
| 1773 | return rc; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1774 | } |
| 1775 | |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1776 | /* 3GPP TS 24.008 Section 9.4.20 Service request. |
| 1777 | * In Iu, a UE in PMM-IDLE mode can use GSM48_MT_GMM_SERVICE_REQ to switch back |
| 1778 | * to PMM-CONNECTED mode. */ |
| 1779 | static int gsm48_rx_gmm_service_req(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
| 1780 | { |
| 1781 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
| 1782 | uint8_t *cur = gh->data, *mi; |
| 1783 | uint8_t ciph_seq_nr, service_type, mi_len, mi_type; |
| 1784 | uint32_t tmsi; |
| 1785 | struct tlv_parsed tp; |
| 1786 | char mi_string[GSM48_MI_SIZE]; |
| 1787 | enum gsm48_gmm_cause reject_cause; |
| 1788 | int rc; |
| 1789 | |
| 1790 | LOGMMCTXP(LOGL_INFO, ctx, "-> GMM SERVICE REQUEST "); |
| 1791 | |
| 1792 | /* This message is only valid in Iu mode */ |
| 1793 | if (!msg->dst) { |
| 1794 | LOGPC(DMM, LOGL_INFO, "Invalid if not in Iu mode\n"); |
| 1795 | return -1; |
| 1796 | } |
| 1797 | |
| 1798 | /* Skip Ciphering key sequence number 10.5.1.2 */ |
| 1799 | ciph_seq_nr = *cur & 0x07; |
| 1800 | |
| 1801 | /* Service type 10.5.5.20 */ |
| 1802 | service_type = (*cur++ >> 4) & 0x07; |
| 1803 | |
| 1804 | /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */ |
| 1805 | mi_len = *cur++; |
| 1806 | mi = cur; |
| 1807 | if (mi_len > 8) |
| 1808 | goto err_inval; |
| 1809 | mi_type = *mi & GSM_MI_TYPE_MASK; |
| 1810 | cur += mi_len; |
| 1811 | |
| 1812 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 1813 | |
| 1814 | DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, |
| 1815 | get_value_string(gprs_service_t_strs, service_type)); |
| 1816 | |
| 1817 | LOGPC(DMM, LOGL_INFO, "\n"); |
| 1818 | |
| 1819 | /* Optional: PDP context status, MBMS context status, Uplink data status, Device properties */ |
| 1820 | tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur, (msg->data + msg->len) - cur, 0, 0); |
| 1821 | |
| 1822 | switch (mi_type) { |
| 1823 | case GSM_MI_TYPE_IMSI: |
| 1824 | /* Try to find MM context based on IMSI */ |
| 1825 | if (!ctx) |
| 1826 | ctx = sgsn_mm_ctx_by_imsi(mi_string); |
| 1827 | if (!ctx) { |
| 1828 | /* FIXME: We need to have a context for service request? */ |
| 1829 | reject_cause = GMM_CAUSE_NET_FAIL; |
| 1830 | goto rejected; |
| 1831 | } |
| 1832 | msgid2mmctx(ctx, msg); |
| 1833 | break; |
| 1834 | case GSM_MI_TYPE_TMSI: |
| 1835 | memcpy(&tmsi, mi+1, 4); |
| 1836 | tmsi = ntohl(tmsi); |
| 1837 | /* Try to find MM context based on P-TMSI */ |
| 1838 | if (!ctx) |
| 1839 | ctx = sgsn_mm_ctx_by_ptmsi(tmsi); |
| 1840 | if (!ctx) { |
| 1841 | /* FIXME: We need to have a context for service request? */ |
| 1842 | reject_cause = GMM_CAUSE_NET_FAIL; |
| 1843 | goto rejected; |
| 1844 | } |
| 1845 | msgid2mmctx(ctx, msg); |
| 1846 | break; |
| 1847 | default: |
| 1848 | LOGMMCTXP(LOGL_NOTICE, ctx, "Rejecting SERVICE REQUEST with " |
| 1849 | "MI type %s\n", gsm48_mi_type_name(mi_type)); |
| 1850 | reject_cause = GMM_CAUSE_MS_ID_NOT_DERIVED; |
| 1851 | goto rejected; |
| 1852 | } |
| 1853 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 1854 | ctx->gmm_state = GMM_COMMON_PROC_INIT; |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1855 | |
| 1856 | ctx->iu.service.type = service_type; |
| 1857 | |
| 1858 | /* TODO: Handle those only in case of accept? */ |
| 1859 | /* Look at PDP Context Status IE and see if MS's view of |
| 1860 | * activated/deactivated NSAPIs agrees with our view */ |
| 1861 | if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) { |
| 1862 | const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS); |
| 1863 | process_ms_ctx_status(ctx, pdp_status); |
| 1864 | } |
| 1865 | |
| 1866 | |
| 1867 | ctx->pending_req = GSM48_MT_GMM_SERVICE_REQ; |
| 1868 | return gsm48_gmm_authorize(ctx); |
| 1869 | |
| 1870 | err_inval: |
| 1871 | LOGPC(DMM, LOGL_INFO, "\n"); |
| 1872 | reject_cause = GMM_CAUSE_SEM_INCORR_MSG; |
| 1873 | |
| 1874 | rejected: |
| 1875 | /* Send SERVICE REJECT */ |
| 1876 | LOGMMCTXP(LOGL_NOTICE, ctx, |
| 1877 | "Rejecting Service Request with cause '%s' (%d)\n", |
| 1878 | get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause); |
| 1879 | rc = gsm48_tx_gmm_service_rej_oldmsg(msg, reject_cause); |
| 1880 | |
| 1881 | return rc; |
| 1882 | |
| 1883 | } |
| 1884 | |
| 1885 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1886 | static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1887 | { |
| 1888 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 1889 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1890 | LOGMMCTXP(LOGL_INFO, mmctx, "-> GPRS MM STATUS (cause: %s)\n", |
Jacob Erlbeck | afcf230 | 2015-01-05 17:30:02 +0100 | [diff] [blame] | 1891 | get_value_string(gsm48_gmm_cause_names, gh->data[0])); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1892 | |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | /* GPRS Mobility Management */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1897 | static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg, |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 1898 | struct gprs_llc_llme *llme, bool drop_cipherable) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1899 | { |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 1900 | struct sgsn_signal_data sig_data; |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 1901 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1902 | int rc; |
| 1903 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1904 | /* MMCTX can be NULL when called */ |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 1905 | if (drop_cipherable && gsm48_hdr_gmm_cipherable(gh)) { |
| 1906 | LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping cleartext GMM %s which " |
| 1907 | "is expected to be encrypted for TLLI 0x%08x\n", |
| 1908 | get_value_string(gprs_msgt_gmm_names, gh->msg_type), |
| 1909 | llme->tlli); |
| 1910 | return -EBADMSG; |
| 1911 | } |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1912 | |
Harald Welte | 2b2429e | 2015-12-25 22:31:36 +0100 | [diff] [blame] | 1913 | if (llme && !mmctx && |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1914 | gh->msg_type != GSM48_MT_GMM_ATTACH_REQ && |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1915 | gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) { |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1916 | LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n"); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1917 | /* 4.7.10 */ |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 1918 | if (gh->msg_type == GSM48_MT_GMM_STATUS) { |
| 1919 | /* TLLI unassignment */ |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1920 | gprs_llgmm_unassign(llme); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1921 | return 0; |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 1922 | } |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1923 | |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1924 | /* Don't reply or establish a LLME on DETACH_ACK */ |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1925 | if (gh->msg_type == GSM48_MT_GMM_DETACH_ACK) |
| 1926 | return gprs_llgmm_unassign(llme); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1927 | |
Jacob Erlbeck | c4f9bf3 | 2014-11-11 08:55:13 +0100 | [diff] [blame] | 1928 | gprs_llgmm_reset(llme); |
| 1929 | |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1930 | /* Don't force it into re-attachment */ |
| 1931 | if (gh->msg_type == GSM48_MT_GMM_DETACH_REQ) { |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 1932 | /* Handle Detach Request */ |
| 1933 | rc = gsm48_rx_gmm_det_req(NULL, msg); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1934 | |
| 1935 | /* TLLI unassignment */ |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1936 | gprs_llgmm_unassign(llme); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1937 | return rc; |
| 1938 | } |
| 1939 | |
| 1940 | /* Force the MS to re-attach */ |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 1941 | rc = gsm0408_gprs_force_reattach_oldmsg(msg, llme); |
Jacob Erlbeck | b1c074f | 2014-10-31 12:27:11 +0100 | [diff] [blame] | 1942 | |
| 1943 | /* TLLI unassignment */ |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 1944 | gprs_llgmm_unassign(llme); |
Jacob Erlbeck | b1c074f | 2014-10-31 12:27:11 +0100 | [diff] [blame] | 1945 | return rc; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1946 | } |
| 1947 | |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 1948 | /* |
| 1949 | * For a few messages, mmctx may be NULL. For most, we want to ensure a |
| 1950 | * non-NULL mmctx. At the same time, we want to keep the message |
| 1951 | * validity check intact, so that all message types appear in the |
| 1952 | * switch statement and the default case thus means "unknown message". |
| 1953 | * If we split the switch in two parts to check non-NULL halfway, the |
| 1954 | * unknown-message check breaks, or we'd need to duplicate the switch |
| 1955 | * cases in both parts. Just keep one large switch and add some gotos. |
| 1956 | */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1957 | switch (gh->msg_type) { |
| 1958 | case GSM48_MT_GMM_RA_UPD_REQ: |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1959 | rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1960 | break; |
| 1961 | case GSM48_MT_GMM_ATTACH_REQ: |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 1962 | rc = gsm48_rx_gmm_att_req(mmctx, msg, llme); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1963 | break; |
Daniel Willmann | 1dee2b6 | 2016-05-21 00:15:29 +0200 | [diff] [blame] | 1964 | case GSM48_MT_GMM_SERVICE_REQ: |
| 1965 | rc = gsm48_rx_gmm_service_req(mmctx, msg); |
| 1966 | break; |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1967 | /* For all the following types mmctx can not be NULL */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1968 | case GSM48_MT_GMM_ID_RESP: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 1969 | if (!mmctx) |
| 1970 | goto null_mmctx; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1971 | rc = gsm48_rx_gmm_id_resp(mmctx, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1972 | break; |
| 1973 | case GSM48_MT_GMM_STATUS: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 1974 | if (!mmctx) |
| 1975 | goto null_mmctx; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1976 | rc = gsm48_rx_gmm_status(mmctx, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1977 | break; |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1978 | case GSM48_MT_GMM_DETACH_REQ: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 1979 | if (!mmctx) |
| 1980 | goto null_mmctx; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 1981 | rc = gsm48_rx_gmm_det_req(mmctx, msg); |
Harald Welte | 2720e73 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 1982 | break; |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1983 | case GSM48_MT_GMM_DETACH_ACK: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 1984 | if (!mmctx) |
| 1985 | goto null_mmctx; |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 1986 | LOGMMCTXP(LOGL_INFO, mmctx, "-> DETACH ACK\n"); |
| 1987 | mm_ctx_cleanup_free(mmctx, "GPRS DETACH ACK"); |
| 1988 | rc = 0; |
| 1989 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1990 | case GSM48_MT_GMM_ATTACH_COMPL: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 1991 | if (!mmctx) |
| 1992 | goto null_mmctx; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1993 | /* only in case SGSN offered new P-TMSI */ |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 1994 | LOGMMCTXP(LOGL_INFO, mmctx, "-> ATTACH COMPLETE\n"); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1995 | mmctx_timer_stop(mmctx, 3350); |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1996 | mmctx->t3350_mode = GMM_T3350_MODE_NONE; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 1997 | mmctx->p_tmsi_old = 0; |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 1998 | mmctx->pending_req = 0; |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 1999 | if (mmctx->ran_type == MM_CTX_T_GERAN_Gb) { |
| 2000 | /* Unassign the old TLLI */ |
| 2001 | mmctx->gb.tlli = mmctx->gb.tlli_new; |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 2002 | gprs_llme_copy_key(mmctx, mmctx->gb.llme); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 2003 | gprs_llgmm_assign(mmctx->gb.llme, 0xffffffff, |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 2004 | mmctx->gb.tlli_new); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 2005 | } |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2006 | mmctx->gmm_state = GMM_REGISTERED_NORMAL; |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 2007 | mmctx_set_pmm_state(mmctx, PMM_CONNECTED); |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2008 | mmctx_set_mm_state(mmctx, MM_READY); |
Holger Hans Peter Freyther | 2b17454 | 2011-10-14 23:31:34 +0200 | [diff] [blame] | 2009 | rc = 0; |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 2010 | |
| 2011 | memset(&sig_data, 0, sizeof(sig_data)); |
| 2012 | sig_data.mm = mmctx; |
| 2013 | osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2014 | break; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 2015 | case GSM48_MT_GMM_RA_UPD_COMPL: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 2016 | if (!mmctx) |
| 2017 | goto null_mmctx; |
Harald Welte | ab1d562 | 2010-05-18 19:58:38 +0200 | [diff] [blame] | 2018 | /* only in case SGSN offered new P-TMSI */ |
Harald Welte | ecff242 | 2015-08-16 18:14:33 +0200 | [diff] [blame] | 2019 | LOGMMCTXP(LOGL_INFO, mmctx, "-> ROUTING AREA UPDATE COMPLETE\n"); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2020 | mmctx_timer_stop(mmctx, 3350); |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 2021 | mmctx->t3350_mode = GMM_T3350_MODE_NONE; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2022 | mmctx->p_tmsi_old = 0; |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 2023 | mmctx->pending_req = 0; |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 2024 | if (mmctx->ran_type == MM_CTX_T_GERAN_Gb) { |
| 2025 | /* Unassign the old TLLI */ |
| 2026 | mmctx->gb.tlli = mmctx->gb.tlli_new; |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 2027 | gprs_llgmm_assign(mmctx->gb.llme, 0xffffffff, |
| 2028 | mmctx->gb.tlli_new); |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 2029 | } |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2030 | mmctx->gmm_state = GMM_REGISTERED_NORMAL; |
Daniel Willmann | c17cdb4 | 2016-05-11 12:42:54 +0200 | [diff] [blame] | 2031 | mmctx_set_pmm_state(mmctx, PMM_CONNECTED); |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2032 | mmctx_set_mm_state(mmctx, MM_READY); |
Holger Hans Peter Freyther | 2b17454 | 2011-10-14 23:31:34 +0200 | [diff] [blame] | 2033 | rc = 0; |
Holger Hans Peter Freyther | b100895 | 2015-05-02 19:55:38 +0200 | [diff] [blame] | 2034 | |
| 2035 | memset(&sig_data, 0, sizeof(sig_data)); |
| 2036 | sig_data.mm = mmctx; |
| 2037 | osmo_signal_dispatch(SS_SGSN, S_SGSN_UPDATE, &sig_data); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2038 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2039 | case GSM48_MT_GMM_PTMSI_REALL_COMPL: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 2040 | if (!mmctx) |
| 2041 | goto null_mmctx; |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2042 | LOGMMCTXP(LOGL_INFO, mmctx, "-> PTMSI REALLLICATION COMPLETE\n"); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2043 | mmctx_timer_stop(mmctx, 3350); |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 2044 | mmctx->t3350_mode = GMM_T3350_MODE_NONE; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2045 | mmctx->p_tmsi_old = 0; |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 2046 | mmctx->pending_req = 0; |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 2047 | if (mmctx->ran_type == MM_CTX_T_GERAN_Gb) { |
| 2048 | /* Unassign the old TLLI */ |
| 2049 | mmctx->gb.tlli = mmctx->gb.tlli_new; |
| 2050 | //gprs_llgmm_assign(mmctx->gb.llme, 0xffffffff, mmctx->gb.tlli_new, GPRS_ALGO_GEA0, NULL); |
| 2051 | } |
Holger Hans Peter Freyther | 2b17454 | 2011-10-14 23:31:34 +0200 | [diff] [blame] | 2052 | rc = 0; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2053 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2054 | case GSM48_MT_GMM_AUTH_CIPH_RESP: |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 2055 | if (!mmctx) |
| 2056 | goto null_mmctx; |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 2057 | rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2058 | break; |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 2059 | case GSM48_MT_GMM_AUTH_CIPH_FAIL: |
| 2060 | rc = gsm48_rx_gmm_auth_ciph_fail(mmctx, msg); |
| 2061 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2062 | default: |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2063 | LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GMM msg type 0x%02x\n", |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2064 | gh->msg_type); |
Neels Hofmeyr | 9329e6f | 2016-06-03 00:44:23 +0200 | [diff] [blame] | 2065 | if (mmctx) |
| 2066 | rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL); |
| 2067 | else |
| 2068 | rc = -EINVAL; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2069 | break; |
| 2070 | } |
| 2071 | |
| 2072 | return rc; |
Neels Hofmeyr | e98ba82 | 2016-05-22 14:28:19 +0200 | [diff] [blame] | 2073 | |
| 2074 | null_mmctx: |
| 2075 | LOGP(DMM, LOGL_ERROR, |
| 2076 | "Received GSM 04.08 message type 0x%02x," |
| 2077 | " but no MM context available\n", |
| 2078 | gh->msg_type); |
| 2079 | return -EINVAL; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2080 | } |
| 2081 | |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2082 | static void mmctx_timer_cb(void *_mm) |
| 2083 | { |
| 2084 | struct sgsn_mm_ctx *mm = _mm; |
Jacob Erlbeck | bd0cf11 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 2085 | struct gsm_auth_tuple *at; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2086 | |
| 2087 | mm->num_T_exp++; |
| 2088 | |
| 2089 | switch (mm->T) { |
| 2090 | case 3350: /* waiting for ATTACH COMPLETE */ |
| 2091 | if (mm->num_T_exp >= 5) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2092 | LOGMMCTXP(LOGL_NOTICE, mm, "T3350 expired >= 5 times\n"); |
Jacob Erlbeck | a790456 | 2014-11-03 10:12:52 +0100 | [diff] [blame] | 2093 | mm_ctx_cleanup_free(mm, "T3350"); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2094 | /* FIXME: should we return some error? */ |
| 2095 | break; |
| 2096 | } |
| 2097 | /* re-transmit the respective msg and re-start timer */ |
| 2098 | switch (mm->t3350_mode) { |
| 2099 | case GMM_T3350_MODE_ATT: |
| 2100 | gsm48_tx_gmm_att_ack(mm); |
| 2101 | break; |
| 2102 | case GMM_T3350_MODE_RAU: |
| 2103 | gsm48_tx_gmm_ra_upd_ack(mm); |
| 2104 | break; |
| 2105 | case GMM_T3350_MODE_PTMSI_REALL: |
| 2106 | /* FIXME */ |
| 2107 | break; |
Jacob Erlbeck | 93eae8e | 2014-10-28 12:23:29 +0100 | [diff] [blame] | 2108 | case GMM_T3350_MODE_NONE: |
| 2109 | LOGMMCTXP(LOGL_NOTICE, mm, |
| 2110 | "T3350 mode wasn't set, ignoring timeout\n"); |
| 2111 | break; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2112 | } |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 2113 | osmo_timer_schedule(&mm->timer, sgsn->cfg.timers.T3350, 0); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2114 | break; |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 2115 | case 3360: /* waiting for AUTH AND CIPH RESP */ |
| 2116 | if (mm->num_T_exp >= 5) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2117 | LOGMMCTXP(LOGL_NOTICE, mm, "T3360 expired >= 5 times\n"); |
Jacob Erlbeck | a790456 | 2014-11-03 10:12:52 +0100 | [diff] [blame] | 2118 | mm_ctx_cleanup_free(mm, "T3360"); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 2119 | break; |
| 2120 | } |
Jacob Erlbeck | bd0cf11 | 2014-12-01 12:33:33 +0100 | [diff] [blame] | 2121 | /* Re-transmit the respective msg and re-start timer */ |
| 2122 | if (mm->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) { |
| 2123 | LOGMMCTXP(LOGL_ERROR, mm, |
| 2124 | "timeout: invalid auth triplet reference\n"); |
| 2125 | mm_ctx_cleanup_free(mm, "T3360"); |
| 2126 | break; |
| 2127 | } |
| 2128 | at = &mm->auth_triplet; |
| 2129 | |
Neels Hofmeyr | 058cd57 | 2017-02-24 06:24:45 +0100 | [diff] [blame] | 2130 | gsm48_tx_gmm_auth_ciph_req(mm, &at->vec, at->key_seq, false); |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 2131 | osmo_timer_schedule(&mm->timer, sgsn->cfg.timers.T3360, 0); |
Harald Welte | 4b6ac1e | 2010-07-03 11:09:06 +0200 | [diff] [blame] | 2132 | break; |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2133 | case 3370: /* waiting for IDENTITY RESPONSE */ |
| 2134 | if (mm->num_T_exp >= 5) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2135 | LOGMMCTXP(LOGL_NOTICE, mm, "T3370 expired >= 5 times\n"); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2136 | gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED); |
Jacob Erlbeck | a790456 | 2014-11-03 10:12:52 +0100 | [diff] [blame] | 2137 | mm_ctx_cleanup_free(mm, "GPRS ATTACH REJECT (T3370)"); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2138 | break; |
| 2139 | } |
| 2140 | /* re-tranmit IDENTITY REQUEST and re-start timer */ |
| 2141 | gsm48_tx_gmm_id_req(mm, mm->t3370_id_type); |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 2142 | osmo_timer_schedule(&mm->timer, sgsn->cfg.timers.T3370, 0); |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2143 | break; |
| 2144 | default: |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2145 | LOGMMCTXP(LOGL_ERROR, mm, "timer expired in unknown mode %u\n", |
Harald Welte | c2e8cc4 | 2010-05-31 20:23:38 +0200 | [diff] [blame] | 2146 | mm->T); |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | /* GPRS SESSION MANAGEMENT */ |
| 2151 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2152 | static void pdpctx_timer_cb(void *_mm); |
| 2153 | |
| 2154 | static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T, |
| 2155 | unsigned int seconds) |
| 2156 | { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 2157 | if (osmo_timer_pending(&pdp->timer)) |
Pravin Kumarvel | 26f6ced | 2016-11-17 12:19:01 +0530 | [diff] [blame] | 2158 | LOGPDPCTXP(LOGL_ERROR, pdp, "Starting PDP timer %u while old " |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2159 | "timer %u pending\n", T, pdp->T); |
| 2160 | pdp->T = T; |
| 2161 | pdp->num_T_exp = 0; |
| 2162 | |
| 2163 | /* FIXME: we should do this only once ? */ |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 2164 | osmo_timer_setup(&pdp->timer, pdpctx_timer_cb, pdp); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 2165 | osmo_timer_schedule(&pdp->timer, seconds, 0); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2166 | } |
| 2167 | |
Pravin Kumarvel | 16606c9 | 2016-11-17 12:20:01 +0530 | [diff] [blame] | 2168 | static void pdpctx_timer_stop(struct sgsn_pdp_ctx *pdp, unsigned int T) |
| 2169 | { |
| 2170 | if (pdp->T != T) |
| 2171 | LOGPDPCTXP(LOGL_ERROR, pdp, "Stopping PDP timer %u but " |
| 2172 | "%u is running\n", T, pdp->T); |
| 2173 | osmo_timer_del(&pdp->timer); |
| 2174 | } |
| 2175 | |
Holger Hans Peter Freyther | 1768a57 | 2014-04-04 12:40:34 +0200 | [diff] [blame] | 2176 | #if 0 |
Harald Welte | 421cba4 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 2177 | static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr) |
| 2178 | { |
| 2179 | uint8_t v[6]; |
| 2180 | |
| 2181 | v[0] = PDP_TYPE_ORG_IETF; |
| 2182 | v[1] = PDP_TYPE_N_IETF_IPv4; |
| 2183 | *(uint32_t *)(v+2) = htonl(ipaddr); |
| 2184 | |
| 2185 | msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v); |
| 2186 | } |
| 2187 | |
| 2188 | static void msgb_put_pdp_addr_ppp(struct msgb *msg) |
| 2189 | { |
| 2190 | uint8_t v[2]; |
| 2191 | |
| 2192 | v[0] = PDP_TYPE_ORG_ETSI; |
| 2193 | v[1] = PDP_TYPE_N_ETSI_PPP; |
| 2194 | |
| 2195 | msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v); |
| 2196 | } |
Holger Hans Peter Freyther | 1768a57 | 2014-04-04 12:40:34 +0200 | [diff] [blame] | 2197 | #endif |
Harald Welte | 421cba4 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 2198 | |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2199 | /* Section 9.5.2: Activate PDP Context Accept */ |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2200 | int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2201 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 2202 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP ACC"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2203 | struct gsm48_hdr *gh; |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2204 | uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2205 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2206 | LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n"); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2207 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_ACTIVATE_ACCEPT]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2208 | |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2209 | mmctx2msgid(msg, pdp->mm); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2210 | |
| 2211 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2212 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 2213 | gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK; |
Harald Welte | 421cba4 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 2214 | |
| 2215 | /* Negotiated LLC SAPI */ |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2216 | msgb_v_put(msg, pdp->sapi); |
Harald Welte | 4e38ac7 | 2010-05-18 14:51:18 +0200 | [diff] [blame] | 2217 | |
Harald Welte | 55e0df7 | 2010-05-18 13:20:08 +0200 | [diff] [blame] | 2218 | /* FIXME: copy QoS parameters from original request */ |
| 2219 | //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v); |
| 2220 | msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos); |
Harald Welte | 4e38ac7 | 2010-05-18 14:51:18 +0200 | [diff] [blame] | 2221 | |
Harald Welte | 421cba4 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 2222 | /* Radio priority 10.5.7.2 */ |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2223 | msgb_v_put(msg, pdp->lib->radio_pri); |
Harald Welte | 4e38ac7 | 2010-05-18 14:51:18 +0200 | [diff] [blame] | 2224 | |
Harald Welte | 421cba4 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 2225 | /* PDP address */ |
Harald Welte | 4e38ac7 | 2010-05-18 14:51:18 +0200 | [diff] [blame] | 2226 | /* Highest 4 bits of first byte need to be set to 1, otherwise |
| 2227 | * the IE is identical with the 04.08 PDP Address IE */ |
| 2228 | pdp->lib->eua.v[0] &= ~0xf0; |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2229 | msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, |
| 2230 | pdp->lib->eua.l, pdp->lib->eua.v); |
Harald Welte | 4e38ac7 | 2010-05-18 14:51:18 +0200 | [diff] [blame] | 2231 | pdp->lib->eua.v[0] |= 0xf0; |
| 2232 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2233 | /* Optional: Protocol configuration options (FIXME: why 'req') */ |
Holger Hans Peter Freyther | d219507 | 2014-04-04 11:55:21 +0200 | [diff] [blame] | 2234 | if (pdp->lib->pco_req.l) |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2235 | msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, |
Harald Welte | 3c0b9b9 | 2010-05-18 14:36:11 +0200 | [diff] [blame] | 2236 | pdp->lib->pco_req.l, pdp->lib->pco_req.v); |
Harald Welte | 4e38ac7 | 2010-05-18 14:51:18 +0200 | [diff] [blame] | 2237 | |
Harald Welte | 421cba4 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 2238 | /* Optional: Packet Flow Identifier */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2239 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 2240 | return gsm48_gmm_sendmsg(msg, 0, pdp->mm, true); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2241 | } |
| 2242 | |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2243 | /* Section 9.5.3: Activate PDP Context reject */ |
| 2244 | int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid, |
| 2245 | uint8_t cause, uint8_t pco_len, uint8_t *pco_v) |
| 2246 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 2247 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP REJ"); |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2248 | struct gsm48_hdr *gh; |
| 2249 | uint8_t transaction_id = tid ^ 0x8; /* flip */ |
| 2250 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2251 | LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2252 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_ACTIVATE_REJECT]); |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2253 | |
| 2254 | mmctx2msgid(msg, mm); |
| 2255 | |
| 2256 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2257 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 2258 | gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ; |
| 2259 | |
| 2260 | msgb_v_put(msg, cause); |
| 2261 | if (pco_len && pco_v) |
| 2262 | msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v); |
| 2263 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 2264 | return gsm48_gmm_sendmsg(msg, 0, mm, true); |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2267 | /* Section 9.5.8: Deactivate PDP Context Request */ |
| 2268 | static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid, |
| 2269 | uint8_t sm_cause) |
| 2270 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 2271 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP DET REQ"); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2272 | struct gsm48_hdr *gh; |
| 2273 | uint8_t transaction_id = tid ^ 0x8; /* flip */ |
| 2274 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2275 | LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n"); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2276 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_DL_DEACTIVATE_REQUEST]); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2277 | |
| 2278 | mmctx2msgid(msg, mm); |
| 2279 | |
| 2280 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2281 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 2282 | gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ; |
| 2283 | |
| 2284 | msgb_v_put(msg, sm_cause); |
| 2285 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 2286 | return gsm48_gmm_sendmsg(msg, 0, mm, true); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2287 | } |
| 2288 | int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause) |
| 2289 | { |
Harald Welte | 9450882 | 2015-08-15 19:08:21 +0200 | [diff] [blame] | 2290 | pdpctx_timer_start(pdp, 3395, sgsn->cfg.timers.T3395); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2291 | |
| 2292 | return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause); |
| 2293 | } |
| 2294 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2295 | /* Section 9.5.9: Deactivate PDP Context Accept */ |
Harald Welte | 60ebc02 | 2010-06-08 18:08:59 +0200 | [diff] [blame] | 2296 | static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2297 | { |
Holger Hans Peter Freyther | 8239e06 | 2016-01-25 22:03:25 +0100 | [diff] [blame] | 2298 | struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP DET ACC"); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2299 | struct gsm48_hdr *gh; |
Harald Welte | 60ebc02 | 2010-06-08 18:08:59 +0200 | [diff] [blame] | 2300 | uint8_t transaction_id = tid ^ 0x8; /* flip */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2301 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2302 | LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n"); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2303 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_DL_DEACTIVATE_ACCEPT]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2304 | |
Harald Welte | 60ebc02 | 2010-06-08 18:08:59 +0200 | [diff] [blame] | 2305 | mmctx2msgid(msg, mm); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2306 | |
| 2307 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 2308 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 2309 | gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK; |
| 2310 | |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 2311 | return gsm48_gmm_sendmsg(msg, 0, mm, true); |
Harald Welte | 60ebc02 | 2010-06-08 18:08:59 +0200 | [diff] [blame] | 2312 | } |
| 2313 | int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp) |
| 2314 | { |
| 2315 | return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2316 | } |
| 2317 | |
Holger Hans Peter Freyther | 7605bdc | 2015-05-24 12:42:26 +0800 | [diff] [blame] | 2318 | static int activate_ggsn(struct sgsn_mm_ctx *mmctx, |
| 2319 | struct sgsn_ggsn_ctx *ggsn, const uint8_t transaction_id, |
| 2320 | const uint8_t req_nsapi, const uint8_t req_llc_sapi, |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2321 | struct tlv_parsed *tp, int destroy_ggsn) |
Holger Hans Peter Freyther | 7605bdc | 2015-05-24 12:42:26 +0800 | [diff] [blame] | 2322 | { |
| 2323 | struct sgsn_pdp_ctx *pdp; |
| 2324 | |
Holger Hans Peter Freyther | b7ae0b3 | 2015-05-29 15:11:55 +0200 | [diff] [blame] | 2325 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Using GGSN %u\n", ggsn->id); |
Holger Hans Peter Freyther | 7605bdc | 2015-05-24 12:42:26 +0800 | [diff] [blame] | 2326 | ggsn->gsn = sgsn->gsn; |
| 2327 | pdp = sgsn_create_pdp_ctx(ggsn, mmctx, req_nsapi, tp); |
| 2328 | if (!pdp) |
| 2329 | return -1; |
| 2330 | |
| 2331 | /* Store SAPI and Transaction Identifier */ |
| 2332 | pdp->sapi = req_llc_sapi; |
| 2333 | pdp->ti = transaction_id; |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2334 | pdp->destroy_ggsn = destroy_ggsn; |
Holger Hans Peter Freyther | 7605bdc | 2015-05-24 12:42:26 +0800 | [diff] [blame] | 2335 | |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2339 | static void ggsn_lookup_cb(void *arg, int status, int timeouts, struct hostent *hostent) |
| 2340 | { |
| 2341 | struct sgsn_ggsn_ctx *ggsn; |
| 2342 | struct sgsn_ggsn_lookup *lookup = arg; |
| 2343 | struct in_addr *addr = NULL; |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2344 | |
| 2345 | /* The context is gone while we made a request */ |
| 2346 | if (!lookup->mmctx) { |
Holger Hans Peter Freyther | 725f3f1 | 2017-01-26 11:05:33 +0100 | [diff] [blame] | 2347 | talloc_free(lookup->orig_msg); |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2348 | talloc_free(lookup); |
| 2349 | return; |
| 2350 | } |
| 2351 | |
| 2352 | if (status != ARES_SUCCESS) { |
| 2353 | struct sgsn_mm_ctx *mmctx = lookup->mmctx; |
| 2354 | |
| 2355 | LOGMMCTXP(LOGL_ERROR, mmctx, "DNS query failed.\n"); |
| 2356 | |
| 2357 | /* Need to try with three digits now */ |
| 2358 | if (lookup->state == SGSN_GGSN_2DIGIT) { |
| 2359 | char *hostname; |
| 2360 | int rc; |
| 2361 | |
| 2362 | lookup->state = SGSN_GGSN_3DIGIT; |
| 2363 | hostname = osmo_apn_qualify_from_imsi(mmctx->imsi, |
| 2364 | lookup->apn_str, 1); |
| 2365 | LOGMMCTXP(LOGL_DEBUG, mmctx, |
| 2366 | "Going to query %s\n", hostname); |
| 2367 | rc = sgsn_ares_query(sgsn, hostname, |
| 2368 | ggsn_lookup_cb, lookup); |
| 2369 | if (rc != 0) { |
Harald Welte | 601cf12 | 2015-08-16 17:16:28 +0200 | [diff] [blame] | 2370 | LOGMMCTXP(LOGL_ERROR, mmctx, "Couldn't start GGSN\n"); |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2371 | goto reject_due_failure; |
| 2372 | } |
| 2373 | return; |
| 2374 | } |
| 2375 | |
| 2376 | LOGMMCTXP(LOGL_ERROR, mmctx, "Couldn't resolve GGSN\n"); |
| 2377 | goto reject_due_failure; |
| 2378 | } |
| 2379 | |
| 2380 | if (hostent->h_length != sizeof(struct in_addr)) { |
| 2381 | LOGMMCTXP(LOGL_ERROR, lookup->mmctx, |
Harald Welte | 6b39fee | 2015-08-16 17:16:57 +0200 | [diff] [blame] | 2382 | "Wrong addr size(%zu)\n", sizeof(struct in_addr)); |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2383 | goto reject_due_failure; |
| 2384 | } |
| 2385 | |
Holger Hans Peter Freyther | 0958938 | 2015-06-02 09:38:41 +0200 | [diff] [blame] | 2386 | /* Get the first addr from the list */ |
| 2387 | addr = (struct in_addr *) hostent->h_addr_list[0]; |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2388 | if (!addr) { |
| 2389 | LOGMMCTXP(LOGL_ERROR, lookup->mmctx, "No host address.\n"); |
| 2390 | goto reject_due_failure; |
| 2391 | } |
| 2392 | |
| 2393 | ggsn = sgsn_ggsn_ctx_alloc(UINT32_MAX); |
| 2394 | if (!ggsn) { |
| 2395 | LOGMMCTXP(LOGL_ERROR, lookup->mmctx, "Failed to create ggsn.\n"); |
| 2396 | goto reject_due_failure; |
| 2397 | } |
| 2398 | ggsn->remote_addr = *addr; |
| 2399 | LOGMMCTXP(LOGL_NOTICE, lookup->mmctx, |
| 2400 | "Selected %s as GGSN.\n", inet_ntoa(*addr)); |
| 2401 | |
| 2402 | /* forget about the ggsn look-up */ |
| 2403 | lookup->mmctx->ggsn_lookup = NULL; |
| 2404 | |
| 2405 | activate_ggsn(lookup->mmctx, ggsn, lookup->ti, lookup->nsapi, |
| 2406 | lookup->sapi, &lookup->tp, 1); |
| 2407 | |
| 2408 | /* Now free it */ |
Holger Hans Peter Freyther | 725f3f1 | 2017-01-26 11:05:33 +0100 | [diff] [blame] | 2409 | talloc_free(lookup->orig_msg); |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2410 | talloc_free(lookup); |
| 2411 | return; |
| 2412 | |
| 2413 | reject_due_failure: |
| 2414 | gsm48_tx_gsm_act_pdp_rej(lookup->mmctx, lookup->ti, |
| 2415 | GMM_CAUSE_NET_FAIL, 0, NULL); |
| 2416 | lookup->mmctx->ggsn_lookup = NULL; |
Holger Hans Peter Freyther | 725f3f1 | 2017-01-26 11:05:33 +0100 | [diff] [blame] | 2417 | talloc_free(lookup->orig_msg); |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2418 | talloc_free(lookup); |
| 2419 | } |
Holger Hans Peter Freyther | ccd2312 | 2015-05-24 16:48:22 +0800 | [diff] [blame] | 2420 | |
Holger Hans Peter Freyther | 725f3f1 | 2017-01-26 11:05:33 +0100 | [diff] [blame] | 2421 | static int do_act_pdp_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg, bool *delete) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2422 | { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 2423 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2424 | struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data; |
Harald Welte | 0c3eae0 | 2010-05-02 21:07:14 +0200 | [diff] [blame] | 2425 | uint8_t req_qos_len, req_pdpa_len; |
| 2426 | uint8_t *req_qos, *req_pdpa; |
| 2427 | struct tlv_parsed tp; |
Neels Hofmeyr | 961bd0b | 2016-03-14 16:13:25 +0100 | [diff] [blame] | 2428 | uint8_t transaction_id = gsm48_hdr_trans_id(gh); |
Harald Welte | 11ff3e8 | 2010-07-02 10:17:24 +0200 | [diff] [blame] | 2429 | struct sgsn_ggsn_ctx *ggsn; |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2430 | struct sgsn_pdp_ctx *pdp; |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 2431 | enum gsm48_gsm_cause gsm_cause; |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2432 | char apn_str[GSM_APN_LENGTH] = { 0, }; |
| 2433 | char *hostname; |
| 2434 | int rc; |
Philipp Maier | d015cbd | 2017-02-27 17:58:31 +0100 | [diff] [blame] | 2435 | struct gprs_llc_lle *lle; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2436 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2437 | LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ", |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2438 | act_req->req_llc_sapi, act_req->req_nsapi); |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 2439 | |
Harald Welte | 23c1376 | 2010-07-02 22:52:04 +0200 | [diff] [blame] | 2440 | /* FIXME: length checks! */ |
Harald Welte | 0c3eae0 | 2010-05-02 21:07:14 +0200 | [diff] [blame] | 2441 | req_qos_len = act_req->data[0]; |
| 2442 | req_qos = act_req->data + 1; /* 10.5.6.5 */ |
| 2443 | req_pdpa_len = act_req->data[1 + req_qos_len]; |
| 2444 | req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2445 | |
Harald Welte | 0c3eae0 | 2010-05-02 21:07:14 +0200 | [diff] [blame] | 2446 | switch (req_pdpa[0] & 0xf) { |
| 2447 | case 0x0: |
| 2448 | DEBUGPC(DMM, "ETSI "); |
| 2449 | break; |
| 2450 | case 0x1: |
| 2451 | DEBUGPC(DMM, "IETF "); |
| 2452 | break; |
| 2453 | case 0xf: |
| 2454 | DEBUGPC(DMM, "Empty "); |
| 2455 | break; |
| 2456 | } |
| 2457 | |
| 2458 | switch (req_pdpa[1]) { |
| 2459 | case 0x21: |
| 2460 | DEBUGPC(DMM, "IPv4 "); |
| 2461 | if (req_pdpa_len >= 6) { |
| 2462 | struct in_addr ia; |
| 2463 | ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2))); |
| 2464 | DEBUGPC(DMM, "%s ", inet_ntoa(ia)); |
| 2465 | } |
| 2466 | break; |
| 2467 | case 0x57: |
| 2468 | DEBUGPC(DMM, "IPv6 "); |
| 2469 | if (req_pdpa_len >= 18) { |
| 2470 | /* FIXME: print IPv6 address */ |
| 2471 | } |
| 2472 | break; |
| 2473 | default: |
| 2474 | DEBUGPC(DMM, "0x%02x ", req_pdpa[1]); |
| 2475 | break; |
| 2476 | } |
| 2477 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2478 | LOGPC(DMM, LOGL_INFO, "\n"); |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2479 | |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2480 | /* Check if NSAPI is out of range (TS 04.65 / 7.2) */ |
| 2481 | if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) { |
| 2482 | /* Send reject with GSM_CAUSE_INV_MAND_INFO */ |
| 2483 | return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id, |
| 2484 | GSM_CAUSE_INV_MAND_INFO, |
| 2485 | 0, NULL); |
| 2486 | } |
| 2487 | |
Holger Hans Peter Freyther | 7605bdc | 2015-05-24 12:42:26 +0800 | [diff] [blame] | 2488 | /* Optional: Access Point Name, Protocol Config Options */ |
| 2489 | if (req_pdpa + req_pdpa_len < msg->data + msg->len) |
| 2490 | tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len, |
| 2491 | (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0); |
| 2492 | else |
| 2493 | memset(&tp, 0, sizeof(tp)); |
| 2494 | |
| 2495 | |
| 2496 | /* put the non-TLV elements in the TLV parser structure to |
| 2497 | * pass them on to the SGSN / GTP code */ |
| 2498 | tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len; |
| 2499 | tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos; |
| 2500 | tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len; |
| 2501 | tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa; |
| 2502 | |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2503 | /* Check if NSAPI is already in use */ |
Harald Welte | b37515e | 2010-07-02 10:12:58 +0200 | [diff] [blame] | 2504 | pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi); |
| 2505 | if (pdp) { |
| 2506 | /* We already have a PDP context for this TLLI + NSAPI tuple */ |
| 2507 | if (pdp->sapi == act_req->req_llc_sapi && |
| 2508 | pdp->ti == transaction_id) { |
| 2509 | /* This apparently is a re-transmission of a PDP CTX |
| 2510 | * ACT REQ (our ACT ACK must have got dropped) */ |
Philipp Maier | d015cbd | 2017-02-27 17:58:31 +0100 | [diff] [blame] | 2511 | rc = gsm48_tx_gsm_act_pdp_acc(pdp); |
| 2512 | if (rc < 0) |
| 2513 | return rc; |
| 2514 | |
| 2515 | if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) { |
| 2516 | /* Also re-transmit the SNDCP XID message */ |
| 2517 | lle = &pdp->mm->gb.llme->lle[pdp->sapi]; |
| 2518 | rc = sndcp_sn_xid_req(lle,pdp->nsapi); |
| 2519 | if (rc < 0) |
| 2520 | return rc; |
| 2521 | } |
| 2522 | |
| 2523 | return 0; |
Harald Welte | b37515e | 2010-07-02 10:12:58 +0200 | [diff] [blame] | 2524 | } |
| 2525 | |
| 2526 | /* Send reject with GSM_CAUSE_NSAPI_IN_USE */ |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2527 | return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id, |
| 2528 | GSM_CAUSE_NSAPI_IN_USE, |
| 2529 | 0, NULL); |
| 2530 | } |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2531 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2532 | if (mmctx->ggsn_lookup) { |
| 2533 | if (mmctx->ggsn_lookup->sapi == act_req->req_llc_sapi && |
| 2534 | mmctx->ggsn_lookup->ti == transaction_id) { |
| 2535 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 2536 | "Re-transmission while doing look-up. Ignoring.\n"); |
| 2537 | return 0; |
| 2538 | } |
| 2539 | } |
| 2540 | |
Harald Welte | 19f9e30 | 2010-07-02 10:15:49 +0200 | [diff] [blame] | 2541 | /* Only increment counter for a real activation, after we checked |
| 2542 | * for re-transmissions */ |
| 2543 | rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]); |
| 2544 | |
Jacob Erlbeck | 277b71e | 2015-02-02 18:03:05 +0100 | [diff] [blame] | 2545 | /* Determine GGSN based on APN and subscription options */ |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2546 | ggsn = sgsn_mm_ctx_find_ggsn_ctx(mmctx, &tp, &gsm_cause, apn_str); |
| 2547 | if (ggsn) |
| 2548 | return activate_ggsn(mmctx, ggsn, transaction_id, |
| 2549 | act_req->req_nsapi, act_req->req_llc_sapi, |
| 2550 | &tp, 0); |
| 2551 | |
| 2552 | if (strlen(apn_str) == 0) |
| 2553 | goto no_context; |
| 2554 | if (!sgsn->cfg.dynamic_lookup) |
| 2555 | goto no_context; |
| 2556 | |
| 2557 | /* schedule a dynamic look-up */ |
| 2558 | mmctx->ggsn_lookup = talloc_zero(tall_bsc_ctx, struct sgsn_ggsn_lookup); |
| 2559 | if (!mmctx->ggsn_lookup) |
| 2560 | goto no_context; |
| 2561 | |
| 2562 | mmctx->ggsn_lookup->state = SGSN_GGSN_2DIGIT; |
| 2563 | mmctx->ggsn_lookup->mmctx = mmctx; |
| 2564 | strcpy(mmctx->ggsn_lookup->apn_str, apn_str); |
| 2565 | |
| 2566 | mmctx->ggsn_lookup->orig_msg = msg; |
| 2567 | mmctx->ggsn_lookup->tp = tp; |
| 2568 | |
| 2569 | mmctx->ggsn_lookup->ti = transaction_id; |
| 2570 | mmctx->ggsn_lookup->nsapi = act_req->req_nsapi; |
| 2571 | mmctx->ggsn_lookup->sapi = act_req->req_llc_sapi; |
| 2572 | |
| 2573 | hostname = osmo_apn_qualify_from_imsi(mmctx->imsi, |
| 2574 | mmctx->ggsn_lookup->apn_str, 0); |
| 2575 | |
| 2576 | LOGMMCTXP(LOGL_DEBUG, mmctx, "Going to query %s\n", hostname); |
| 2577 | rc = sgsn_ares_query(sgsn, hostname, |
| 2578 | ggsn_lookup_cb, mmctx->ggsn_lookup); |
| 2579 | if (rc != 0) { |
| 2580 | LOGMMCTXP(LOGL_ERROR, mmctx, "Failed to start ares query.\n"); |
| 2581 | goto no_context; |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2582 | } |
Holger Hans Peter Freyther | 725f3f1 | 2017-01-26 11:05:33 +0100 | [diff] [blame] | 2583 | *delete = 0; |
Harald Welte | 11ff3e8 | 2010-07-02 10:17:24 +0200 | [diff] [blame] | 2584 | |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2585 | return 0; |
| 2586 | |
| 2587 | no_context: |
Harald Welte | 601cf12 | 2015-08-16 17:16:28 +0200 | [diff] [blame] | 2588 | LOGMMCTXP(LOGL_ERROR, mmctx, "No GGSN context found!\n"); |
Holger Hans Peter Freyther | 39c430e | 2015-05-25 12:26:49 +0800 | [diff] [blame] | 2589 | return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id, |
| 2590 | gsm_cause, 0, NULL); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2591 | } |
| 2592 | |
Holger Hans Peter Freyther | ccd2312 | 2015-05-24 16:48:22 +0800 | [diff] [blame] | 2593 | /* Section 9.5.1: Activate PDP Context Request */ |
| 2594 | static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx, |
| 2595 | struct msgb *_msg) |
| 2596 | { |
Holger Hans Peter Freyther | 725f3f1 | 2017-01-26 11:05:33 +0100 | [diff] [blame] | 2597 | bool delete = 1; |
Holger Hans Peter Freyther | ccd2312 | 2015-05-24 16:48:22 +0800 | [diff] [blame] | 2598 | struct msgb *msg; |
| 2599 | int rc; |
| 2600 | |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2601 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_ACTIVATE_REQUEST]); |
| 2602 | |
Holger Hans Peter Freyther | ccd2312 | 2015-05-24 16:48:22 +0800 | [diff] [blame] | 2603 | /* |
| 2604 | * This is painful. We might not have a static GGSN |
| 2605 | * configuration and then would need to copy the msg |
| 2606 | * and re-do most of this routine (or call it again |
| 2607 | * and make sure it only goes through the dynamic |
| 2608 | * resolving. The question is what to optimize for |
| 2609 | * and the dynamic resolution will be the right thing |
| 2610 | * in the long run. |
| 2611 | */ |
| 2612 | msg = gprs_msgb_copy(_msg, __func__); |
| 2613 | if (!msg) { |
| 2614 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(_msg); |
Neels Hofmeyr | 961bd0b | 2016-03-14 16:13:25 +0100 | [diff] [blame] | 2615 | uint8_t transaction_id = gsm48_hdr_trans_id(gh); |
Holger Hans Peter Freyther | ccd2312 | 2015-05-24 16:48:22 +0800 | [diff] [blame] | 2616 | |
| 2617 | LOGMMCTXP(LOGL_ERROR, mmctx, "-> ACTIVATE PDP CONTEXT REQ failed copy.\n"); |
| 2618 | /* Send reject with GSM_CAUSE_INV_MAND_INFO */ |
| 2619 | return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id, |
| 2620 | GSM_CAUSE_NET_FAIL, |
| 2621 | 0, NULL); |
| 2622 | } |
| 2623 | |
Holger Hans Peter Freyther | 725f3f1 | 2017-01-26 11:05:33 +0100 | [diff] [blame] | 2624 | rc = do_act_pdp_req(mmctx, msg, &delete); |
| 2625 | if (delete) |
| 2626 | msgb_free(msg); |
Holger Hans Peter Freyther | ccd2312 | 2015-05-24 16:48:22 +0800 | [diff] [blame] | 2627 | return rc; |
| 2628 | } |
| 2629 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2630 | /* Section 9.5.8: Deactivate PDP Context Request */ |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2631 | static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2632 | { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 2633 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Neels Hofmeyr | 961bd0b | 2016-03-14 16:13:25 +0100 | [diff] [blame] | 2634 | uint8_t transaction_id = gsm48_hdr_trans_id(gh); |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2635 | struct sgsn_pdp_ctx *pdp; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2636 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2637 | LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n", |
Jacob Erlbeck | afcf230 | 2015-01-05 17:30:02 +0100 | [diff] [blame] | 2638 | get_value_string(gsm48_gsm_cause_names, gh->data[0])); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2639 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_UL_DEACTIVATE_REQUEST]); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2640 | |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2641 | pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id); |
| 2642 | if (!pdp) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2643 | LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for " |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2644 | "non-existing PDP Context (IMSI=%s, TI=%u)\n", |
| 2645 | mm->imsi, transaction_id); |
Harald Welte | 60ebc02 | 2010-06-08 18:08:59 +0200 | [diff] [blame] | 2646 | return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id); |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | return sgsn_delete_pdp_ctx(pdp); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2650 | } |
| 2651 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2652 | /* Section 9.5.9: Deactivate PDP Context Accept */ |
| 2653 | static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg) |
| 2654 | { |
| 2655 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Neels Hofmeyr | 961bd0b | 2016-03-14 16:13:25 +0100 | [diff] [blame] | 2656 | uint8_t transaction_id = gsm48_hdr_trans_id(gh); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2657 | struct sgsn_pdp_ctx *pdp; |
| 2658 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2659 | LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n"); |
Alexander Couzens | 14314bd | 2016-07-05 09:52:52 +0200 | [diff] [blame] | 2660 | rate_ctr_inc(&sgsn->rate_ctrs->ctr[CTR_PDP_UL_DEACTIVATE_ACCEPT]); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2661 | |
| 2662 | pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id); |
| 2663 | if (!pdp) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2664 | LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for " |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2665 | "non-existing PDP Context (IMSI=%s, TI=%u)\n", |
| 2666 | mm->imsi, transaction_id); |
| 2667 | return 0; |
| 2668 | } |
Pravin Kumarvel | 16606c9 | 2016-11-17 12:20:01 +0530 | [diff] [blame] | 2669 | /* stop timer 3395 */ |
| 2670 | pdpctx_timer_stop(pdp, 3395); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2671 | return sgsn_delete_pdp_ctx(pdp); |
| 2672 | } |
| 2673 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2674 | static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2675 | { |
| 2676 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 2677 | |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2678 | LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n", |
Jacob Erlbeck | afcf230 | 2015-01-05 17:30:02 +0100 | [diff] [blame] | 2679 | get_value_string(gsm48_gsm_cause_names, gh->data[0])); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2680 | |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2684 | static void pdpctx_timer_cb(void *_pdp) |
| 2685 | { |
| 2686 | struct sgsn_pdp_ctx *pdp = _pdp; |
| 2687 | |
| 2688 | pdp->num_T_exp++; |
| 2689 | |
| 2690 | switch (pdp->T) { |
| 2691 | case 3395: /* waiting for PDP CTX DEACT ACK */ |
| 2692 | if (pdp->num_T_exp >= 4) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2693 | LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired >= 5 times\n"); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2694 | pdp->state = PDP_STATE_INACTIVE; |
| 2695 | sgsn_delete_pdp_ctx(pdp); |
| 2696 | break; |
| 2697 | } |
Alexander Couzens | 15fcd10 | 2016-07-19 15:14:03 +0200 | [diff] [blame] | 2698 | gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL); |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2699 | break; |
| 2700 | default: |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2701 | LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n", |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2702 | pdp->T); |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2707 | /* GPRS Session Management */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 2708 | static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg, |
| 2709 | struct gprs_llc_llme *llme) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2710 | { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 2711 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2712 | int rc; |
| 2713 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 2714 | /* MMCTX can be NULL when called */ |
| 2715 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2716 | if (!mmctx) { |
| 2717 | LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n"); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2718 | /* 6.1.3.6 */ |
| 2719 | if (gh->msg_type == GSM48_MT_GSM_STATUS) |
| 2720 | return 0; |
| 2721 | |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 2722 | return gsm0408_gprs_force_reattach_oldmsg(msg, llme); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2723 | } |
| 2724 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2725 | switch (gh->msg_type) { |
| 2726 | case GSM48_MT_GSM_ACT_PDP_REQ: |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2727 | rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2728 | break; |
| 2729 | case GSM48_MT_GSM_DEACT_PDP_REQ: |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2730 | rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg); |
Harald Welte | 77289c2 | 2010-05-18 14:32:29 +0200 | [diff] [blame] | 2731 | break; |
Harald Welte | a9b473a | 2010-12-24 21:13:26 +0100 | [diff] [blame] | 2732 | case GSM48_MT_GSM_DEACT_PDP_ACK: |
| 2733 | rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg); |
| 2734 | break; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2735 | case GSM48_MT_GSM_STATUS: |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2736 | rc = gsm48_rx_gsm_status(mmctx, msg); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2737 | break; |
| 2738 | case GSM48_MT_GSM_REQ_PDP_ACT_REJ: |
| 2739 | case GSM48_MT_GSM_ACT_AA_PDP_REQ: |
| 2740 | case GSM48_MT_GSM_DEACT_AA_PDP_REQ: |
Harald Welte | 6a19dd0 | 2015-08-16 12:18:13 +0200 | [diff] [blame] | 2741 | LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x: %s\n", |
| 2742 | gh->msg_type, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg))); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 2743 | rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2744 | break; |
| 2745 | default: |
Harald Welte | 6a19dd0 | 2015-08-16 12:18:13 +0200 | [diff] [blame] | 2746 | LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x: %s\n", |
| 2747 | gh->msg_type, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg))); |
Harald Welte | f54e7e2 | 2010-06-09 10:51:23 +0200 | [diff] [blame] | 2748 | rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2749 | break; |
| 2750 | |
| 2751 | } |
| 2752 | |
| 2753 | return rc; |
| 2754 | } |
| 2755 | |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 2756 | int gsm0408_gprs_force_reattach_oldmsg(struct msgb *msg, |
| 2757 | struct gprs_llc_llme *llme) |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2758 | { |
Jacob Erlbeck | abdf02b | 2014-10-31 12:20:49 +0100 | [diff] [blame] | 2759 | int rc; |
Alexander Couzens | a173566 | 2016-08-30 18:50:58 +0200 | [diff] [blame] | 2760 | if (llme) |
| 2761 | gprs_llgmm_reset_oldmsg(msg, GPRS_SAPI_GMM, llme); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2762 | |
Jacob Erlbeck | abdf02b | 2014-10-31 12:20:49 +0100 | [diff] [blame] | 2763 | rc = gsm48_tx_gmm_detach_req_oldmsg( |
| 2764 | msg, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2765 | |
Jacob Erlbeck | abdf02b | 2014-10-31 12:20:49 +0100 | [diff] [blame] | 2766 | return rc; |
| 2767 | } |
| 2768 | |
| 2769 | int gsm0408_gprs_force_reattach(struct sgsn_mm_ctx *mmctx) |
| 2770 | { |
| 2771 | int rc; |
Harald Welte | dbc72b3 | 2015-12-25 22:23:27 +0100 | [diff] [blame] | 2772 | if (mmctx->ran_type == MM_CTX_T_GERAN_Gb) |
| 2773 | gprs_llgmm_reset(mmctx->gb.llme); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2774 | |
Jacob Erlbeck | b1c074f | 2014-10-31 12:27:11 +0100 | [diff] [blame] | 2775 | rc = gsm48_tx_gmm_detach_req( |
| 2776 | mmctx, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2777 | |
Jacob Erlbeck | b1c074f | 2014-10-31 12:27:11 +0100 | [diff] [blame] | 2778 | mm_ctx_cleanup_free(mmctx, "forced reattach"); |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2779 | |
Jacob Erlbeck | b1c074f | 2014-10-31 12:27:11 +0100 | [diff] [blame] | 2780 | return rc; |
Jacob Erlbeck | 78ecaf0 | 2014-09-05 14:32:36 +0200 | [diff] [blame] | 2781 | } |
| 2782 | |
Daniel Willmann | 6292c8d | 2016-05-21 17:35:57 +0200 | [diff] [blame] | 2783 | /* Main entry point for incoming 04.08 GPRS messages from Iu */ |
| 2784 | int gsm0408_gprs_rcvmsg_iu(struct msgb *msg, struct gprs_ra_id *ra_id, |
| 2785 | uint16_t *sai) |
| 2786 | { |
| 2787 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
| 2788 | uint8_t pdisc = gsm48_hdr_pdisc(gh); |
| 2789 | struct sgsn_mm_ctx *mmctx; |
| 2790 | int rc = -EINVAL; |
| 2791 | |
| 2792 | mmctx = sgsn_mm_ctx_by_ue_ctx(msg->dst); |
| 2793 | if (mmctx) { |
| 2794 | rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]); |
| 2795 | if (ra_id) |
| 2796 | memcpy(&mmctx->ra, ra_id, sizeof(mmctx->ra)); |
| 2797 | } |
| 2798 | |
| 2799 | /* MMCTX can be NULL */ |
| 2800 | |
| 2801 | switch (pdisc) { |
| 2802 | case GSM48_PDISC_MM_GPRS: |
| 2803 | rc = gsm0408_rcv_gmm(mmctx, msg, NULL, false); |
| 2804 | #warning "set drop_cipherable arg for gsm0408_rcv_gmm() from IuPS?" |
| 2805 | break; |
| 2806 | case GSM48_PDISC_SM_GPRS: |
| 2807 | rc = gsm0408_rcv_gsm(mmctx, msg, NULL); |
| 2808 | break; |
| 2809 | default: |
| 2810 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 2811 | "Unknown GSM 04.08 discriminator 0x%02x: %s\n", |
| 2812 | pdisc, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg))); |
| 2813 | /* FIXME: return status message */ |
| 2814 | break; |
| 2815 | } |
| 2816 | |
| 2817 | /* MMCTX can be invalid */ |
| 2818 | |
| 2819 | return rc; |
| 2820 | } |
| 2821 | |
Harald Welte | 49393e1 | 2015-12-25 19:14:57 +0100 | [diff] [blame] | 2822 | /* Main entry point for incoming 04.08 GPRS messages from Gb */ |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 2823 | int gsm0408_gprs_rcvmsg_gb(struct msgb *msg, struct gprs_llc_llme *llme, |
| 2824 | bool drop_cipherable) |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2825 | { |
Harald Welte | 943c5bc | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 2826 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Neels Hofmeyr | 531734a | 2016-03-14 16:13:24 +0100 | [diff] [blame] | 2827 | uint8_t pdisc = gsm48_hdr_pdisc(gh); |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2828 | struct sgsn_mm_ctx *mmctx; |
| 2829 | struct gprs_ra_id ra_id; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2830 | int rc = -EINVAL; |
| 2831 | |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2832 | bssgp_parse_cell_id(&ra_id, msgb_bcid(msg)); |
| 2833 | mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id); |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 2834 | if (mmctx) { |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2835 | msgid2mmctx(mmctx, msg); |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 2836 | rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]); |
Harald Welte | f97ee04 | 2015-12-25 19:12:21 +0100 | [diff] [blame] | 2837 | mmctx->gb.llme = llme; |
Harald Welte | 8acd88f | 2010-05-18 10:57:45 +0200 | [diff] [blame] | 2838 | } |
Harald Welte | d193cb3 | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 2839 | |
| 2840 | /* MMCTX can be NULL */ |
| 2841 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2842 | switch (pdisc) { |
| 2843 | case GSM48_PDISC_MM_GPRS: |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 2844 | rc = gsm0408_rcv_gmm(mmctx, msg, llme, drop_cipherable); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2845 | break; |
| 2846 | case GSM48_PDISC_SM_GPRS: |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 2847 | rc = gsm0408_rcv_gsm(mmctx, msg, llme); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2848 | break; |
| 2849 | default: |
Harald Welte | eb98c74 | 2015-08-16 17:11:32 +0200 | [diff] [blame] | 2850 | LOGMMCTXP(LOGL_NOTICE, mmctx, |
| 2851 | "Unknown GSM 04.08 discriminator 0x%02x: %s\n", |
Harald Welte | 6a19dd0 | 2015-08-16 12:18:13 +0200 | [diff] [blame] | 2852 | pdisc, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg))); |
Harald Welte | 6abf94e | 2010-05-18 10:35:06 +0200 | [diff] [blame] | 2853 | /* FIXME: return status message */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2854 | break; |
| 2855 | } |
| 2856 | |
Jacob Erlbeck | 258ce3d | 2014-09-30 13:51:45 +0200 | [diff] [blame] | 2857 | /* MMCTX can be invalid */ |
| 2858 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 2859 | return rc; |
| 2860 | } |
Harald Welte | 5bfe499 | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 2861 | |
| 2862 | int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli) |
| 2863 | { |
| 2864 | struct sgsn_mm_ctx *mmctx; |
| 2865 | |
| 2866 | mmctx = sgsn_mm_ctx_by_tlli(tlli, raid); |
| 2867 | if (!mmctx) { |
| 2868 | LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown " |
| 2869 | "TLLI=%08x\n", tlli); |
| 2870 | return -EINVAL; |
| 2871 | } |
| 2872 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2873 | if (mmctx->gmm_state != GMM_REGISTERED_NORMAL && |
| 2874 | mmctx->gmm_state != GMM_REGISTERED_SUSPENDED) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2875 | LOGMMCTXP(LOGL_NOTICE, mmctx, "SUSPEND request while state " |
Harald Welte | 5bfe499 | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 2876 | "!= REGISTERED (TLLI=%08x)\n", tlli); |
| 2877 | return -EINVAL; |
| 2878 | } |
| 2879 | |
| 2880 | /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */ |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2881 | mmctx->gmm_state = GMM_REGISTERED_SUSPENDED; |
Harald Welte | 5bfe499 | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 2882 | return 0; |
| 2883 | } |
| 2884 | |
| 2885 | int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli, |
| 2886 | uint8_t suspend_ref) |
| 2887 | { |
| 2888 | struct sgsn_mm_ctx *mmctx; |
| 2889 | |
| 2890 | /* FIXME: make use of suspend reference? */ |
| 2891 | |
| 2892 | mmctx = sgsn_mm_ctx_by_tlli(tlli, raid); |
| 2893 | if (!mmctx) { |
| 2894 | LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown " |
| 2895 | "TLLI=%08x\n", tlli); |
| 2896 | return -EINVAL; |
| 2897 | } |
| 2898 | |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2899 | if (mmctx->gmm_state != GMM_REGISTERED_NORMAL && |
| 2900 | mmctx->gmm_state != GMM_REGISTERED_SUSPENDED) { |
Daniel Willmann | 4655314 | 2014-09-03 17:46:44 +0200 | [diff] [blame] | 2901 | LOGMMCTXP(LOGL_NOTICE, mmctx, "RESUME request while state " |
Harald Welte | 5bfe499 | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 2902 | "!= SUSPENDED (TLLI=%08x)\n", tlli); |
| 2903 | /* FIXME: should we not simply ignore it? */ |
| 2904 | return -EINVAL; |
| 2905 | } |
| 2906 | |
| 2907 | /* Transition from SUSPENDED to NORMAL */ |
Alexander Couzens | 4f8da6d | 2017-01-31 15:34:26 +0100 | [diff] [blame] | 2908 | mmctx->gmm_state = GMM_REGISTERED_NORMAL; |
Harald Welte | 5bfe499 | 2010-06-09 11:22:47 +0200 | [diff] [blame] | 2909 | return 0; |
| 2910 | } |
Daniel Willmann | 6b7b319 | 2016-05-21 00:53:42 +0200 | [diff] [blame] | 2911 | |
| 2912 | #ifdef BUILD_IU |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 2913 | int iu_rab_act_ps(uint8_t rab_id, struct sgsn_pdp_ctx *pdp) |
Daniel Willmann | 6b7b319 | 2016-05-21 00:53:42 +0200 | [diff] [blame] | 2914 | { |
| 2915 | struct msgb *msg; |
| 2916 | struct sgsn_mm_ctx *mm = pdp->mm; |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 2917 | struct ranap_ue_conn_ctx *uectx; |
Daniel Willmann | 6b7b319 | 2016-05-21 00:53:42 +0200 | [diff] [blame] | 2918 | uint32_t ggsn_ip; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 2919 | bool use_x213_nsap; |
Daniel Willmann | 6b7b319 | 2016-05-21 00:53:42 +0200 | [diff] [blame] | 2920 | |
| 2921 | uectx = mm->iu.ue_ctx; |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 2922 | use_x213_nsap = (uectx->rab_assign_addr_enc == RANAP_NSAP_ADDR_ENC_X213); |
Daniel Willmann | 6b7b319 | 2016-05-21 00:53:42 +0200 | [diff] [blame] | 2923 | |
| 2924 | /* Get the IP address for ggsn user plane */ |
| 2925 | memcpy(&ggsn_ip, pdp->lib->gsnru.v, pdp->lib->gsnru.l); |
| 2926 | ggsn_ip = htonl(ggsn_ip); |
| 2927 | |
| 2928 | LOGP(DRANAP, LOGL_DEBUG, "Assigning RAB: rab_id=%d, ggsn_ip=%x," |
| 2929 | " teid_gn=%x, use_x213_nsap=%d\n", |
| 2930 | rab_id, ggsn_ip, pdp->lib->teid_gn, use_x213_nsap); |
| 2931 | |
| 2932 | msg = ranap_new_msg_rab_assign_data(rab_id, ggsn_ip, |
| 2933 | pdp->lib->teid_gn, use_x213_nsap); |
| 2934 | msg->l2h = msg->data; |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 2935 | return ranap_iu_rab_act(uectx, msg); |
Daniel Willmann | 6b7b319 | 2016-05-21 00:53:42 +0200 | [diff] [blame] | 2936 | } |
| 2937 | #endif |