blob: b17873e89dd939617e00aba599bfb56d8563703c [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* 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 Welte9f1f3ad2010-05-02 12:56:57 +02004/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Weltec2e8cc42010-05-31 20:23:38 +02005 * (C) 2010 by On-Waves
Harald Welte9b455bf2010-03-14 15:45:01 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * 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 Welte9b455bf2010-03-14 15:45:01 +080012 * (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 Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * 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 Welte9b455bf2010-03-14 15:45:01 +080021 *
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020027#include <stdint.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080028#include <errno.h>
29
30#include <netinet/in.h>
Harald Welte421cba42010-05-02 23:11:50 +020031#include <arpa/inet.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080032
33#include <openbsc/db.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/msgb.h>
Harald Welted36ff762011-03-23 18:26:56 +010035#include <osmocom/gsm/tlv.h>
36#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010037#include <osmocom/core/signal.h>
38#include <osmocom/core/talloc.h>
39#include <osmocom/core/rate_ctr.h>
Harald Welted6057092010-05-02 12:57:45 +020040
Harald Welteea34a4e2012-06-16 14:59:56 +080041#include <osmocom/gprs/gprs_bssgp.h>
42
Harald Welte9b455bf2010-03-14 15:45:01 +080043#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080045#include <openbsc/gsm_subscriber.h>
46#include <openbsc/gsm_04_08.h>
47#include <openbsc/gsm_04_08_gprs.h>
48#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080049#include <openbsc/transaction.h>
50#include <openbsc/gprs_llc.h>
51#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020052#include <openbsc/gprs_gmm.h>
Jacob Erlbeck8de9c482015-01-23 11:44:39 +010053#include <openbsc/gprs_utils.h>
Harald Welted193cb32010-05-17 22:58:03 +020054#include <openbsc/sgsn.h>
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +020055#include <openbsc/signal.h>
Harald Welted193cb32010-05-17 22:58:03 +020056
Harald Welte6abf94e2010-05-18 10:35:06 +020057#include <pdp.h>
58
Harald Weltec2e8cc42010-05-31 20:23:38 +020059#define PTMSI_ALLOC
60
Jacob Erlbeck841d95f2015-01-23 12:32:38 +010061/* Section 11.2.2 / Table 11.3a GPRS Mobility management timers – MS side */
62#define GSM0408_T3312_SECS (10*60) /* periodic RAU interval, default 54min */
63
Harald Welte649e1ff2013-07-21 17:41:46 +080064/* Section 11.2.2 / Table 11.4 MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020065#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
66#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
67#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
68#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
69
Harald Welte649e1ff2013-07-21 17:41:46 +080070/* Section 11.2.2 / Table 11.4a MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020071#define GSM0408_T3313_SECS 30 /* waiting for paging response */
Jacob Erlbeck8de9c482015-01-23 11:44:39 +010072#define GSM0408_T3314_SECS 44 /* force to STBY on expiry, Ready timer */
Harald Weltec2e8cc42010-05-31 20:23:38 +020073#define GSM0408_T3316_SECS 44
Jacob Erlbeck81ffb742015-01-23 11:33:51 +010074#define GSM0408_MOBILE_REACHABLE_SECS (GSM0408_T3312_SECS + 4 * 60)
Harald Weltec2e8cc42010-05-31 20:23:38 +020075
Harald Weltea9b473a2010-12-24 21:13:26 +010076/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
77#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
78#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
79#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
80#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
81
Harald Welted193cb32010-05-17 22:58:03 +020082extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080083
Harald Welte588d44e2010-06-09 10:28:29 +020084static const struct tlv_definition gsm48_gmm_att_tlvdef = {
85 .def = {
Harald Welte4b6ac1e2010-07-03 11:09:06 +020086 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
Harald Welte588d44e2010-06-09 10:28:29 +020087 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
88 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +020089 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
90 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
91 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
Harald Welte588d44e2010-06-09 10:28:29 +020092 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +020093 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
Harald Welte588d44e2010-06-09 10:28:29 +020094 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
95 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
96 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
97 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
98 },
99};
100
101static const struct tlv_definition gsm48_sm_att_tlvdef = {
102 .def = {
103 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
104 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
105 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
106 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
107 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
108 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200109 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
110 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
Harald Welte588d44e2010-06-09 10:28:29 +0200111 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
112 },
113};
114
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100115static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx);
116
Harald Welte2720e732010-05-17 00:44:57 +0200117/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800118
Harald Weltec2e8cc42010-05-31 20:23:38 +0200119static void mmctx_timer_cb(void *_mm);
120
121static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T,
122 unsigned int seconds)
123{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200124 if (osmo_timer_pending(&mm->timer))
Harald Weltec2e8cc42010-05-31 20:23:38 +0200125 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
126 "timer %u pending\n", T, mm->T);
127 mm->T = T;
128 mm->num_T_exp = 0;
129
130 /* FIXME: we should do this only once ? */
131 mm->timer.data = mm;
132 mm->timer.cb = &mmctx_timer_cb;
133
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200134 osmo_timer_schedule(&mm->timer, seconds, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200135}
136
137static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T)
138{
139 if (mm->T != T)
140 LOGP(DMM, LOGL_ERROR, "Stopping MM timer %u but "
141 "%u is running\n", T, mm->T);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200142 osmo_timer_del(&mm->timer);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200143}
144
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100145time_t gprs_max_time_to_idle(void)
146{
147 return GSM0408_T3314_SECS + GSM0408_MOBILE_REACHABLE_SECS;
148}
149
Harald Welte9b455bf2010-03-14 15:45:01 +0800150/* Send a message through the underlying layer */
Harald Welte8acd88f2010-05-18 10:57:45 +0200151static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +0200152 struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800153{
Harald Welte8acd88f2010-05-18 10:57:45 +0200154 if (mm)
155 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
156
Harald Welte11d7c102010-05-02 11:54:55 +0200157 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte56a01452010-05-31 22:12:30 +0200158 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800159}
160
Harald Welte11d7c102010-05-02 11:54:55 +0200161/* copy identifiers from old message to new message, this
162 * is required so lower layers can route it correctly */
163static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
164{
165 msgb_tlli(msg) = msgb_tlli(old);
166 msgb_bvci(msg) = msgb_bvci(old);
167 msgb_nsei(msg) = msgb_nsei(old);
168}
169
Harald Welte6abf94e2010-05-18 10:35:06 +0200170/* Store BVCI/NSEI in MM context */
171static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
172{
173 mm->bvci = msgb_bvci(msg);
174 mm->nsei = msgb_nsei(msg);
175}
176
177/* Store BVCI/NSEI in MM context */
178static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
179{
180 msgb_tlli(msg) = mm->tlli;
181 msgb_bvci(msg) = mm->bvci;
182 msgb_nsei(msg) = mm->nsei;
183}
184
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200185static void mm_ctx_cleanup_free(struct sgsn_mm_ctx *ctx, const char *log_text)
186{
Jacob Erlbecke671d252015-01-26 14:43:07 +0100187 LOGMMCTXP(LOGL_INFO, ctx, "Cleaning MM context due to %s\n", log_text);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200188
189 /* Mark MM state as deregistered */
190 ctx->mm_state = GMM_DEREGISTERED;
191
Jacob Erlbecke671d252015-01-26 14:43:07 +0100192 sgsn_mm_ctx_cleanup_free(ctx);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200193}
194
Harald Weltef54e7e22010-06-09 10:51:23 +0200195/* Chapter 9.4.18 */
196static int _tx_status(struct msgb *msg, uint8_t cause,
197 struct sgsn_mm_ctx *mmctx, int sm)
198{
199 struct gsm48_hdr *gh;
200
201 /* MMCTX might be NULL! */
202
203 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100204 get_value_string(gsm48_gmm_cause_names, cause));
Harald Weltef54e7e22010-06-09 10:51:23 +0200205
206 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
207 if (sm) {
208 gh->proto_discr = GSM48_PDISC_SM_GPRS;
209 gh->msg_type = GSM48_MT_GSM_STATUS;
210 } else {
211 gh->proto_discr = GSM48_PDISC_MM_GPRS;
212 gh->msg_type = GSM48_MT_GMM_STATUS;
213 }
214 gh->data[0] = cause;
215
216 return gsm48_gmm_sendmsg(msg, 0, mmctx);
217}
Jacob Erlbeck70671422014-10-29 22:57:30 +0100218
Harald Weltef54e7e22010-06-09 10:51:23 +0200219static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
220{
221 struct msgb *msg = gsm48_msgb_alloc();
222
223 mmctx2msgid(msg, mmctx);
224 return _tx_status(msg, cause, mmctx, 0);
Harald Weltef54e7e22010-06-09 10:51:23 +0200225}
Jacob Erlbeck70671422014-10-29 22:57:30 +0100226
Harald Weltef54e7e22010-06-09 10:51:23 +0200227static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
228{
229 struct msgb *msg = gsm48_msgb_alloc();
230
231 mmctx2msgid(msg, mmctx);
232 return _tx_status(msg, cause, mmctx, 1);
Harald Weltef54e7e22010-06-09 10:51:23 +0200233}
234
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200235static int _tx_detach_req(struct msgb *msg, uint8_t detach_type, uint8_t cause,
236 struct sgsn_mm_ctx *mmctx)
237{
238 struct gsm48_hdr *gh;
239
240 /* MMCTX might be NULL! */
241
242 DEBUGP(DMM, "<- GPRS MM DETACH REQ (type: %s, cause: %s)\n",
243 get_value_string(gprs_det_t_mt_strs, detach_type),
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100244 get_value_string(gsm48_gmm_cause_names, cause));
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200245
246 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
247
248 gh->proto_discr = GSM48_PDISC_MM_GPRS;
249 gh->msg_type = GSM48_MT_GMM_DETACH_REQ;
250 gh->data[0] = detach_type & 0x07;
251
252 msgb_tv_put(msg, GSM48_IE_GMM_CAUSE, cause);
253
254 return gsm48_gmm_sendmsg(msg, 0, mmctx);
255}
256
257static int gsm48_tx_gmm_detach_req(struct sgsn_mm_ctx *mmctx,
258 uint8_t detach_type, uint8_t cause)
259{
260 struct msgb *msg = gsm48_msgb_alloc();
261
262 mmctx2msgid(msg, mmctx);
263 return _tx_detach_req(msg, detach_type, cause, mmctx);
264}
265
266static int gsm48_tx_gmm_detach_req_oldmsg(struct msgb *oldmsg,
267 uint8_t detach_type, uint8_t cause)
268{
269 struct msgb *msg = gsm48_msgb_alloc();
270
271 gmm_copy_id(msg, oldmsg);
272 return _tx_detach_req(msg, detach_type, cause, NULL);
273}
Harald Weltef54e7e22010-06-09 10:51:23 +0200274
Harald Welte86fda902010-05-03 10:54:35 +0200275static struct gsm48_qos default_qos = {
276 .delay_class = 4, /* best effort */
277 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
278 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
279 .preced_class = GSM48_QOS_PC_NORMAL,
280 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
281 .traf_class = GSM48_QOS_TC_INTERACTIVE,
282 .deliv_order = GSM48_QOS_DO_UNORDERED,
283 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
284 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
285 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
286 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
287 .resid_ber = GSM48_QOS_RBER_5e_2,
288 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
289 .handling_prio = 3,
290 .xfer_delay = 0x10, /* 200ms */
291 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
292 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
293 .sig_ind = 0, /* not optimised for signalling */
294 .max_bitrate_down_ext = 0, /* use octet 9 */
295 .guar_bitrate_down_ext = 0, /* use octet 13 */
296};
297
Harald Welte9b455bf2010-03-14 15:45:01 +0800298/* Chapter 9.4.2: Attach accept */
Harald Welte6463c072010-05-18 17:04:55 +0200299static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800300{
301 struct msgb *msg = gsm48_msgb_alloc();
302 struct gsm48_hdr *gh;
303 struct gsm48_attach_ack *aa;
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200304 uint8_t *mid;
305#if 0
306 uint8_t *ptsig;
307#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800308
Daniel Willmann46553142014-09-03 17:46:44 +0200309 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800310
Harald Welte6463c072010-05-18 17:04:55 +0200311 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800312
313 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
314 gh->proto_discr = GSM48_PDISC_MM_GPRS;
315 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
316
317 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
318 aa->force_stby = 0; /* not indicated */
319 aa->att_result = 1; /* GPRS only */
Jacob Erlbeck841d95f2015-01-23 12:32:38 +0100320 aa->ra_upd_timer = gprs_secs_to_tmr_floor(GSM0408_T3312_SECS);
Harald Welte9b455bf2010-03-14 15:45:01 +0800321 aa->radio_prio = 4; /* lowest */
Harald Welte6463c072010-05-18 17:04:55 +0200322 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800323
Harald Welteab1d5622010-05-18 19:58:38 +0200324#if 0
Harald Welte6463c072010-05-18 17:04:55 +0200325 /* Optional: P-TMSI signature */
326 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
327 ptsig = msgb_put(msg, 3);
328 ptsig[0] = mm->p_tmsi_sig >> 16;
329 ptsig[1] = mm->p_tmsi_sig >> 8;
330 ptsig[2] = mm->p_tmsi_sig & 0xff;
331
Harald Weltec2e8cc42010-05-31 20:23:38 +0200332#endif
Jacob Erlbeck8de9c482015-01-23 11:44:39 +0100333 /* Optional: Negotiated Ready timer value
334 * (fixed 44s, default value, GSM 04.08, table 11.4a) to safely limit
335 * the inactivity time READY->STANDBY.
336 */
337 msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY,
338 gprs_secs_to_tmr_floor(GSM0408_T3314_SECS));
Harald Welte6463c072010-05-18 17:04:55 +0200339
Harald Weltec2e8cc42010-05-31 20:23:38 +0200340#ifdef PTMSI_ALLOC
Harald Welte6463c072010-05-18 17:04:55 +0200341 /* Optional: Allocated P-TMSI */
Harald Welte6463c072010-05-18 17:04:55 +0200342 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
343 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
Harald Welteab1d5622010-05-18 19:58:38 +0200344 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
345#endif
Harald Weltec2e8cc42010-05-31 20:23:38 +0200346
Harald Welte6463c072010-05-18 17:04:55 +0200347 /* Optional: MS-identity (combined attach) */
348 /* Optional: GMM cause (partial attach result for combined attach) */
349
Harald Welte56a01452010-05-31 22:12:30 +0200350 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800351}
352
353/* Chapter 9.4.5: Attach reject */
Harald Welte6463c072010-05-18 17:04:55 +0200354static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800355{
Harald Welte9b455bf2010-03-14 15:45:01 +0800356 struct gsm48_hdr *gh;
357
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100358 LOGP(DMM, LOGL_NOTICE, "<- GPRS ATTACH REJECT: %s\n", get_value_string(gsm48_gmm_cause_names, gmm_cause));
Harald Welte9b455bf2010-03-14 15:45:01 +0800359
Harald Welte9b455bf2010-03-14 15:45:01 +0800360 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
361 gh->proto_discr = GSM48_PDISC_MM_GPRS;
362 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
363 gh->data[0] = gmm_cause;
364
Harald Welte8acd88f2010-05-18 10:57:45 +0200365 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800366}
Harald Welte6463c072010-05-18 17:04:55 +0200367static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
368 uint8_t gmm_cause)
369{
370 struct msgb *msg = gsm48_msgb_alloc();
371 gmm_copy_id(msg, old_msg);
372 return _tx_gmm_att_rej(msg, gmm_cause);
373}
374static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
375 uint8_t gmm_cause)
376{
377 struct msgb *msg = gsm48_msgb_alloc();
378 mmctx2msgid(msg, mm);
379 return _tx_gmm_att_rej(msg, gmm_cause);
380}
Harald Welte9b455bf2010-03-14 15:45:01 +0800381
Harald Welte2720e732010-05-17 00:44:57 +0200382/* Chapter 9.4.6.2 Detach accept */
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200383static int _tx_detach_ack(struct msgb *msg, uint8_t force_stby,
384 struct sgsn_mm_ctx *mm)
Harald Welte2720e732010-05-17 00:44:57 +0200385{
Harald Welte2720e732010-05-17 00:44:57 +0200386 struct gsm48_hdr *gh;
387
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200388 /* MMCTX might be NULL! */
Harald Welte2720e732010-05-17 00:44:57 +0200389
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200390 DEBUGP(DMM, "<- GPRS MM DETACH ACC (force-standby: %d)\n", force_stby);
Harald Welte6463c072010-05-18 17:04:55 +0200391
Harald Welte2720e732010-05-17 00:44:57 +0200392 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
393 gh->proto_discr = GSM48_PDISC_MM_GPRS;
394 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
395 gh->data[0] = force_stby;
396
Harald Welte56a01452010-05-31 22:12:30 +0200397 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte2720e732010-05-17 00:44:57 +0200398}
399
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200400static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
401{
402 struct msgb *msg = gsm48_msgb_alloc();
403
404 mmctx2msgid(msg, mm);
405 return _tx_detach_ack(msg, force_stby, mm);
406}
407
408static int gsm48_tx_gmm_det_ack_oldmsg(struct msgb *oldmsg, uint8_t force_stby)
409{
410 struct msgb *msg = gsm48_msgb_alloc();
411
412 gmm_copy_id(msg, oldmsg);
413 return _tx_detach_ack(msg, force_stby, NULL);
414}
415
Harald Welte9b455bf2010-03-14 15:45:01 +0800416/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte6463c072010-05-18 17:04:55 +0200417static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800418{
419 struct msgb *msg = gsm48_msgb_alloc();
420 struct gsm48_hdr *gh;
421
Daniel Willmann46553142014-09-03 17:46:44 +0200422 LOGMMCTXP(LOGL_DEBUG, mm, "<- GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800423
Harald Welte6463c072010-05-18 17:04:55 +0200424 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800425
426 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
427 gh->proto_discr = GSM48_PDISC_MM_GPRS;
428 gh->msg_type = GSM48_MT_GMM_ID_REQ;
429 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
430 gh->data[0] = id_type & 0xf;
431
Harald Weltedce12552010-05-31 22:31:04 +0200432 return gsm48_gmm_sendmsg(msg, 1, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800433}
434
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200435/* Section 9.4.9: Authentication and Ciphering Request */
436static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, uint8_t *rand,
437 uint8_t key_seq, uint8_t algo)
438{
439 struct msgb *msg = gsm48_msgb_alloc();
440 struct gsm48_hdr *gh;
441 struct gsm48_auth_ciph_req *acreq;
442 uint8_t *m_rand, *m_cksn;
443
Daniel Willmann46553142014-09-03 17:46:44 +0200444 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS AUTH AND CIPHERING REQ (rand = %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200445 osmo_hexdump(rand, 16));
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200446
447 mmctx2msgid(msg, mm);
448
449 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
450 gh->proto_discr = GSM48_PDISC_MM_GPRS;
451 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ;
452
453 acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq));
454 acreq->ciph_alg = algo & 0xf;
455 acreq->imeisv_req = 0x1;
456 acreq->force_stby = 0x0;
457 acreq->ac_ref_nr = 0x0; /* FIXME: increment this? */
458
459 /* Only if authentication is requested we need to set RAND + CKSN */
460 if (rand) {
461 m_rand = msgb_put(msg, 16+1);
462 m_rand[0] = GSM48_IE_GMM_AUTH_RAND;
463 memcpy(m_rand+1, rand, 16);
464
Jacob Erlbeck65d82732014-12-01 10:36:17 +0100465 m_cksn = msgb_put(msg, 1);
466 m_cksn[0] = (GSM48_IE_GMM_CIPH_CKSN << 4) | (key_seq & 0x07);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200467 }
468
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200469 /* FIXME: make sure we don't send any other messages to the MS */
470
471 return gsm48_gmm_sendmsg(msg, 1, mm);
472}
473
474/* Section 9.4.11: Authentication and Ciphering Reject */
475static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm)
476{
477 struct msgb *msg = gsm48_msgb_alloc();
478 struct gsm48_hdr *gh;
479
Daniel Willmann46553142014-09-03 17:46:44 +0200480 LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS AUTH AND CIPH REJECT\n");
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200481
482 mmctx2msgid(msg, mm);
483
484 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
485 gh->proto_discr = GSM48_PDISC_MM_GPRS;
486 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ;
487
488 return gsm48_gmm_sendmsg(msg, 0, mm);
489}
490
491/* Section 9.4.10: Authentication and Ciphering Response */
492static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx,
493 struct msgb *msg)
494{
495 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
496 struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data;
497 struct tlv_parsed tp;
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100498 struct gsm_auth_tuple *at;
499 int rc;
500
501 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS AUTH AND CIPH RESPONSE\n");
502
503 if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
504 LOGMMCTXP(LOGL_NOTICE, ctx,
505 "Unexpected Auth & Ciph Response (ignored)\n");
506 return 0;
507 }
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200508
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100509 /* Stop T3360 */
510 mmctx_timer_stop(ctx, 3360);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200511
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200512 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data,
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200513 (msg->data + msg->len) - acr->data, 0, 0);
514
515 /* FIXME: compare ac_ref? */
516
517 if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) ||
518 !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV)) {
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100519 /* TODO: missing mandatory IE, return STATUS or REJ? */
520 LOGMMCTXP(LOGL_ERROR, ctx, "Missing mandantory IE\n");
521 return -EINVAL;
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200522 }
523
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100524 /* Compare SRES with what we expected */
525 LOGMMCTXP(LOGL_DEBUG, ctx, "checking received auth info, SRES = %s\n",
526 osmo_hexdump(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES),
527 TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES)));
528
529 at = &ctx->auth_triplet;
530
531 if (TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES) != sizeof(at->sres) ||
532 memcmp(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES), at->sres,
533 sizeof(at->sres)) != 0) {
534
535 LOGMMCTXP(LOGL_NOTICE, ctx, "Received SRES doesn't match\n");
536 rc = gsm48_tx_gmm_auth_ciph_rej(ctx);
537 mm_ctx_cleanup_free(ctx, "GPRS AUTH AND CIPH REJECT");
538 return rc;
539 }
540
541 ctx->is_authenticated = 1;
542
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200543 /* FIXME: enable LLC cipheirng */
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100544
545 /* Check if we can let the mobile station enter */
546 return gsm48_gmm_authorize(ctx);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200547}
548
Holger Hans Peter Freyther20de3ae2015-05-05 22:52:40 +0200549static void extract_subscr_msisdn(struct sgsn_mm_ctx *ctx)
550{
551 struct gsm_mncc_number called;
552 uint8_t msisdn[sizeof(ctx->subscr->sgsn_data->msisdn) + 1];
553
554 /* Convert MSISDN from encoded to string.. */
555 if (!ctx->subscr)
556 return;
557
558 if (ctx->subscr->sgsn_data->msisdn_len < 1)
559 return;
560
561 /* prepare the data for the decoder */
562 memset(&called, 0, sizeof(called));
563 msisdn[0] = ctx->subscr->sgsn_data->msisdn_len;
564 memcpy(&msisdn[1], ctx->subscr->sgsn_data->msisdn,
565 ctx->subscr->sgsn_data->msisdn_len);
566
567 /* decode the string now */
568 gsm48_decode_called(&called, msisdn);
569
570 /* Prepend a '+' for international numbers */
571 if (called.plan == 1 && called.type == 1) {
572 ctx->msisdn[0] = '+';
573 strncpy(&ctx->msisdn[1], called.number,
574 sizeof(ctx->msisdn) - 1);
575 } else {
576 strncpy(&ctx->msisdn[0], called.number,
577 sizeof(ctx->msisdn) - 1);
578 }
579}
580
Holger Hans Peter Freyther8ee13e22015-05-18 10:00:03 +0200581static void extract_subscr_hlr(struct sgsn_mm_ctx *ctx)
582{
583 struct gsm_mncc_number called;
584 uint8_t hlr_number[sizeof(ctx->subscr->sgsn_data->hlr) + 1];
585
586 if (!ctx->subscr)
587 return;
588
589 if (ctx->subscr->sgsn_data->hlr_len < 1)
590 return;
591
592 /* prepare the data for the decoder */
593 memset(&called, 0, sizeof(called));
594 hlr_number[0] = ctx->subscr->sgsn_data->hlr_len;
595 memcpy(&hlr_number[1], ctx->subscr->sgsn_data->hlr,
596 ctx->subscr->sgsn_data->hlr_len);
597
598 /* decode the string now */
599 gsm48_decode_called(&called, hlr_number);
600
601 if (called.plan != 1) {
602 LOGMMCTXP(LOGL_ERROR, ctx,
603 "Numbering plan(%d) not allowed\n",
604 called.plan);
605 return;
606 }
607
608 if (called.type != 1) {
609 LOGMMCTXP(LOGL_ERROR, ctx,
610 "Numbering type(%d) not allowed\n",
611 called.type);
612 return;
613 }
614
615 strncpy(&ctx->hlr[0], called.number, sizeof(ctx->hlr) - 1);
616}
617
Harald Welte9b455bf2010-03-14 15:45:01 +0800618/* Check if we can already authorize a subscriber */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100619static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800620{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200621#ifndef PTMSI_ALLOC
622 struct sgsn_signal_data sig_data;
623#endif
624
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100625 /* Request IMSI and IMEI from the MS if they are unknown */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200626 if (!strlen(ctx->imei)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200627 ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
628 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200629 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200630 }
Harald Weltec2e8cc42010-05-31 20:23:38 +0200631 if (!strlen(ctx->imsi)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200632 ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
633 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200634 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200635 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800636
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100637 /* All information required for authentication is available */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100638 ctx->t3370_id_type = GSM_MI_TYPE_NONE;
639
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200640 if (ctx->auth_state == SGSN_AUTH_UNKNOWN) {
641 /* Request authorization, this leads to a call to
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100642 * sgsn_auth_update which in turn calls
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200643 * gsm0408_gprs_access_granted or gsm0408_gprs_access_denied */
644
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100645 sgsn_auth_request(ctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200646 /* Note that gsm48_gmm_authorize can be called recursively via
647 * sgsn_auth_request iff ctx->auth_info changes to AUTH_ACCEPTED
648 */
649 return 0;
650 }
651
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100652 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && !ctx->is_authenticated) {
653 struct gsm_auth_tuple *at = &ctx->auth_triplet;
654
655 mmctx_timer_start(ctx, 3360, GSM0408_T3360_SECS);
656 return gsm48_tx_gmm_auth_ciph_req(ctx, at->rand, at->key_seq,
657 GPRS_ALGO_GEA0);
658 }
659
660 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && ctx->is_authenticated &&
661 ctx->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
662 /* Check again for authorization */
663 sgsn_auth_request(ctx);
664 return 0;
665 }
666
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200667 if (ctx->auth_state != SGSN_AUTH_ACCEPTED) {
668 LOGMMCTXP(LOGL_NOTICE, ctx,
669 "authorization is denied, aborting procedure\n");
670 return -EACCES;
Jacob Erlbeck0c06f982014-10-29 22:12:20 +0100671 }
672
673 /* The MS is authorized */
674
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100675 switch (ctx->pending_req) {
676 case 0:
677 LOGMMCTXP(LOGL_INFO, ctx,
678 "no pending request, authorization completed\n");
679 break;
680 case GSM48_MT_GMM_ATTACH_REQ:
Holger Hans Peter Freyther20de3ae2015-05-05 22:52:40 +0200681
682 extract_subscr_msisdn(ctx);
Holger Hans Peter Freyther8ee13e22015-05-18 10:00:03 +0200683 extract_subscr_hlr(ctx);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100684#ifdef PTMSI_ALLOC
685 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
686 mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
687 ctx->t3350_mode = GMM_T3350_MODE_ATT;
Jacob Erlbeck0074a772014-10-28 16:23:46 +0100688#else
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +0200689 memset(&sig_data, 0, sizeof(sig_data));
690 sig_data.mm = mmctx;
691 osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data);
Jacob Erlbeck0074a772014-10-28 16:23:46 +0100692 ctx->mm_state = GMM_REGISTERED_NORMAL;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100693#endif
694
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100695 return gsm48_tx_gmm_att_ack(ctx);
696 default:
697 LOGMMCTXP(LOGL_ERROR, ctx,
698 "only Attach Request is supported yet, "
699 "got request type %u\n", ctx->pending_req);
700 break;
701 }
702
Harald Welte9b455bf2010-03-14 15:45:01 +0800703 return 0;
704}
705
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100706void gsm0408_gprs_authenticate(struct sgsn_mm_ctx *ctx)
707{
708 ctx->is_authenticated = 0;
709
710 gsm48_gmm_authorize(ctx);
711}
712
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200713void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx)
714{
715 switch (ctx->mm_state) {
716 case GMM_COMMON_PROC_INIT:
717 LOGP(DMM, LOGL_NOTICE,
718 "Authorized, continuing procedure, IMSI=%s\n",
719 ctx->imsi);
720 /* Continue with the authorization */
721 gsm48_gmm_authorize(ctx);
722 break;
723 default:
724 LOGP(DMM, LOGL_INFO,
725 "Authorized, ignored, IMSI=%s\n",
726 ctx->imsi);
727 }
728}
729
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100730void gsm0408_gprs_access_denied(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200731{
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100732 if (gmm_cause == SGSN_ERROR_CAUSE_NONE)
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100733 gmm_cause = GMM_CAUSE_GPRS_NOTALLOWED;
734
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200735 switch (ctx->mm_state) {
736 case GMM_COMMON_PROC_INIT:
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100737 LOGMMCTXP(LOGL_NOTICE, ctx,
738 "Not authorized, rejecting ATTACH REQUEST "
739 "with cause '%s' (%d)\n",
740 get_value_string(gsm48_gmm_cause_names, gmm_cause),
741 gmm_cause);
742 gsm48_tx_gmm_att_rej(ctx, gmm_cause);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200743 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT");
744 break;
745 case GMM_REGISTERED_NORMAL:
746 case GMM_REGISTERED_SUSPENDED:
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100747 LOGMMCTXP(LOGL_NOTICE, ctx,
748 "Authorization lost, detaching "
749 "with cause '%s' (%d)\n",
750 get_value_string(gsm48_gmm_cause_names, gmm_cause),
751 gmm_cause);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200752 gsm48_tx_gmm_detach_req(
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100753 ctx, GPRS_DET_T_MT_REATT_NOTREQ, gmm_cause);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200754
755 mm_ctx_cleanup_free(ctx, "auth lost");
756 break;
757 default:
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100758 LOGMMCTXP(LOGL_INFO, ctx,
759 "Authorization lost, cause is '%s' (%d)\n",
760 get_value_string(gsm48_gmm_cause_names, gmm_cause),
761 gmm_cause);
762 mm_ctx_cleanup_free(ctx, "auth lost");
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200763 }
764}
765
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100766void gsm0408_gprs_access_cancelled(struct sgsn_mm_ctx *ctx, int gmm_cause)
Jacob Erlbeck98647ca2014-11-11 14:47:38 +0100767{
Jacob Erlbeckd6267d12015-01-19 11:10:04 +0100768 if (gmm_cause != SGSN_ERROR_CAUSE_NONE) {
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100769 LOGMMCTXP(LOGL_INFO, ctx,
770 "Cancelled with cause '%s' (%d), deleting context\n",
771 get_value_string(gsm48_gmm_cause_names, gmm_cause),
772 gmm_cause);
773 gsm0408_gprs_access_denied(ctx, gmm_cause);
774 return;
Jacob Erlbeck98647ca2014-11-11 14:47:38 +0100775 }
Jacob Erlbeckaf3d5c52015-01-05 17:51:17 +0100776
777 LOGMMCTXP(LOGL_INFO, ctx, "Cancelled, deleting context silently\n");
Jacob Erlbeck98647ca2014-11-11 14:47:38 +0100778 mm_ctx_cleanup_free(ctx, "access cancelled");
779}
780
Harald Welte9b455bf2010-03-14 15:45:01 +0800781/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200782static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800783{
Harald Welte943c5bc2010-04-30 16:33:12 +0200784 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200785 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800786 char mi_string[GSM48_MI_SIZE];
Harald Welte9b455bf2010-03-14 15:45:01 +0800787
788 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte9b455bf2010-03-14 15:45:01 +0800789 if (!ctx) {
Daniel Willmann46553142014-09-03 17:46:44 +0200790 DEBUGP(DMM, "from unknown TLLI 0x%08x?!? This should not happen\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800791 return -EINVAL;
792 }
793
Daniel Willmannf8070f42014-09-23 18:48:44 +0200794 LOGMMCTXP(LOGL_DEBUG, ctx, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
795 mi_type, mi_string);
796
Jacob Erlbeckfb26c602014-10-22 12:20:26 +0200797 if (ctx->t3370_id_type == GSM_MI_TYPE_NONE) {
798 LOGMMCTXP(LOGL_NOTICE, ctx,
799 "Got unexpected IDENTITY RESPONSE: mi_type=0x%02x MI(%s), "
800 "ignoring message\n",
801 mi_type, mi_string);
802 return -EINVAL;
803 }
804
Harald Weltec2e8cc42010-05-31 20:23:38 +0200805 if (mi_type == ctx->t3370_id_type)
806 mmctx_timer_stop(ctx, 3370);
807
Harald Welte9b455bf2010-03-14 15:45:01 +0800808 switch (mi_type) {
809 case GSM_MI_TYPE_IMSI:
810 /* we already have a mm context with current TLLI, but no
811 * P-TMSI / IMSI yet. What we now need to do is to fill
812 * this initial context with data from the HLR */
Harald Weltec728eea2010-12-24 23:07:18 +0100813 if (strlen(ctx->imsi) == 0) {
814 /* Check if we already have a MM context for this IMSI */
815 struct sgsn_mm_ctx *ictx;
816 ictx = sgsn_mm_ctx_by_imsi(mi_string);
817 if (ictx) {
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200818 /* Handle it like in gsm48_rx_gmm_det_req,
819 * except that no messages are sent to the BSS */
820
Daniel Willmann46553142014-09-03 17:46:44 +0200821 LOGMMCTXP(LOGL_NOTICE, ctx, "Deleting old MM Context for same IMSI "
822 "p_tmsi_old=0x%08x\n",
823 ictx->p_tmsi);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200824
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200825 mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use");
Harald Weltec728eea2010-12-24 23:07:18 +0100826 }
827 }
Jacob Erlbeck496aee72015-01-26 10:38:12 +0100828 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1);
Harald Welte9b455bf2010-03-14 15:45:01 +0800829 break;
830 case GSM_MI_TYPE_IMEI:
Jacob Erlbeck496aee72015-01-26 10:38:12 +0100831 strncpy(ctx->imei, mi_string, sizeof(ctx->imei) - 1);
Harald Welte9b455bf2010-03-14 15:45:01 +0800832 break;
833 case GSM_MI_TYPE_IMEISV:
834 break;
835 }
836
Harald Welte9b455bf2010-03-14 15:45:01 +0800837 /* Check if we can let the mobile station enter */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100838 return gsm48_gmm_authorize(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800839}
840
841/* Section 9.4.1 Attach request */
Harald Welte807a5d82010-06-01 11:53:01 +0200842static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg,
843 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800844{
Harald Welte943c5bc2010-04-30 16:33:12 +0200845 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200846 uint8_t *cur = gh->data, *msnc, *mi, *ms_ra_acc_cap;
Harald Welte56a01452010-05-31 22:12:30 +0200847 uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len;
Harald Welteeaa614c2010-05-02 11:26:34 +0200848 uint16_t drx_par;
849 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800850 char mi_string[GSM48_MI_SIZE];
851 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200852 uint16_t cid;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100853 enum gsm48_gmm_cause reject_cause;
854 int rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800855
Daniel Willmann46553142014-09-03 17:46:44 +0200856 LOGP(DMM, LOGL_INFO, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800857
858 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
859 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
860 * or with random TLLI. */
861
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200862 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800863
864 /* MS network capability 10.5.5.12 */
865 msnc_len = *cur++;
866 msnc = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400867 if (msnc_len > sizeof(ctx->ms_network_capa.buf))
Harald Welte9b455bf2010-03-14 15:45:01 +0800868 goto err_inval;
869 cur += msnc_len;
870
871 /* aTTACH Type 10.5.5.2 */
872 att_type = *cur++ & 0x0f;
873
874 /* DRX parameter 10.5.5.6 */
Harald Welte56a01452010-05-31 22:12:30 +0200875 drx_par = *cur++ << 8;
876 drx_par |= *cur++;
Harald Welte9b455bf2010-03-14 15:45:01 +0800877
878 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
879 mi_len = *cur++;
880 mi = cur;
881 if (mi_len > 8)
882 goto err_inval;
883 mi_type = *mi & GSM_MI_TYPE_MASK;
884 cur += mi_len;
885
886 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
887
Harald Welte2720e732010-05-17 00:44:57 +0200888 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
889 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800890
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200891 /* Old routing area identification 10.5.5.15. Skip it */
Harald Welte9b455bf2010-03-14 15:45:01 +0800892 cur += 6;
893
894 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte56a01452010-05-31 22:12:30 +0200895 ms_ra_acc_cap_len = *cur++;
896 ms_ra_acc_cap = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400897 if (ms_ra_acc_cap_len > sizeof(ctx->ms_radio_access_capa.buf))
Harald Weltec48ac472010-07-03 21:20:06 +0200898 goto err_inval;
Harald Welte4b2ed352011-07-27 23:35:38 +0200899 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +0800900
Daniel Willmann46553142014-09-03 17:46:44 +0200901 LOGPC(DMM, LOGL_INFO, "\n");
902
Harald Welte9b455bf2010-03-14 15:45:01 +0800903 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
904
905 switch (mi_type) {
906 case GSM_MI_TYPE_IMSI:
907 /* Try to find MM context based on IMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200908 if (!ctx)
909 ctx = sgsn_mm_ctx_by_imsi(mi_string);
Harald Welte9b455bf2010-03-14 15:45:01 +0800910 if (!ctx) {
911#if 0
912 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
913#else
Harald Welte9b455bf2010-03-14 15:45:01 +0800914 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100915 if (!ctx) {
916 reject_cause = GMM_CAUSE_NET_FAIL;
917 goto rejected;
918 }
Jacob Erlbeck496aee72015-01-26 10:38:12 +0100919 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi) - 1);
Harald Welte9b455bf2010-03-14 15:45:01 +0800920#endif
921 }
Harald Welte943c5bc2010-04-30 16:33:12 +0200922 ctx->tlli = msgb_tlli(msg);
Harald Welte807a5d82010-06-01 11:53:01 +0200923 ctx->llme = llme;
Harald Welte68b99a42010-05-18 12:05:42 +0200924 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800925 break;
926 case GSM_MI_TYPE_TMSI:
Harald Welteab1d5622010-05-18 19:58:38 +0200927 memcpy(&tmsi, mi+1, 4);
928 tmsi = ntohl(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800929 /* Try to find MM context based on P-TMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200930 if (!ctx)
931 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800932 if (!ctx) {
Harald Weltec728eea2010-12-24 23:07:18 +0100933 /* Allocate a context as most of our code expects one.
934 * Context will not have an IMSI ultil ID RESP is received */
Harald Welte943c5bc2010-04-30 16:33:12 +0200935 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte807a5d82010-06-01 11:53:01 +0200936 ctx->p_tmsi = tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800937 }
Harald Welte807a5d82010-06-01 11:53:01 +0200938 ctx->tlli = msgb_tlli(msg);
939 ctx->llme = llme;
940 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800941 break;
942 default:
Harald Weltec2e8cc42010-05-31 20:23:38 +0200943 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST with "
944 "MI type %u\n", mi_type);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100945 reject_cause = GMM_CAUSE_MS_ID_NOT_DERIVED;
946 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +0800947 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200948 /* Update MM Context with currient RA and Cell ID */
949 ctx->ra = ra_id;
950 ctx->cell_id = cid;
Harald Welte56a01452010-05-31 22:12:30 +0200951 /* Update MM Context with other data */
952 ctx->drx_parms = drx_par;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400953 ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len;
Harald Welte4b2ed352011-07-27 23:35:38 +0200954 memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap,
955 ctx->ms_radio_access_capa.len);
Harald Welte56a01452010-05-31 22:12:30 +0200956 ctx->ms_network_capa.len = msnc_len;
957 memcpy(ctx->ms_network_capa.buf, msnc, msnc_len);
958
Harald Weltec2e8cc42010-05-31 20:23:38 +0200959#ifdef PTMSI_ALLOC
Harald Welte807a5d82010-06-01 11:53:01 +0200960 /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
Jacob Erlbeckf6e7d992014-11-06 15:43:10 +0100961 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
962 if (ctx->mm_state != GMM_COMMON_PROC_INIT) {
963 ctx->p_tmsi_old = ctx->p_tmsi;
964 ctx->p_tmsi = sgsn_alloc_ptmsi();
965 }
Jacob Erlbeck0074a772014-10-28 16:23:46 +0100966 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welteab1d5622010-05-18 19:58:38 +0200967#endif
Harald Welte807a5d82010-06-01 11:53:01 +0200968 /* Even if there is no P-TMSI allocated, the MS will switch from
969 * foreign TLLI to local TLLI */
970 ctx->tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL);
971
972 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +0200973 gprs_llgmm_assign(ctx->llme, ctx->tlli, ctx->tlli_new,
974 GPRS_ALGO_GEA0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800975
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100976 ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;
977 return gsm48_gmm_authorize(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800978
979err_inval:
Daniel Willmann46553142014-09-03 17:46:44 +0200980 LOGPC(DMM, LOGL_INFO, "\n");
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100981 reject_cause = GMM_CAUSE_SEM_INCORR_MSG;
982
983rejected:
984 /* Send ATTACH REJECT */
985 LOGMMCTXP(LOGL_NOTICE, ctx,
986 "Rejecting Attach Request with cause '%s' (%d)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100987 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100988 rc = gsm48_tx_gmm_att_rej_oldmsg(msg, reject_cause);
989 if (ctx)
990 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJ");
991 else
992 /* TLLI unassignment */
993 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
994
995 return rc;
996
Harald Welte9b455bf2010-03-14 15:45:01 +0800997}
998
Harald Welte2720e732010-05-17 00:44:57 +0200999/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +02001000static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +02001001{
1002 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte2720e732010-05-17 00:44:57 +02001003 uint8_t detach_type, power_off;
Jacob Erlbeck5a38f642014-10-21 13:09:55 +02001004 int rc = 0;
Harald Welte2720e732010-05-17 00:44:57 +02001005
1006 detach_type = gh->data[0] & 0x7;
1007 power_off = gh->data[0] & 0x8;
1008
1009 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
1010
Daniel Willmann46553142014-09-03 17:46:44 +02001011 LOGMMCTXP(LOGL_INFO, ctx, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
Harald Welte2720e732010-05-17 00:44:57 +02001012 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
1013 power_off ? "Power-off" : "");
1014
Jacob Erlbeck5a38f642014-10-21 13:09:55 +02001015 /* Only send the Detach Accept (MO) if power off isn't indicated,
Jacob Erlbeckb9ab0d42014-10-21 09:52:05 +02001016 * see 04.08, 4.7.4.1.2/3 for details */
1017 if (!power_off) {
1018 /* force_stby = 0 */
Jacob Erlbeck5a38f642014-10-21 13:09:55 +02001019 if (ctx)
1020 rc = gsm48_tx_gmm_det_ack(ctx, 0);
1021 else
1022 rc = gsm48_tx_gmm_det_ack_oldmsg(msg, 0);
Jacob Erlbeckb9ab0d42014-10-21 09:52:05 +02001023 }
Harald Weltebd5c9122010-06-28 22:18:53 +02001024
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +02001025 if (ctx) {
1026 struct sgsn_signal_data sig_data;
1027 memset(&sig_data, 0, sizeof(sig_data));
1028 sig_data.mm = ctx;
1029 osmo_signal_dispatch(SS_SGSN, S_SGSN_DETACH, &sig_data);
Jacob Erlbeck5a38f642014-10-21 13:09:55 +02001030 mm_ctx_cleanup_free(ctx, "GPRS DETACH REQUEST");
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +02001031 }
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +02001032
Harald Weltebd5c9122010-06-28 22:18:53 +02001033 return rc;
Harald Welte2720e732010-05-17 00:44:57 +02001034}
1035
Harald Welte9b455bf2010-03-14 15:45:01 +08001036/* Chapter 9.4.15: Routing area update accept */
Harald Welte6463c072010-05-18 17:04:55 +02001037static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +08001038{
1039 struct msgb *msg = gsm48_msgb_alloc();
1040 struct gsm48_hdr *gh;
1041 struct gsm48_ra_upd_ack *rua;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001042 uint8_t *mid;
Harald Welte9b455bf2010-03-14 15:45:01 +08001043
Daniel Willmann46553142014-09-03 17:46:44 +02001044 LOGMMCTXP(LOGL_INFO, mm, "<- ROUTING AREA UPDATE ACCEPT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001045
Harald Welte6463c072010-05-18 17:04:55 +02001046 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001047
1048 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1049 gh->proto_discr = GSM48_PDISC_MM_GPRS;
1050 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
1051
1052 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
1053 rua->force_stby = 0; /* not indicated */
1054 rua->upd_result = 0; /* RA updated */
Jacob Erlbeck841d95f2015-01-23 12:32:38 +01001055 rua->ra_upd_timer = gprs_secs_to_tmr_floor(GSM0408_T3312_SECS);
Harald Welte11d7c102010-05-02 11:54:55 +02001056
Harald Welte6463c072010-05-18 17:04:55 +02001057 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +08001058
Harald Weltec2e8cc42010-05-31 20:23:38 +02001059#if 0
1060 /* Optional: P-TMSI signature */
1061 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
1062 ptsig = msgb_put(msg, 3);
1063 ptsig[0] = mm->p_tmsi_sig >> 16;
1064 ptsig[1] = mm->p_tmsi_sig >> 8;
1065 ptsig[2] = mm->p_tmsi_sig & 0xff;
1066#endif
1067
1068#ifdef PTMSI_ALLOC
1069 /* Optional: Allocated P-TMSI */
1070 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
1071 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
1072 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
1073#endif
1074
Jacob Erlbeck8de9c482015-01-23 11:44:39 +01001075 /* Optional: Negotiated READY timer value */
1076 msgb_tv_put(msg, GSM48_IE_GMM_TIMER_READY,
1077 gprs_secs_to_tmr_floor(GSM0408_T3314_SECS));
1078
Harald Weltec2e8cc42010-05-31 20:23:38 +02001079 /* Option: MS ID, ... */
Harald Welte56a01452010-05-31 22:12:30 +02001080 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001081}
1082
1083/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +02001084static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +08001085{
1086 struct msgb *msg = gsm48_msgb_alloc();
1087 struct gsm48_hdr *gh;
1088
Daniel Willmann46553142014-09-03 17:46:44 +02001089 LOGP(DMM, LOGL_NOTICE, "<- ROUTING AREA UPDATE REJECT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001090
Harald Welte11d7c102010-05-02 11:54:55 +02001091 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001092
1093 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
1094 gh->proto_discr = GSM48_PDISC_MM_GPRS;
1095 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
1096 gh->data[0] = cause;
1097 gh->data[1] = 0; /* ? */
1098
1099 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +02001100 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001101}
1102
Harald Welte588d44e2010-06-09 10:28:29 +02001103static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001104 const uint8_t *pdp_status)
Harald Welte588d44e2010-06-09 10:28:29 +02001105{
1106 struct sgsn_pdp_ctx *pdp, *pdp2;
1107 /* 24.008 4.7.5.1.3: If the PDP context status information element is
1108 * included in ROUTING AREA UPDATE REQUEST message, then the network
1109 * shall deactivate all those PDP contexts locally (without peer to
1110 * peer signalling between the MS and the network), which are not in SM
1111 * state PDP-INACTIVE on network side but are indicated by the MS as
1112 * being in state PDP-INACTIVE. */
1113
1114 llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) {
Harald Weltecdf76cf2011-08-05 21:23:46 +02001115 if (pdp->nsapi < 8) {
1116 if (!(pdp_status[0] & (1 << pdp->nsapi))) {
Daniel Willmann46553142014-09-03 17:46:44 +02001117 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +02001118 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1119 pdp->nsapi, pdp_status[1], pdp_status[0]);
1120 sgsn_delete_pdp_ctx(pdp);
1121 }
1122 } else {
1123 if (!(pdp_status[1] & (1 << (pdp->nsapi - 8)))) {
Daniel Willmann46553142014-09-03 17:46:44 +02001124 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +02001125 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1126 pdp->nsapi, pdp_status[1], pdp_status[0]);
1127 sgsn_delete_pdp_ctx(pdp);
1128 }
Harald Welte588d44e2010-06-09 10:28:29 +02001129 }
1130 }
1131}
1132
Harald Welte9b455bf2010-03-14 15:45:01 +08001133/* Chapter 9.4.14: Routing area update request */
Harald Welte807a5d82010-06-01 11:53:01 +02001134static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1135 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001136{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +02001137#ifndef PTMSI_ALLOC
1138 struct sgsn_signal_data sig_data;
1139#endif
Harald Welte943c5bc2010-04-30 16:33:12 +02001140 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001141 uint8_t *cur = gh->data;
Harald Welte588d44e2010-06-09 10:28:29 +02001142 uint8_t ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +08001143 struct gprs_ra_id old_ra_id;
Harald Welte588d44e2010-06-09 10:28:29 +02001144 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +02001145 uint8_t upd_type;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001146 enum gsm48_gmm_cause reject_cause;
1147 int rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001148
1149 /* Update Type 10.5.5.18 */
1150 upd_type = *cur++ & 0x0f;
1151
Daniel Willmann46553142014-09-03 17:46:44 +02001152 LOGP(DMM, LOGL_INFO, "-> GMM RA UPDATE REQUEST type=\"%s\"\n",
Harald Welte2720e732010-05-17 00:44:57 +02001153 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +08001154
1155 /* Old routing area identification 10.5.5.15 */
1156 gsm48_parse_ra(&old_ra_id, cur);
1157 cur += 6;
1158
1159 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte588d44e2010-06-09 10:28:29 +02001160 ms_ra_acc_cap_len = *cur++;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001161 if (ms_ra_acc_cap_len > 52) {
1162 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1163 goto rejected;
1164 }
Harald Welte4b2ed352011-07-27 23:35:38 +02001165 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +08001166
1167 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
1168 * DRX parameter, MS network capability */
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001169 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur,
Harald Welte588d44e2010-06-09 10:28:29 +02001170 (msg->data + msg->len) - cur, 0, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +08001171
1172 switch (upd_type) {
1173 case GPRS_UPD_T_RA_LA:
1174 case GPRS_UPD_T_RA_LA_IMSI_ATT:
Daniel Willmann46553142014-09-03 17:46:44 +02001175 LOGP(DMM, LOGL_NOTICE, "Update type %i unsupported in Mode III, is your SI13 corrupt?\n", upd_type);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001176 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1177 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +08001178 case GPRS_UPD_T_RA:
1179 case GPRS_UPD_T_PERIODIC:
1180 break;
1181 }
1182
1183 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Weltef6bd3402010-12-23 23:34:43 +01001184 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +08001185 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
Harald Welte2b697a62011-10-16 18:50:56 +02001186 /* send a XID reset to re-set all LLC sequence numbers
1187 * in the MS */
Daniel Willmann46553142014-09-03 17:46:44 +02001188 LOGP(DMM, LOGL_NOTICE, "LLC XID RESET\n");
Harald Welte2b697a62011-10-16 18:50:56 +02001189 gprs_llgmm_reset(llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001190 /* The MS has to perform GPRS attach */
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001191 /* Device is still IMSI attached for CS but initiate GPRS ATTACH,
1192 * see GSM 04.08, 4.7.5.1.4 and G.6 */
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001193 reject_cause = GMM_CAUSE_IMPL_DETACHED;
1194 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +08001195 }
1196
Harald Weltef0901f02010-12-26 10:39:26 +01001197 /* Store new BVCI/NSEI in MM context (FIXME: delay until we ack?) */
1198 msgid2mmctx(mmctx, msg);
1199 /* Bump the statistics of received signalling msgs for this MM context */
1200 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
1201
Harald Welte9f1f3ad2010-05-02 12:56:57 +02001202 /* Update the MM context with the new RA-ID */
1203 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
Harald Weltef0901f02010-12-26 10:39:26 +01001204 /* Update the MM context with the new (i.e. foreign) TLLI */
Harald Welte9f1f3ad2010-05-02 12:56:57 +02001205 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001206 /* FIXME: Update the MM context with the MS radio acc capabilities */
1207 /* FIXME: Update the MM context with the MS network capabilities */
1208
Harald Welte77289c22010-05-18 14:32:29 +02001209 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
1210
Harald Weltec2e8cc42010-05-31 20:23:38 +02001211#ifdef PTMSI_ALLOC
Jacob Erlbeckf6e7d992014-11-06 15:43:10 +01001212 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
1213 if (mmctx->mm_state != GMM_COMMON_PROC_INIT) {
1214 mmctx->p_tmsi_old = mmctx->p_tmsi;
1215 mmctx->p_tmsi = sgsn_alloc_ptmsi();
1216 }
Harald Weltec2e8cc42010-05-31 20:23:38 +02001217 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
1218 mmctx->t3350_mode = GMM_T3350_MODE_RAU;
1219 mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001220
1221 mmctx->mm_state = GMM_COMMON_PROC_INIT;
1222#else
1223 /* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
1224 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +02001225
1226 memset(&sig_data, 0, sizeof(sig_data));
1227 sig_data.mm = mmctx;
1228 osmo_signal_dispatch(SS_SGSN, S_SGSN_UPDATE, &sig_data);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001229#endif
Harald Welte807a5d82010-06-01 11:53:01 +02001230 /* Even if there is no P-TMSI allocated, the MS will switch from
1231 * foreign TLLI to local TLLI */
1232 mmctx->tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001233
Harald Welte807a5d82010-06-01 11:53:01 +02001234 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +02001235 gprs_llgmm_assign(mmctx->llme, mmctx->tlli, mmctx->tlli_new,
1236 GPRS_ALGO_GEA0, NULL);
Harald Welte807a5d82010-06-01 11:53:01 +02001237
Harald Welte588d44e2010-06-09 10:28:29 +02001238 /* Look at PDP Context Status IE and see if MS's view of
1239 * activated/deactivated NSAPIs agrees with our view */
1240 if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) {
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001241 const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS);
Harald Welte588d44e2010-06-09 10:28:29 +02001242 process_ms_ctx_status(mmctx, pdp_status);
1243 }
1244
Harald Welte807a5d82010-06-01 11:53:01 +02001245 /* Send RA UPDATE ACCEPT */
Harald Welte6463c072010-05-18 17:04:55 +02001246 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001247
1248rejected:
1249 /* Send RA UPDATE REJECT */
1250 LOGMMCTXP(LOGL_NOTICE, mmctx,
1251 "Rejecting RA Update Request with cause '%s' (%d)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001252 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001253 rc = gsm48_tx_gmm_ra_upd_rej(msg, reject_cause);
1254 if (mmctx)
1255 mm_ctx_cleanup_free(mmctx, "GPRS RA UPDATE REJ");
1256 else
1257 /* TLLI unassignment */
1258 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0,
1259 NULL);
1260
1261 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001262}
1263
Harald Welted193cb32010-05-17 22:58:03 +02001264static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001265{
1266 struct gsm48_hdr *gh = msgb_l3(msg);
1267
Daniel Willmann46553142014-09-03 17:46:44 +02001268 LOGMMCTXP(LOGL_INFO, mmctx, "-> GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001269 get_value_string(gsm48_gmm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001270
1271 return 0;
1272}
1273
1274/* GPRS Mobility Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001275static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1276 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001277{
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +02001278 struct sgsn_signal_data sig_data;
Harald Welte943c5bc2010-04-30 16:33:12 +02001279 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001280 int rc;
1281
Harald Welte807a5d82010-06-01 11:53:01 +02001282 /* MMCTX can be NULL when called */
1283
Harald Welted193cb32010-05-17 22:58:03 +02001284 if (!mmctx &&
1285 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
Harald Weltec2e8cc42010-05-31 20:23:38 +02001286 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
Harald Welted193cb32010-05-17 22:58:03 +02001287 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001288 /* 4.7.10 */
Jacob Erlbeck14ae5822014-10-28 09:47:03 +01001289 if (gh->msg_type == GSM48_MT_GMM_STATUS) {
1290 /* TLLI unassignment */
1291 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1292 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001293 return 0;
Jacob Erlbeck14ae5822014-10-28 09:47:03 +01001294 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001295
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001296 /* Don't reply or establish a LLME on DETACH_ACK */
1297 if (gh->msg_type == GSM48_MT_GMM_DETACH_ACK) {
1298 /* TLLI unassignment */
1299 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1300 GPRS_ALGO_GEA0, NULL);
1301 }
1302
Jacob Erlbeckc4f9bf32014-11-11 08:55:13 +01001303 gprs_llgmm_reset(llme);
1304
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001305 /* Don't force it into re-attachment */
1306 if (gh->msg_type == GSM48_MT_GMM_DETACH_REQ) {
Jacob Erlbeck5a38f642014-10-21 13:09:55 +02001307 /* Handle Detach Request */
1308 rc = gsm48_rx_gmm_det_req(NULL, msg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001309
1310 /* TLLI unassignment */
1311 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1312 GPRS_ALGO_GEA0, NULL);
1313 return rc;
1314 }
1315
1316 /* Force the MS to re-attach */
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001317 rc = sgsn_force_reattach_oldmsg(msg);
1318
1319 /* TLLI unassignment */
1320 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1321 GPRS_ALGO_GEA0, NULL);
1322 return rc;
Harald Welted193cb32010-05-17 22:58:03 +02001323 }
1324
Harald Welte9b455bf2010-03-14 15:45:01 +08001325 switch (gh->msg_type) {
1326 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001327 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001328 break;
1329 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001330 rc = gsm48_rx_gmm_att_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001331 break;
Daniel Willmann46553142014-09-03 17:46:44 +02001332 /* For all the following types mmctx can not be NULL */
Harald Welte9b455bf2010-03-14 15:45:01 +08001333 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +02001334 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001335 break;
1336 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001337 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001338 break;
Harald Welte2720e732010-05-17 00:44:57 +02001339 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001340 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +02001341 break;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001342 case GSM48_MT_GMM_DETACH_ACK:
1343 LOGMMCTXP(LOGL_INFO, mmctx, "-> DETACH ACK\n");
1344 mm_ctx_cleanup_free(mmctx, "GPRS DETACH ACK");
1345 rc = 0;
1346 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001347 case GSM48_MT_GMM_ATTACH_COMPL:
1348 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001349 LOGMMCTXP(LOGL_INFO, mmctx, "-> ATTACH COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001350 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001351 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001352 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001353 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001354 /* Unassign the old TLLI */
1355 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001356 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1357 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001358 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001359 rc = 0;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +02001360
1361 memset(&sig_data, 0, sizeof(sig_data));
1362 sig_data.mm = mmctx;
1363 osmo_signal_dispatch(SS_SGSN, S_SGSN_ATTACH, &sig_data);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001364 break;
Harald Welteab1d5622010-05-18 19:58:38 +02001365 case GSM48_MT_GMM_RA_UPD_COMPL:
1366 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001367 LOGMMCTXP(LOGL_INFO, mmctx, "-> ROUTEING AREA UPDATE COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001368 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001369 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001370 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001371 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001372 /* Unassign the old TLLI */
1373 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001374 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1375 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001376 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001377 rc = 0;
Holger Hans Peter Freytherb1008952015-05-02 19:55:38 +02001378
1379 memset(&sig_data, 0, sizeof(sig_data));
1380 sig_data.mm = mmctx;
1381 osmo_signal_dispatch(SS_SGSN, S_SGSN_UPDATE, &sig_data);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001382 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001383 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
Daniel Willmann46553142014-09-03 17:46:44 +02001384 LOGMMCTXP(LOGL_INFO, mmctx, "-> PTMSI REALLLICATION COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001385 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001386 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001387 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001388 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001389 /* Unassign the old TLLI */
1390 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001391 //gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new, GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001392 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001393 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001394 case GSM48_MT_GMM_AUTH_CIPH_RESP:
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001395 rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001396 break;
1397 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001398 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001399 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001400 rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001401 break;
1402 }
1403
1404 return rc;
1405}
1406
Harald Weltec2e8cc42010-05-31 20:23:38 +02001407static void mmctx_timer_cb(void *_mm)
1408{
1409 struct sgsn_mm_ctx *mm = _mm;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +01001410 struct gsm_auth_tuple *at;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001411
1412 mm->num_T_exp++;
1413
1414 switch (mm->T) {
1415 case 3350: /* waiting for ATTACH COMPLETE */
1416 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001417 LOGMMCTXP(LOGL_NOTICE, mm, "T3350 expired >= 5 times\n");
Jacob Erlbecka7904562014-11-03 10:12:52 +01001418 mm_ctx_cleanup_free(mm, "T3350");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001419 /* FIXME: should we return some error? */
1420 break;
1421 }
1422 /* re-transmit the respective msg and re-start timer */
1423 switch (mm->t3350_mode) {
1424 case GMM_T3350_MODE_ATT:
1425 gsm48_tx_gmm_att_ack(mm);
1426 break;
1427 case GMM_T3350_MODE_RAU:
1428 gsm48_tx_gmm_ra_upd_ack(mm);
1429 break;
1430 case GMM_T3350_MODE_PTMSI_REALL:
1431 /* FIXME */
1432 break;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001433 case GMM_T3350_MODE_NONE:
1434 LOGMMCTXP(LOGL_NOTICE, mm,
1435 "T3350 mode wasn't set, ignoring timeout\n");
1436 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001437 }
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001438 osmo_timer_schedule(&mm->timer, GSM0408_T3350_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001439 break;
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001440 case 3360: /* waiting for AUTH AND CIPH RESP */
1441 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001442 LOGMMCTXP(LOGL_NOTICE, mm, "T3360 expired >= 5 times\n");
Jacob Erlbecka7904562014-11-03 10:12:52 +01001443 mm_ctx_cleanup_free(mm, "T3360");
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001444 break;
1445 }
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +01001446 /* Re-transmit the respective msg and re-start timer */
1447 if (mm->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
1448 LOGMMCTXP(LOGL_ERROR, mm,
1449 "timeout: invalid auth triplet reference\n");
1450 mm_ctx_cleanup_free(mm, "T3360");
1451 break;
1452 }
1453 at = &mm->auth_triplet;
1454
1455 gsm48_tx_gmm_auth_ciph_req(mm, at->rand, at->key_seq, GPRS_ALGO_GEA0);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001456 osmo_timer_schedule(&mm->timer, GSM0408_T3360_SECS, 0);
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001457 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001458 case 3370: /* waiting for IDENTITY RESPONSE */
1459 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001460 LOGMMCTXP(LOGL_NOTICE, mm, "T3370 expired >= 5 times\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001461 gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED);
Jacob Erlbecka7904562014-11-03 10:12:52 +01001462 mm_ctx_cleanup_free(mm, "GPRS ATTACH REJECT (T3370)");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001463 break;
1464 }
1465 /* re-tranmit IDENTITY REQUEST and re-start timer */
1466 gsm48_tx_gmm_id_req(mm, mm->t3370_id_type);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001467 osmo_timer_schedule(&mm->timer, GSM0408_T3370_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001468 break;
1469 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001470 LOGMMCTXP(LOGL_ERROR, mm, "timer expired in unknown mode %u\n",
Harald Weltec2e8cc42010-05-31 20:23:38 +02001471 mm->T);
1472 }
1473}
1474
1475/* GPRS SESSION MANAGEMENT */
1476
Harald Weltea9b473a2010-12-24 21:13:26 +01001477static void pdpctx_timer_cb(void *_mm);
1478
1479static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T,
1480 unsigned int seconds)
1481{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001482 if (osmo_timer_pending(&pdp->timer))
Harald Weltea9b473a2010-12-24 21:13:26 +01001483 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
1484 "timer %u pending\n", T, pdp->T);
1485 pdp->T = T;
1486 pdp->num_T_exp = 0;
1487
1488 /* FIXME: we should do this only once ? */
1489 pdp->timer.data = pdp;
1490 pdp->timer.cb = &pdpctx_timer_cb;
1491
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001492 osmo_timer_schedule(&pdp->timer, seconds, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001493}
1494
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001495#if 0
Harald Welte421cba42010-05-02 23:11:50 +02001496static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
1497{
1498 uint8_t v[6];
1499
1500 v[0] = PDP_TYPE_ORG_IETF;
1501 v[1] = PDP_TYPE_N_IETF_IPv4;
1502 *(uint32_t *)(v+2) = htonl(ipaddr);
1503
1504 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1505}
1506
1507static void msgb_put_pdp_addr_ppp(struct msgb *msg)
1508{
1509 uint8_t v[2];
1510
1511 v[0] = PDP_TYPE_ORG_ETSI;
1512 v[1] = PDP_TYPE_N_ETSI_PPP;
1513
1514 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1515}
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001516#endif
Harald Welte421cba42010-05-02 23:11:50 +02001517
Harald Welte9b455bf2010-03-14 15:45:01 +08001518/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +02001519int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +08001520{
Harald Welte9b455bf2010-03-14 15:45:01 +08001521 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +08001522 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +02001523 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001524
Daniel Willmann46553142014-09-03 17:46:44 +02001525 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001526
Harald Welte6abf94e2010-05-18 10:35:06 +02001527 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001528
1529 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1530 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1531 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +02001532
1533 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +02001534 msgb_v_put(msg, pdp->sapi);
Harald Welte4e38ac72010-05-18 14:51:18 +02001535
Harald Welte55e0df72010-05-18 13:20:08 +02001536 /* FIXME: copy QoS parameters from original request */
1537 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
1538 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte4e38ac72010-05-18 14:51:18 +02001539
Harald Welte421cba42010-05-02 23:11:50 +02001540 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +02001541 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte4e38ac72010-05-18 14:51:18 +02001542
Harald Welte421cba42010-05-02 23:11:50 +02001543 /* PDP address */
Harald Welte4e38ac72010-05-18 14:51:18 +02001544 /* Highest 4 bits of first byte need to be set to 1, otherwise
1545 * the IE is identical with the 04.08 PDP Address IE */
1546 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte6abf94e2010-05-18 10:35:06 +02001547 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
1548 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001549 pdp->lib->eua.v[0] |= 0xf0;
1550
Harald Welte77289c22010-05-18 14:32:29 +02001551 /* Optional: Protocol configuration options (FIXME: why 'req') */
Holger Hans Peter Freytherd2195072014-04-04 11:55:21 +02001552 if (pdp->lib->pco_req.l)
Harald Welte6abf94e2010-05-18 10:35:06 +02001553 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Welte3c0b9b92010-05-18 14:36:11 +02001554 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001555
Harald Welte421cba42010-05-02 23:11:50 +02001556 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +08001557
Harald Welte8acd88f2010-05-18 10:57:45 +02001558 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001559}
1560
Harald Welte6abf94e2010-05-18 10:35:06 +02001561/* Section 9.5.3: Activate PDP Context reject */
1562int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
1563 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
1564{
1565 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte6abf94e2010-05-18 10:35:06 +02001566 struct gsm48_hdr *gh;
1567 uint8_t transaction_id = tid ^ 0x8; /* flip */
1568
Daniel Willmann46553142014-09-03 17:46:44 +02001569 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte6abf94e2010-05-18 10:35:06 +02001570
1571 mmctx2msgid(msg, mm);
1572
1573 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1574 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1575 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
1576
1577 msgb_v_put(msg, cause);
1578 if (pco_len && pco_v)
1579 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
1580
Harald Welte8acd88f2010-05-18 10:57:45 +02001581 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte6abf94e2010-05-18 10:35:06 +02001582}
1583
Harald Weltea9b473a2010-12-24 21:13:26 +01001584/* Section 9.5.8: Deactivate PDP Context Request */
1585static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
1586 uint8_t sm_cause)
1587{
1588 struct msgb *msg = gsm48_msgb_alloc();
1589 struct gsm48_hdr *gh;
1590 uint8_t transaction_id = tid ^ 0x8; /* flip */
1591
Daniel Willmann46553142014-09-03 17:46:44 +02001592 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001593
1594 mmctx2msgid(msg, mm);
1595
1596 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1597 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1598 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
1599
1600 msgb_v_put(msg, sm_cause);
1601
1602 return gsm48_gmm_sendmsg(msg, 0, mm);
1603}
1604int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause)
1605{
1606 pdpctx_timer_start(pdp, 3395, GSM0408_T3395_SECS);
1607
1608 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause);
1609}
1610
Harald Welte9b455bf2010-03-14 15:45:01 +08001611/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte60ebc022010-06-08 18:08:59 +02001612static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
Harald Welte9b455bf2010-03-14 15:45:01 +08001613{
Harald Welte9b455bf2010-03-14 15:45:01 +08001614 struct msgb *msg = gsm48_msgb_alloc();
1615 struct gsm48_hdr *gh;
Harald Welte60ebc022010-06-08 18:08:59 +02001616 uint8_t transaction_id = tid ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001617
Daniel Willmann46553142014-09-03 17:46:44 +02001618 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001619
Harald Welte60ebc022010-06-08 18:08:59 +02001620 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001621
1622 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1623 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1624 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
1625
Harald Welte60ebc022010-06-08 18:08:59 +02001626 return gsm48_gmm_sendmsg(msg, 0, mm);
1627}
1628int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
1629{
1630 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
Harald Welte9b455bf2010-03-14 15:45:01 +08001631}
1632
1633/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +02001634static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
1635 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001636{
Harald Welte943c5bc2010-04-30 16:33:12 +02001637 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001638 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welte0c3eae02010-05-02 21:07:14 +02001639 uint8_t req_qos_len, req_pdpa_len;
1640 uint8_t *req_qos, *req_pdpa;
1641 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +02001642 uint8_t transaction_id = (gh->proto_discr >> 4);
Harald Welte11ff3e82010-07-02 10:17:24 +02001643 struct sgsn_ggsn_ctx *ggsn;
Harald Welte6abf94e2010-05-18 10:35:06 +02001644 struct sgsn_pdp_ctx *pdp;
Jacob Erlbeck277b71e2015-02-02 18:03:05 +01001645 enum gsm48_gsm_cause gsm_cause;
Harald Welte9b455bf2010-03-14 15:45:01 +08001646
Daniel Willmann46553142014-09-03 17:46:44 +02001647 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
Harald Welte6abf94e2010-05-18 10:35:06 +02001648 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8acd88f2010-05-18 10:57:45 +02001649
Harald Welte23c13762010-07-02 22:52:04 +02001650 /* FIXME: length checks! */
Harald Welte0c3eae02010-05-02 21:07:14 +02001651 req_qos_len = act_req->data[0];
1652 req_qos = act_req->data + 1; /* 10.5.6.5 */
1653 req_pdpa_len = act_req->data[1 + req_qos_len];
1654 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +08001655
Harald Welte23c13762010-07-02 22:52:04 +02001656 /* Optional: Access Point Name, Protocol Config Options */
1657 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
1658 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
1659 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
1660 else
1661 memset(&tp, 0, sizeof(tp));
1662
Harald Welte0c3eae02010-05-02 21:07:14 +02001663 switch (req_pdpa[0] & 0xf) {
1664 case 0x0:
1665 DEBUGPC(DMM, "ETSI ");
1666 break;
1667 case 0x1:
1668 DEBUGPC(DMM, "IETF ");
1669 break;
1670 case 0xf:
1671 DEBUGPC(DMM, "Empty ");
1672 break;
1673 }
1674
1675 switch (req_pdpa[1]) {
1676 case 0x21:
1677 DEBUGPC(DMM, "IPv4 ");
1678 if (req_pdpa_len >= 6) {
1679 struct in_addr ia;
1680 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
1681 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
1682 }
1683 break;
1684 case 0x57:
1685 DEBUGPC(DMM, "IPv6 ");
1686 if (req_pdpa_len >= 18) {
1687 /* FIXME: print IPv6 address */
1688 }
1689 break;
1690 default:
1691 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
1692 break;
1693 }
1694
Daniel Willmann46553142014-09-03 17:46:44 +02001695 LOGPC(DMM, LOGL_INFO, "\n");
Harald Welte77289c22010-05-18 14:32:29 +02001696
Harald Welte2720e732010-05-17 00:44:57 +02001697 /* put the non-TLV elements in the TLV parser structure to
1698 * pass them on to the SGSN / GTP code */
1699 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
1700 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
1701 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
1702 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
1703
Harald Welte6abf94e2010-05-18 10:35:06 +02001704 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
1705 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
1706 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
1707 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1708 GSM_CAUSE_INV_MAND_INFO,
1709 0, NULL);
1710 }
1711
1712 /* Check if NSAPI is already in use */
Harald Welteb37515e2010-07-02 10:12:58 +02001713 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
1714 if (pdp) {
1715 /* We already have a PDP context for this TLLI + NSAPI tuple */
1716 if (pdp->sapi == act_req->req_llc_sapi &&
1717 pdp->ti == transaction_id) {
1718 /* This apparently is a re-transmission of a PDP CTX
1719 * ACT REQ (our ACT ACK must have got dropped) */
1720 return gsm48_tx_gsm_act_pdp_acc(pdp);
1721 }
1722
1723 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
Harald Welte6abf94e2010-05-18 10:35:06 +02001724 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1725 GSM_CAUSE_NSAPI_IN_USE,
1726 0, NULL);
1727 }
Harald Welte9b455bf2010-03-14 15:45:01 +08001728
Harald Welte19f9e302010-07-02 10:15:49 +02001729 /* Only increment counter for a real activation, after we checked
1730 * for re-transmissions */
1731 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
1732
Jacob Erlbeck277b71e2015-02-02 18:03:05 +01001733 /* Determine GGSN based on APN and subscription options */
1734 ggsn = sgsn_mm_ctx_find_ggsn_ctx(mmctx, &tp, &gsm_cause);
Harald Welte11ff3e82010-07-02 10:17:24 +02001735 if (!ggsn) {
Jacob Erlbeck277b71e2015-02-02 18:03:05 +01001736 LOGP(DGPRS, LOGL_ERROR, "No GGSN context found!\n");
1737 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1738 gsm_cause, 0, NULL);
Harald Welted193cb32010-05-17 22:58:03 +02001739 }
Jacob Erlbeck277b71e2015-02-02 18:03:05 +01001740 LOGMMCTXP(LOGL_DEBUG, mmctx, "Using GGSN %d\n", ggsn->id);
Harald Welte11ff3e82010-07-02 10:17:24 +02001741 ggsn->gsn = sgsn->gsn;
1742 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, act_req->req_nsapi, &tp);
1743 if (!pdp)
1744 return -1;
1745
1746 /* Store SAPI and Transaction Identifier */
1747 pdp->sapi = act_req->req_llc_sapi;
1748 pdp->ti = transaction_id;
1749
Harald Welte6463c072010-05-18 17:04:55 +02001750 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +08001751}
1752
1753/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welte77289c22010-05-18 14:32:29 +02001754static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001755{
Harald Welte943c5bc2010-04-30 16:33:12 +02001756 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte77289c22010-05-18 14:32:29 +02001757 uint8_t transaction_id = (gh->proto_discr >> 4);
1758 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001759
Daniel Willmann46553142014-09-03 17:46:44 +02001760 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001761 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001762
Harald Welte77289c22010-05-18 14:32:29 +02001763 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1764 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001765 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
Harald Welte77289c22010-05-18 14:32:29 +02001766 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1767 mm->imsi, transaction_id);
Harald Welte60ebc022010-06-08 18:08:59 +02001768 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
Harald Welte77289c22010-05-18 14:32:29 +02001769 }
1770
1771 return sgsn_delete_pdp_ctx(pdp);
Harald Welte9b455bf2010-03-14 15:45:01 +08001772}
1773
Harald Weltea9b473a2010-12-24 21:13:26 +01001774/* Section 9.5.9: Deactivate PDP Context Accept */
1775static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
1776{
1777 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
1778 uint8_t transaction_id = (gh->proto_discr >> 4);
1779 struct sgsn_pdp_ctx *pdp;
1780
Daniel Willmann46553142014-09-03 17:46:44 +02001781 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001782
1783 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1784 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001785 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
Harald Weltea9b473a2010-12-24 21:13:26 +01001786 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1787 mm->imsi, transaction_id);
1788 return 0;
1789 }
1790
1791 return sgsn_delete_pdp_ctx(pdp);
1792}
1793
Harald Welted193cb32010-05-17 22:58:03 +02001794static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001795{
1796 struct gsm48_hdr *gh = msgb_l3(msg);
1797
Daniel Willmann46553142014-09-03 17:46:44 +02001798 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001799 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001800
1801 return 0;
1802}
1803
Harald Weltea9b473a2010-12-24 21:13:26 +01001804static void pdpctx_timer_cb(void *_pdp)
1805{
1806 struct sgsn_pdp_ctx *pdp = _pdp;
1807
1808 pdp->num_T_exp++;
1809
1810 switch (pdp->T) {
1811 case 3395: /* waiting for PDP CTX DEACT ACK */
1812 if (pdp->num_T_exp >= 4) {
Daniel Willmann46553142014-09-03 17:46:44 +02001813 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired >= 5 times\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001814 pdp->state = PDP_STATE_INACTIVE;
1815 sgsn_delete_pdp_ctx(pdp);
1816 break;
1817 }
1818 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001819 osmo_timer_schedule(&pdp->timer, GSM0408_T3395_SECS, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001820 break;
1821 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001822 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
Harald Weltea9b473a2010-12-24 21:13:26 +01001823 pdp->T);
1824 }
1825}
1826
1827
Harald Welte9b455bf2010-03-14 15:45:01 +08001828/* GPRS Session Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001829static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1830 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001831{
Harald Welte943c5bc2010-04-30 16:33:12 +02001832 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001833 int rc;
1834
Harald Welte807a5d82010-06-01 11:53:01 +02001835 /* MMCTX can be NULL when called */
1836
Harald Welted193cb32010-05-17 22:58:03 +02001837 if (!mmctx) {
1838 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001839 /* 6.1.3.6 */
1840 if (gh->msg_type == GSM48_MT_GSM_STATUS)
1841 return 0;
1842
1843 return sgsn_force_reattach_oldmsg(msg);
Harald Welted193cb32010-05-17 22:58:03 +02001844 }
1845
Harald Welte9b455bf2010-03-14 15:45:01 +08001846 switch (gh->msg_type) {
1847 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001848 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001849 break;
1850 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001851 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte77289c22010-05-18 14:32:29 +02001852 break;
Harald Weltea9b473a2010-12-24 21:13:26 +01001853 case GSM48_MT_GSM_DEACT_PDP_ACK:
1854 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
1855 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001856 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001857 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001858 break;
1859 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
1860 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
1861 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
Daniel Willmann46553142014-09-03 17:46:44 +02001862 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001863 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001864 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001865 break;
1866 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001867 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001868 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001869 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001870 break;
1871
1872 }
1873
1874 return rc;
1875}
1876
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001877int gsm0408_gprs_force_reattach_oldmsg(struct msgb *msg)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001878{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001879 int rc;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001880 gprs_llgmm_reset_oldmsg(msg, GPRS_SAPI_GMM);
1881
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001882 rc = gsm48_tx_gmm_detach_req_oldmsg(
1883 msg, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001884
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001885 return rc;
1886}
1887
1888int gsm0408_gprs_force_reattach(struct sgsn_mm_ctx *mmctx)
1889{
1890 int rc;
1891 gprs_llgmm_reset(mmctx->llme);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001892
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001893 rc = gsm48_tx_gmm_detach_req(
1894 mmctx, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001895
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001896 mm_ctx_cleanup_free(mmctx, "forced reattach");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001897
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001898 return rc;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001899}
1900
Harald Welte9b455bf2010-03-14 15:45:01 +08001901/* Main entry point for incoming 04.08 GPRS messages */
Harald Welte807a5d82010-06-01 11:53:01 +02001902int gsm0408_gprs_rcvmsg(struct msgb *msg, struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001903{
Harald Welte943c5bc2010-04-30 16:33:12 +02001904 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001905 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +02001906 struct sgsn_mm_ctx *mmctx;
1907 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +08001908 int rc = -EINVAL;
1909
Harald Welted193cb32010-05-17 22:58:03 +02001910 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
1911 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8acd88f2010-05-18 10:57:45 +02001912 if (mmctx) {
Harald Welte6abf94e2010-05-18 10:35:06 +02001913 msgid2mmctx(mmctx, msg);
Harald Welte8acd88f2010-05-18 10:57:45 +02001914 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
Harald Welte807a5d82010-06-01 11:53:01 +02001915 mmctx->llme = llme;
Harald Welte8acd88f2010-05-18 10:57:45 +02001916 }
Harald Welted193cb32010-05-17 22:58:03 +02001917
1918 /* MMCTX can be NULL */
1919
Harald Welte9b455bf2010-03-14 15:45:01 +08001920 switch (pdisc) {
1921 case GSM48_PDISC_MM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001922 rc = gsm0408_rcv_gmm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001923 break;
1924 case GSM48_PDISC_SM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001925 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001926 break;
1927 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001928 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001929 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +02001930 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +08001931 break;
1932 }
1933
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +02001934 /* MMCTX can be invalid */
1935
Harald Welte9b455bf2010-03-14 15:45:01 +08001936 return rc;
1937}
Harald Welte5bfe4992010-06-09 11:22:47 +02001938
1939int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli)
1940{
1941 struct sgsn_mm_ctx *mmctx;
1942
1943 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1944 if (!mmctx) {
1945 LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown "
1946 "TLLI=%08x\n", tlli);
1947 return -EINVAL;
1948 }
1949
Jacob Erlbeck7dba11f2014-12-22 17:58:18 +01001950 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1951 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmann46553142014-09-03 17:46:44 +02001952 LOGMMCTXP(LOGL_NOTICE, mmctx, "SUSPEND request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001953 "!= REGISTERED (TLLI=%08x)\n", tlli);
1954 return -EINVAL;
1955 }
1956
1957 /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */
1958 mmctx->mm_state = GMM_REGISTERED_SUSPENDED;
1959 return 0;
1960}
1961
1962int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli,
1963 uint8_t suspend_ref)
1964{
1965 struct sgsn_mm_ctx *mmctx;
1966
1967 /* FIXME: make use of suspend reference? */
1968
1969 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1970 if (!mmctx) {
1971 LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown "
1972 "TLLI=%08x\n", tlli);
1973 return -EINVAL;
1974 }
1975
Jacob Erlbeck7dba11f2014-12-22 17:58:18 +01001976 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1977 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmann46553142014-09-03 17:46:44 +02001978 LOGMMCTXP(LOGL_NOTICE, mmctx, "RESUME request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001979 "!= SUSPENDED (TLLI=%08x)\n", tlli);
1980 /* FIXME: should we not simply ignore it? */
1981 return -EINVAL;
1982 }
1983
1984 /* Transition from SUSPENDED to NORMAL */
1985 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1986 return 0;
1987}