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