blob: d762ac4aa7089531c0b8b31f29105e33ce35c413 [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>
Harald Welted193cb32010-05-17 22:58:03 +020053#include <openbsc/sgsn.h>
54
Harald Welte6abf94e2010-05-18 10:35:06 +020055#include <pdp.h>
56
Harald Weltec2e8cc42010-05-31 20:23:38 +020057#define PTMSI_ALLOC
58
Harald Welte649e1ff2013-07-21 17:41:46 +080059/* Section 11.2.2 / Table 11.4 MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020060#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
61#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
62#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
63#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
64
Harald Welte649e1ff2013-07-21 17:41:46 +080065/* Section 11.2.2 / Table 11.4a MM timers netwokr side */
Harald Weltec2e8cc42010-05-31 20:23:38 +020066#define GSM0408_T3313_SECS 30 /* waiting for paging response */
67#define GSM0408_T3314_SECS 44 /* force to STBY on expiry */
68#define GSM0408_T3316_SECS 44
69
Harald Weltea9b473a2010-12-24 21:13:26 +010070/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
71#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
72#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
73#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
74#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
75
Harald Welted193cb32010-05-17 22:58:03 +020076extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080077
Harald Welte588d44e2010-06-09 10:28:29 +020078static const struct tlv_definition gsm48_gmm_att_tlvdef = {
79 .def = {
Harald Welte4b6ac1e2010-07-03 11:09:06 +020080 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
Harald Welte588d44e2010-06-09 10:28:29 +020081 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
82 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +020083 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
84 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
85 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
Harald Welte588d44e2010-06-09 10:28:29 +020086 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +020087 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
Harald Welte588d44e2010-06-09 10:28:29 +020088 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
89 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
90 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
91 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
92 },
93};
94
95static const struct tlv_definition gsm48_sm_att_tlvdef = {
96 .def = {
97 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
98 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
99 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
100 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
101 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
102 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200103 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
104 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
Harald Welte588d44e2010-06-09 10:28:29 +0200105 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
106 },
107};
108
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100109static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx);
110
Harald Welte2720e732010-05-17 00:44:57 +0200111/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800112
Harald Weltec2e8cc42010-05-31 20:23:38 +0200113static void mmctx_timer_cb(void *_mm);
114
115static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T,
116 unsigned int seconds)
117{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200118 if (osmo_timer_pending(&mm->timer))
Harald Weltec2e8cc42010-05-31 20:23:38 +0200119 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
120 "timer %u pending\n", T, mm->T);
121 mm->T = T;
122 mm->num_T_exp = 0;
123
124 /* FIXME: we should do this only once ? */
125 mm->timer.data = mm;
126 mm->timer.cb = &mmctx_timer_cb;
127
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200128 osmo_timer_schedule(&mm->timer, seconds, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200129}
130
131static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T)
132{
133 if (mm->T != T)
134 LOGP(DMM, LOGL_ERROR, "Stopping MM timer %u but "
135 "%u is running\n", T, mm->T);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200136 osmo_timer_del(&mm->timer);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200137}
138
Harald Welte9b455bf2010-03-14 15:45:01 +0800139/* Send a message through the underlying layer */
Harald Welte8acd88f2010-05-18 10:57:45 +0200140static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +0200141 struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800142{
Harald Welte8acd88f2010-05-18 10:57:45 +0200143 if (mm)
144 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
145
Harald Welte11d7c102010-05-02 11:54:55 +0200146 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte56a01452010-05-31 22:12:30 +0200147 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800148}
149
Harald Welte11d7c102010-05-02 11:54:55 +0200150/* copy identifiers from old message to new message, this
151 * is required so lower layers can route it correctly */
152static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
153{
154 msgb_tlli(msg) = msgb_tlli(old);
155 msgb_bvci(msg) = msgb_bvci(old);
156 msgb_nsei(msg) = msgb_nsei(old);
157}
158
Harald Welte6abf94e2010-05-18 10:35:06 +0200159/* Store BVCI/NSEI in MM context */
160static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
161{
162 mm->bvci = msgb_bvci(msg);
163 mm->nsei = msgb_nsei(msg);
164}
165
166/* Store BVCI/NSEI in MM context */
167static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
168{
169 msgb_tlli(msg) = mm->tlli;
170 msgb_bvci(msg) = mm->bvci;
171 msgb_nsei(msg) = mm->nsei;
172}
173
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200174static void delete_pdp_contexts(struct sgsn_mm_ctx *ctx, const char *log_text)
175{
176 struct sgsn_pdp_ctx *pdp, *pdp2;
177
178 /* delete all existing PDP contexts for this MS */
179 llist_for_each_entry_safe(pdp, pdp2, &ctx->pdp_list, list) {
180 LOGMMCTXP(LOGL_NOTICE, ctx,
181 "Dropping PDP context for NSAPI=%u due to %s\n",
182 pdp->nsapi, log_text);
183 sgsn_pdp_ctx_terminate(pdp);
184 }
185}
186
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200187static void mm_ctx_cleanup_free(struct sgsn_mm_ctx *ctx, const char *log_text)
188{
189 struct gprs_llc_llme *llme = ctx->llme;
190 uint32_t tlli = ctx->tlli;
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200191
192 /* Mark MM state as deregistered */
193 ctx->mm_state = GMM_DEREGISTERED;
194
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200195 delete_pdp_contexts(ctx, log_text);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200196
197 sgsn_mm_ctx_free(ctx);
198 ctx = NULL;
199
200 /* TLLI unassignment, must be called after sgsn_mm_ctx_free */
201 gprs_llgmm_assign(llme, tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
202}
203
Harald Weltef54e7e22010-06-09 10:51:23 +0200204/* Chapter 9.4.18 */
205static int _tx_status(struct msgb *msg, uint8_t cause,
206 struct sgsn_mm_ctx *mmctx, int sm)
207{
208 struct gsm48_hdr *gh;
209
210 /* MMCTX might be NULL! */
211
212 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100213 get_value_string(gsm48_gmm_cause_names, cause));
Harald Weltef54e7e22010-06-09 10:51:23 +0200214
215 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
216 if (sm) {
217 gh->proto_discr = GSM48_PDISC_SM_GPRS;
218 gh->msg_type = GSM48_MT_GSM_STATUS;
219 } else {
220 gh->proto_discr = GSM48_PDISC_MM_GPRS;
221 gh->msg_type = GSM48_MT_GMM_STATUS;
222 }
223 gh->data[0] = cause;
224
225 return gsm48_gmm_sendmsg(msg, 0, mmctx);
226}
Jacob Erlbeck70671422014-10-29 22:57:30 +0100227
Harald Weltef54e7e22010-06-09 10:51:23 +0200228static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
229{
230 struct msgb *msg = gsm48_msgb_alloc();
231
232 mmctx2msgid(msg, mmctx);
233 return _tx_status(msg, cause, mmctx, 0);
Harald Weltef54e7e22010-06-09 10:51:23 +0200234}
Jacob Erlbeck70671422014-10-29 22:57:30 +0100235
Harald Weltef54e7e22010-06-09 10:51:23 +0200236static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
237{
238 struct msgb *msg = gsm48_msgb_alloc();
239
240 mmctx2msgid(msg, mmctx);
241 return _tx_status(msg, cause, mmctx, 1);
Harald Weltef54e7e22010-06-09 10:51:23 +0200242}
243
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200244static int _tx_detach_req(struct msgb *msg, uint8_t detach_type, uint8_t cause,
245 struct sgsn_mm_ctx *mmctx)
246{
247 struct gsm48_hdr *gh;
248
249 /* MMCTX might be NULL! */
250
251 DEBUGP(DMM, "<- GPRS MM DETACH REQ (type: %s, cause: %s)\n",
252 get_value_string(gprs_det_t_mt_strs, detach_type),
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100253 get_value_string(gsm48_gmm_cause_names, cause));
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200254
255 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
256
257 gh->proto_discr = GSM48_PDISC_MM_GPRS;
258 gh->msg_type = GSM48_MT_GMM_DETACH_REQ;
259 gh->data[0] = detach_type & 0x07;
260
261 msgb_tv_put(msg, GSM48_IE_GMM_CAUSE, cause);
262
263 return gsm48_gmm_sendmsg(msg, 0, mmctx);
264}
265
266static int gsm48_tx_gmm_detach_req(struct sgsn_mm_ctx *mmctx,
267 uint8_t detach_type, uint8_t cause)
268{
269 struct msgb *msg = gsm48_msgb_alloc();
270
271 mmctx2msgid(msg, mmctx);
272 return _tx_detach_req(msg, detach_type, cause, mmctx);
273}
274
275static int gsm48_tx_gmm_detach_req_oldmsg(struct msgb *oldmsg,
276 uint8_t detach_type, uint8_t cause)
277{
278 struct msgb *msg = gsm48_msgb_alloc();
279
280 gmm_copy_id(msg, oldmsg);
281 return _tx_detach_req(msg, detach_type, cause, NULL);
282}
Harald Weltef54e7e22010-06-09 10:51:23 +0200283
Harald Welte86fda902010-05-03 10:54:35 +0200284static struct gsm48_qos default_qos = {
285 .delay_class = 4, /* best effort */
286 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
287 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
288 .preced_class = GSM48_QOS_PC_NORMAL,
289 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
290 .traf_class = GSM48_QOS_TC_INTERACTIVE,
291 .deliv_order = GSM48_QOS_DO_UNORDERED,
292 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
293 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
294 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
295 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
296 .resid_ber = GSM48_QOS_RBER_5e_2,
297 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
298 .handling_prio = 3,
299 .xfer_delay = 0x10, /* 200ms */
300 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
301 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
302 .sig_ind = 0, /* not optimised for signalling */
303 .max_bitrate_down_ext = 0, /* use octet 9 */
304 .guar_bitrate_down_ext = 0, /* use octet 13 */
305};
306
Harald Welte9b455bf2010-03-14 15:45:01 +0800307/* Chapter 9.4.2: Attach accept */
Harald Welte6463c072010-05-18 17:04:55 +0200308static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800309{
310 struct msgb *msg = gsm48_msgb_alloc();
311 struct gsm48_hdr *gh;
312 struct gsm48_attach_ack *aa;
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200313 uint8_t *mid;
314#if 0
315 uint8_t *ptsig;
316#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800317
Daniel Willmann46553142014-09-03 17:46:44 +0200318 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800319
Harald Welte6463c072010-05-18 17:04:55 +0200320 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800321
322 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
323 gh->proto_discr = GSM48_PDISC_MM_GPRS;
324 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
325
326 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
327 aa->force_stby = 0; /* not indicated */
328 aa->att_result = 1; /* GPRS only */
329 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
330 aa->radio_prio = 4; /* lowest */
Harald Welte6463c072010-05-18 17:04:55 +0200331 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800332
Harald Welteab1d5622010-05-18 19:58:38 +0200333#if 0
Harald Welte6463c072010-05-18 17:04:55 +0200334 /* Optional: P-TMSI signature */
335 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
336 ptsig = msgb_put(msg, 3);
337 ptsig[0] = mm->p_tmsi_sig >> 16;
338 ptsig[1] = mm->p_tmsi_sig >> 8;
339 ptsig[2] = mm->p_tmsi_sig & 0xff;
340
341 /* Optional: Negotiated Ready timer value */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200342#endif
Harald Welte6463c072010-05-18 17:04:55 +0200343
Harald Weltec2e8cc42010-05-31 20:23:38 +0200344#ifdef PTMSI_ALLOC
Harald Welte6463c072010-05-18 17:04:55 +0200345 /* Optional: Allocated P-TMSI */
Harald Welte6463c072010-05-18 17:04:55 +0200346 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
347 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
Harald Welteab1d5622010-05-18 19:58:38 +0200348 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
349#endif
Harald Weltec2e8cc42010-05-31 20:23:38 +0200350
Harald Welte6463c072010-05-18 17:04:55 +0200351 /* Optional: MS-identity (combined attach) */
352 /* Optional: GMM cause (partial attach result for combined attach) */
353
Harald Welte56a01452010-05-31 22:12:30 +0200354 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800355}
356
357/* Chapter 9.4.5: Attach reject */
Harald Welte6463c072010-05-18 17:04:55 +0200358static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800359{
Harald Welte9b455bf2010-03-14 15:45:01 +0800360 struct gsm48_hdr *gh;
361
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100362 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 +0800363
Harald Welte9b455bf2010-03-14 15:45:01 +0800364 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
365 gh->proto_discr = GSM48_PDISC_MM_GPRS;
366 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
367 gh->data[0] = gmm_cause;
368
Harald Welte8acd88f2010-05-18 10:57:45 +0200369 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800370}
Harald Welte6463c072010-05-18 17:04:55 +0200371static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
372 uint8_t gmm_cause)
373{
374 struct msgb *msg = gsm48_msgb_alloc();
375 gmm_copy_id(msg, old_msg);
376 return _tx_gmm_att_rej(msg, gmm_cause);
377}
378static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
379 uint8_t gmm_cause)
380{
381 struct msgb *msg = gsm48_msgb_alloc();
382 mmctx2msgid(msg, mm);
383 return _tx_gmm_att_rej(msg, gmm_cause);
384}
Harald Welte9b455bf2010-03-14 15:45:01 +0800385
Harald Welte2720e732010-05-17 00:44:57 +0200386/* Chapter 9.4.6.2 Detach accept */
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200387static int _tx_detach_ack(struct msgb *msg, uint8_t force_stby,
388 struct sgsn_mm_ctx *mm)
Harald Welte2720e732010-05-17 00:44:57 +0200389{
Harald Welte2720e732010-05-17 00:44:57 +0200390 struct gsm48_hdr *gh;
391
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200392 /* MMCTX might be NULL! */
Harald Welte2720e732010-05-17 00:44:57 +0200393
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200394 DEBUGP(DMM, "<- GPRS MM DETACH ACC (force-standby: %d)\n", force_stby);
Harald Welte6463c072010-05-18 17:04:55 +0200395
Harald Welte2720e732010-05-17 00:44:57 +0200396 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
397 gh->proto_discr = GSM48_PDISC_MM_GPRS;
398 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
399 gh->data[0] = force_stby;
400
Harald Welte56a01452010-05-31 22:12:30 +0200401 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte2720e732010-05-17 00:44:57 +0200402}
403
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200404static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
405{
406 struct msgb *msg = gsm48_msgb_alloc();
407
408 mmctx2msgid(msg, mm);
409 return _tx_detach_ack(msg, force_stby, mm);
410}
411
412static int gsm48_tx_gmm_det_ack_oldmsg(struct msgb *oldmsg, uint8_t force_stby)
413{
414 struct msgb *msg = gsm48_msgb_alloc();
415
416 gmm_copy_id(msg, oldmsg);
417 return _tx_detach_ack(msg, force_stby, NULL);
418}
419
Harald Welte9b455bf2010-03-14 15:45:01 +0800420/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte6463c072010-05-18 17:04:55 +0200421static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800422{
423 struct msgb *msg = gsm48_msgb_alloc();
424 struct gsm48_hdr *gh;
425
Daniel Willmann46553142014-09-03 17:46:44 +0200426 LOGMMCTXP(LOGL_DEBUG, mm, "<- GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800427
Harald Welte6463c072010-05-18 17:04:55 +0200428 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800429
430 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
431 gh->proto_discr = GSM48_PDISC_MM_GPRS;
432 gh->msg_type = GSM48_MT_GMM_ID_REQ;
433 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
434 gh->data[0] = id_type & 0xf;
435
Harald Weltedce12552010-05-31 22:31:04 +0200436 return gsm48_gmm_sendmsg(msg, 1, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800437}
438
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200439/* Section 9.4.9: Authentication and Ciphering Request */
440static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, uint8_t *rand,
441 uint8_t key_seq, uint8_t algo)
442{
443 struct msgb *msg = gsm48_msgb_alloc();
444 struct gsm48_hdr *gh;
445 struct gsm48_auth_ciph_req *acreq;
446 uint8_t *m_rand, *m_cksn;
447
Daniel Willmann46553142014-09-03 17:46:44 +0200448 LOGMMCTXP(LOGL_INFO, mm, "<- GPRS AUTH AND CIPHERING REQ (rand = %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200449 osmo_hexdump(rand, 16));
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200450
451 mmctx2msgid(msg, mm);
452
453 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
454 gh->proto_discr = GSM48_PDISC_MM_GPRS;
455 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ;
456
457 acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq));
458 acreq->ciph_alg = algo & 0xf;
459 acreq->imeisv_req = 0x1;
460 acreq->force_stby = 0x0;
461 acreq->ac_ref_nr = 0x0; /* FIXME: increment this? */
462
463 /* Only if authentication is requested we need to set RAND + CKSN */
464 if (rand) {
465 m_rand = msgb_put(msg, 16+1);
466 m_rand[0] = GSM48_IE_GMM_AUTH_RAND;
467 memcpy(m_rand+1, rand, 16);
468
Jacob Erlbeck65d82732014-12-01 10:36:17 +0100469 m_cksn = msgb_put(msg, 1);
470 m_cksn[0] = (GSM48_IE_GMM_CIPH_CKSN << 4) | (key_seq & 0x07);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200471 }
472
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200473 /* FIXME: make sure we don't send any other messages to the MS */
474
475 return gsm48_gmm_sendmsg(msg, 1, mm);
476}
477
478/* Section 9.4.11: Authentication and Ciphering Reject */
479static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm)
480{
481 struct msgb *msg = gsm48_msgb_alloc();
482 struct gsm48_hdr *gh;
483
Daniel Willmann46553142014-09-03 17:46:44 +0200484 LOGMMCTXP(LOGL_NOTICE, mm, "<- GPRS AUTH AND CIPH REJECT\n");
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200485
486 mmctx2msgid(msg, mm);
487
488 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
489 gh->proto_discr = GSM48_PDISC_MM_GPRS;
490 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ;
491
492 return gsm48_gmm_sendmsg(msg, 0, mm);
493}
494
495/* Section 9.4.10: Authentication and Ciphering Response */
496static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx,
497 struct msgb *msg)
498{
499 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
500 struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data;
501 struct tlv_parsed tp;
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100502 struct gsm_auth_tuple *at;
503 int rc;
504
505 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS AUTH AND CIPH RESPONSE\n");
506
507 if (ctx->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
508 LOGMMCTXP(LOGL_NOTICE, ctx,
509 "Unexpected Auth & Ciph Response (ignored)\n");
510 return 0;
511 }
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200512
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +0100513 /* Stop T3360 */
514 mmctx_timer_stop(ctx, 3360);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200515
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200516 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data,
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200517 (msg->data + msg->len) - acr->data, 0, 0);
518
519 /* FIXME: compare ac_ref? */
520
521 if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) ||
522 !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV)) {
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100523 /* TODO: missing mandatory IE, return STATUS or REJ? */
524 LOGMMCTXP(LOGL_ERROR, ctx, "Missing mandantory IE\n");
525 return -EINVAL;
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200526 }
527
Jacob Erlbeck8ff3fb02014-12-01 12:44:24 +0100528 /* Compare SRES with what we expected */
529 LOGMMCTXP(LOGL_DEBUG, ctx, "checking received auth info, SRES = %s\n",
530 osmo_hexdump(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES),
531 TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES)));
532
533 at = &ctx->auth_triplet;
534
535 if (TLVP_LEN(&tp, GSM48_IE_GMM_AUTH_SRES) != sizeof(at->sres) ||
536 memcmp(TLVP_VAL(&tp, GSM48_IE_GMM_AUTH_SRES), at->sres,
537 sizeof(at->sres)) != 0) {
538
539 LOGMMCTXP(LOGL_NOTICE, ctx, "Received SRES doesn't match\n");
540 rc = gsm48_tx_gmm_auth_ciph_rej(ctx);
541 mm_ctx_cleanup_free(ctx, "GPRS AUTH AND CIPH REJECT");
542 return rc;
543 }
544
545 ctx->is_authenticated = 1;
546
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200547 /* FIXME: enable LLC cipheirng */
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100548
549 /* Check if we can let the mobile station enter */
550 return gsm48_gmm_authorize(ctx);
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200551}
552
Harald Welte9b455bf2010-03-14 15:45:01 +0800553/* Check if we can already authorize a subscriber */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100554static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx)
Harald Welte9b455bf2010-03-14 15:45:01 +0800555{
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100556 /* Request IMSI and IMEI from the MS if they are unknown */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200557 if (!strlen(ctx->imei)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200558 ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
559 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200560 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200561 }
Harald Weltec2e8cc42010-05-31 20:23:38 +0200562 if (!strlen(ctx->imsi)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200563 ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
564 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200565 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200566 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800567
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100568 /* All information required for authentication is available */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100569 ctx->t3370_id_type = GSM_MI_TYPE_NONE;
570
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200571 if (ctx->auth_state == SGSN_AUTH_UNKNOWN) {
572 /* Request authorization, this leads to a call to
Jacob Erlbeckf951a012014-11-07 14:17:44 +0100573 * sgsn_auth_update which in turn calls
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200574 * gsm0408_gprs_access_granted or gsm0408_gprs_access_denied */
575
Jacob Erlbecka0b6efb2014-11-13 10:48:39 +0100576 sgsn_auth_request(ctx);
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200577 /* Note that gsm48_gmm_authorize can be called recursively via
578 * sgsn_auth_request iff ctx->auth_info changes to AUTH_ACCEPTED
579 */
580 return 0;
581 }
582
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100583 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && !ctx->is_authenticated) {
584 struct gsm_auth_tuple *at = &ctx->auth_triplet;
585
586 mmctx_timer_start(ctx, 3360, GSM0408_T3360_SECS);
587 return gsm48_tx_gmm_auth_ciph_req(ctx, at->rand, at->key_seq,
588 GPRS_ALGO_GEA0);
589 }
590
591 if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE && ctx->is_authenticated &&
592 ctx->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
593 /* Check again for authorization */
594 sgsn_auth_request(ctx);
595 return 0;
596 }
597
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200598 if (ctx->auth_state != SGSN_AUTH_ACCEPTED) {
599 LOGMMCTXP(LOGL_NOTICE, ctx,
600 "authorization is denied, aborting procedure\n");
601 return -EACCES;
Jacob Erlbeck0c06f982014-10-29 22:12:20 +0100602 }
603
604 /* The MS is authorized */
605
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100606 switch (ctx->pending_req) {
607 case 0:
608 LOGMMCTXP(LOGL_INFO, ctx,
609 "no pending request, authorization completed\n");
610 break;
611 case GSM48_MT_GMM_ATTACH_REQ:
612#ifdef PTMSI_ALLOC
613 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
614 mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
615 ctx->t3350_mode = GMM_T3350_MODE_ATT;
Jacob Erlbeck0074a772014-10-28 16:23:46 +0100616#else
617 ctx->mm_state = GMM_REGISTERED_NORMAL;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100618#endif
619
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100620 return gsm48_tx_gmm_att_ack(ctx);
621 default:
622 LOGMMCTXP(LOGL_ERROR, ctx,
623 "only Attach Request is supported yet, "
624 "got request type %u\n", ctx->pending_req);
625 break;
626 }
627
Harald Welte9b455bf2010-03-14 15:45:01 +0800628 return 0;
629}
630
Jacob Erlbeck4adb1362014-12-02 09:47:26 +0100631void gsm0408_gprs_authenticate(struct sgsn_mm_ctx *ctx)
632{
633 ctx->is_authenticated = 0;
634
635 gsm48_gmm_authorize(ctx);
636}
637
Jacob Erlbeck423f8bf2014-10-24 18:09:54 +0200638void gsm0408_gprs_access_granted(struct sgsn_mm_ctx *ctx)
639{
640 switch (ctx->mm_state) {
641 case GMM_COMMON_PROC_INIT:
642 LOGP(DMM, LOGL_NOTICE,
643 "Authorized, continuing procedure, IMSI=%s\n",
644 ctx->imsi);
645 /* Continue with the authorization */
646 gsm48_gmm_authorize(ctx);
647 break;
648 default:
649 LOGP(DMM, LOGL_INFO,
650 "Authorized, ignored, IMSI=%s\n",
651 ctx->imsi);
652 }
653}
654
655void gsm0408_gprs_access_denied(struct sgsn_mm_ctx *ctx)
656{
657 switch (ctx->mm_state) {
658 case GMM_COMMON_PROC_INIT:
659 LOGP(DMM, LOGL_NOTICE,
660 "Not authorized, rejecting ATTACH REQUEST, IMSI=%s\n",
661 ctx->imsi);
662 gsm48_tx_gmm_att_rej(ctx, GMM_CAUSE_GPRS_NOTALLOWED);
663 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJECT");
664 break;
665 case GMM_REGISTERED_NORMAL:
666 case GMM_REGISTERED_SUSPENDED:
667 LOGP(DMM, LOGL_NOTICE,
668 "Authorization lost, detaching, IMSI=%s\n",
669 ctx->imsi);
670 gsm48_tx_gmm_detach_req(
671 ctx, GPRS_DET_T_MT_REATT_NOTREQ, GMM_CAUSE_GPRS_NOTALLOWED);
672
673 mm_ctx_cleanup_free(ctx, "auth lost");
674 break;
675 default:
676 LOGP(DMM, LOGL_INFO,
677 "Authorization lost, ignored, IMSI=%s\n",
678 ctx->imsi);
679 }
680}
681
Jacob Erlbeck98647ca2014-11-11 14:47:38 +0100682void gsm0408_gprs_access_cancelled(struct sgsn_mm_ctx *ctx)
683{
684 switch (ctx->mm_state) {
685#if 0
686 case GMM_COMMON_PROC_INIT:
687 LOGP(DMM, LOGL_NOTICE,
688 "Cancelled, rejecting ATTACH REQUEST, IMSI=%s\n",
689 ctx->imsi);
690 gsm48_tx_gmm_att_rej(ctx, GMM_CAUSE_IMPL_DETACHED);
691 break;
692 case GMM_REGISTERED_NORMAL:
693 case GMM_REGISTERED_SUSPENDED:
694 LOGP(DMM, LOGL_NOTICE,
695 "Cancelled, detaching, IMSI=%s\n",
696 ctx->imsi);
697 gsm48_tx_gmm_detach_req(
698 ctx, GPRS_DET_T_MT_REATT_NOTREQ, GMM_CAUSE_IMPL_DETACHED);
699 break;
700#endif
701 default:
702 LOGP(DMM, LOGL_INFO,
703 "Cancelled, deleting context, IMSI=%s\n",
704 ctx->imsi);
705 }
706 mm_ctx_cleanup_free(ctx, "access cancelled");
707}
708
Harald Welte9b455bf2010-03-14 15:45:01 +0800709/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200710static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800711{
Harald Welte943c5bc2010-04-30 16:33:12 +0200712 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200713 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800714 char mi_string[GSM48_MI_SIZE];
Harald Welte9b455bf2010-03-14 15:45:01 +0800715
716 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte9b455bf2010-03-14 15:45:01 +0800717 if (!ctx) {
Daniel Willmann46553142014-09-03 17:46:44 +0200718 DEBUGP(DMM, "from unknown TLLI 0x%08x?!? This should not happen\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800719 return -EINVAL;
720 }
721
Daniel Willmannf8070f42014-09-23 18:48:44 +0200722 LOGMMCTXP(LOGL_DEBUG, ctx, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
723 mi_type, mi_string);
724
Jacob Erlbeckfb26c602014-10-22 12:20:26 +0200725 if (ctx->t3370_id_type == GSM_MI_TYPE_NONE) {
726 LOGMMCTXP(LOGL_NOTICE, ctx,
727 "Got unexpected IDENTITY RESPONSE: mi_type=0x%02x MI(%s), "
728 "ignoring message\n",
729 mi_type, mi_string);
730 return -EINVAL;
731 }
732
Harald Weltec2e8cc42010-05-31 20:23:38 +0200733 if (mi_type == ctx->t3370_id_type)
734 mmctx_timer_stop(ctx, 3370);
735
Harald Welte9b455bf2010-03-14 15:45:01 +0800736 switch (mi_type) {
737 case GSM_MI_TYPE_IMSI:
738 /* we already have a mm context with current TLLI, but no
739 * P-TMSI / IMSI yet. What we now need to do is to fill
740 * this initial context with data from the HLR */
Harald Weltec728eea2010-12-24 23:07:18 +0100741 if (strlen(ctx->imsi) == 0) {
742 /* Check if we already have a MM context for this IMSI */
743 struct sgsn_mm_ctx *ictx;
744 ictx = sgsn_mm_ctx_by_imsi(mi_string);
745 if (ictx) {
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200746 /* Handle it like in gsm48_rx_gmm_det_req,
747 * except that no messages are sent to the BSS */
748
Daniel Willmann46553142014-09-03 17:46:44 +0200749 LOGMMCTXP(LOGL_NOTICE, ctx, "Deleting old MM Context for same IMSI "
750 "p_tmsi_old=0x%08x\n",
751 ictx->p_tmsi);
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200752
Jacob Erlbeck99985b52014-10-13 10:32:00 +0200753 mm_ctx_cleanup_free(ictx, "GPRS IMSI re-use");
Harald Weltec728eea2010-12-24 23:07:18 +0100754 }
755 }
Jacob Erlbeckfd636ae2014-07-08 09:49:07 +0200756 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
Harald Welte9b455bf2010-03-14 15:45:01 +0800757 break;
758 case GSM_MI_TYPE_IMEI:
759 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
760 break;
761 case GSM_MI_TYPE_IMEISV:
762 break;
763 }
764
Harald Welte9b455bf2010-03-14 15:45:01 +0800765 /* Check if we can let the mobile station enter */
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100766 return gsm48_gmm_authorize(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800767}
768
769/* Section 9.4.1 Attach request */
Harald Welte807a5d82010-06-01 11:53:01 +0200770static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg,
771 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800772{
Harald Welte943c5bc2010-04-30 16:33:12 +0200773 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200774 uint8_t *cur = gh->data, *msnc, *mi, *ms_ra_acc_cap;
Harald Welte56a01452010-05-31 22:12:30 +0200775 uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len;
Harald Welteeaa614c2010-05-02 11:26:34 +0200776 uint16_t drx_par;
777 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800778 char mi_string[GSM48_MI_SIZE];
779 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200780 uint16_t cid;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100781 enum gsm48_gmm_cause reject_cause;
782 int rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800783
Daniel Willmann46553142014-09-03 17:46:44 +0200784 LOGP(DMM, LOGL_INFO, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800785
786 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
787 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
788 * or with random TLLI. */
789
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200790 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800791
792 /* MS network capability 10.5.5.12 */
793 msnc_len = *cur++;
794 msnc = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400795 if (msnc_len > sizeof(ctx->ms_network_capa.buf))
Harald Welte9b455bf2010-03-14 15:45:01 +0800796 goto err_inval;
797 cur += msnc_len;
798
799 /* aTTACH Type 10.5.5.2 */
800 att_type = *cur++ & 0x0f;
801
802 /* DRX parameter 10.5.5.6 */
Harald Welte56a01452010-05-31 22:12:30 +0200803 drx_par = *cur++ << 8;
804 drx_par |= *cur++;
Harald Welte9b455bf2010-03-14 15:45:01 +0800805
806 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
807 mi_len = *cur++;
808 mi = cur;
809 if (mi_len > 8)
810 goto err_inval;
811 mi_type = *mi & GSM_MI_TYPE_MASK;
812 cur += mi_len;
813
814 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
815
Harald Welte2720e732010-05-17 00:44:57 +0200816 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
817 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800818
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +0200819 /* Old routing area identification 10.5.5.15. Skip it */
Harald Welte9b455bf2010-03-14 15:45:01 +0800820 cur += 6;
821
822 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte56a01452010-05-31 22:12:30 +0200823 ms_ra_acc_cap_len = *cur++;
824 ms_ra_acc_cap = cur;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400825 if (ms_ra_acc_cap_len > sizeof(ctx->ms_radio_access_capa.buf))
Harald Weltec48ac472010-07-03 21:20:06 +0200826 goto err_inval;
Harald Welte4b2ed352011-07-27 23:35:38 +0200827 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +0800828
Daniel Willmann46553142014-09-03 17:46:44 +0200829 LOGPC(DMM, LOGL_INFO, "\n");
830
Harald Welte9b455bf2010-03-14 15:45:01 +0800831 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
832
833 switch (mi_type) {
834 case GSM_MI_TYPE_IMSI:
835 /* Try to find MM context based on IMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200836 if (!ctx)
837 ctx = sgsn_mm_ctx_by_imsi(mi_string);
Harald Welte9b455bf2010-03-14 15:45:01 +0800838 if (!ctx) {
839#if 0
840 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
841#else
Harald Welte9b455bf2010-03-14 15:45:01 +0800842 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100843 if (!ctx) {
844 reject_cause = GMM_CAUSE_NET_FAIL;
845 goto rejected;
846 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800847 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
848#endif
849 }
Harald Welte943c5bc2010-04-30 16:33:12 +0200850 ctx->tlli = msgb_tlli(msg);
Harald Welte807a5d82010-06-01 11:53:01 +0200851 ctx->llme = llme;
Harald Welte68b99a42010-05-18 12:05:42 +0200852 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800853 break;
854 case GSM_MI_TYPE_TMSI:
Harald Welteab1d5622010-05-18 19:58:38 +0200855 memcpy(&tmsi, mi+1, 4);
856 tmsi = ntohl(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800857 /* Try to find MM context based on P-TMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200858 if (!ctx)
859 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800860 if (!ctx) {
Harald Weltec728eea2010-12-24 23:07:18 +0100861 /* Allocate a context as most of our code expects one.
862 * Context will not have an IMSI ultil ID RESP is received */
Harald Welte943c5bc2010-04-30 16:33:12 +0200863 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte807a5d82010-06-01 11:53:01 +0200864 ctx->p_tmsi = tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800865 }
Harald Welte807a5d82010-06-01 11:53:01 +0200866 ctx->tlli = msgb_tlli(msg);
867 ctx->llme = llme;
868 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800869 break;
870 default:
Harald Weltec2e8cc42010-05-31 20:23:38 +0200871 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST with "
872 "MI type %u\n", mi_type);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100873 reject_cause = GMM_CAUSE_MS_ID_NOT_DERIVED;
874 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +0800875 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200876 /* Update MM Context with currient RA and Cell ID */
877 ctx->ra = ra_id;
878 ctx->cell_id = cid;
Harald Welte56a01452010-05-31 22:12:30 +0200879 /* Update MM Context with other data */
880 ctx->drx_parms = drx_par;
Alexander Chemeris84402c02013-07-03 10:12:23 +0400881 ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len;
Harald Welte4b2ed352011-07-27 23:35:38 +0200882 memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap,
883 ctx->ms_radio_access_capa.len);
Harald Welte56a01452010-05-31 22:12:30 +0200884 ctx->ms_network_capa.len = msnc_len;
885 memcpy(ctx->ms_network_capa.buf, msnc, msnc_len);
886
Harald Weltec2e8cc42010-05-31 20:23:38 +0200887#ifdef PTMSI_ALLOC
Harald Welte807a5d82010-06-01 11:53:01 +0200888 /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
Jacob Erlbeckf6e7d992014-11-06 15:43:10 +0100889 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
890 if (ctx->mm_state != GMM_COMMON_PROC_INIT) {
891 ctx->p_tmsi_old = ctx->p_tmsi;
892 ctx->p_tmsi = sgsn_alloc_ptmsi();
893 }
Jacob Erlbeck0074a772014-10-28 16:23:46 +0100894 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welteab1d5622010-05-18 19:58:38 +0200895#endif
Harald Welte807a5d82010-06-01 11:53:01 +0200896 /* Even if there is no P-TMSI allocated, the MS will switch from
897 * foreign TLLI to local TLLI */
898 ctx->tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL);
899
900 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +0200901 gprs_llgmm_assign(ctx->llme, ctx->tlli, ctx->tlli_new,
902 GPRS_ALGO_GEA0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800903
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +0100904 ctx->pending_req = GSM48_MT_GMM_ATTACH_REQ;
905 return gsm48_gmm_authorize(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800906
907err_inval:
Daniel Willmann46553142014-09-03 17:46:44 +0200908 LOGPC(DMM, LOGL_INFO, "\n");
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100909 reject_cause = GMM_CAUSE_SEM_INCORR_MSG;
910
911rejected:
912 /* Send ATTACH REJECT */
913 LOGMMCTXP(LOGL_NOTICE, ctx,
914 "Rejecting Attach Request with cause '%s' (%d)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +0100915 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +0100916 rc = gsm48_tx_gmm_att_rej_oldmsg(msg, reject_cause);
917 if (ctx)
918 mm_ctx_cleanup_free(ctx, "GPRS ATTACH REJ");
919 else
920 /* TLLI unassignment */
921 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL);
922
923 return rc;
924
Harald Welte9b455bf2010-03-14 15:45:01 +0800925}
926
Harald Welte2720e732010-05-17 00:44:57 +0200927/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +0200928static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +0200929{
930 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte2720e732010-05-17 00:44:57 +0200931 uint8_t detach_type, power_off;
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200932 int rc = 0;
Harald Welte2720e732010-05-17 00:44:57 +0200933
934 detach_type = gh->data[0] & 0x7;
935 power_off = gh->data[0] & 0x8;
936
937 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
938
Daniel Willmann46553142014-09-03 17:46:44 +0200939 LOGMMCTXP(LOGL_INFO, ctx, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
Harald Welte2720e732010-05-17 00:44:57 +0200940 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
941 power_off ? "Power-off" : "");
942
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200943 /* Only send the Detach Accept (MO) if power off isn't indicated,
Jacob Erlbeckb9ab0d42014-10-21 09:52:05 +0200944 * see 04.08, 4.7.4.1.2/3 for details */
945 if (!power_off) {
946 /* force_stby = 0 */
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200947 if (ctx)
948 rc = gsm48_tx_gmm_det_ack(ctx, 0);
949 else
950 rc = gsm48_tx_gmm_det_ack_oldmsg(msg, 0);
Jacob Erlbeckb9ab0d42014-10-21 09:52:05 +0200951 }
Harald Weltebd5c9122010-06-28 22:18:53 +0200952
Jacob Erlbeck5a38f642014-10-21 13:09:55 +0200953 if (ctx)
954 mm_ctx_cleanup_free(ctx, "GPRS DETACH REQUEST");
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +0200955
Harald Weltebd5c9122010-06-28 22:18:53 +0200956 return rc;
Harald Welte2720e732010-05-17 00:44:57 +0200957}
958
Harald Welte9b455bf2010-03-14 15:45:01 +0800959/* Chapter 9.4.15: Routing area update accept */
Harald Welte6463c072010-05-18 17:04:55 +0200960static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800961{
962 struct msgb *msg = gsm48_msgb_alloc();
963 struct gsm48_hdr *gh;
964 struct gsm48_ra_upd_ack *rua;
Harald Weltec2e8cc42010-05-31 20:23:38 +0200965 uint8_t *mid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800966
Daniel Willmann46553142014-09-03 17:46:44 +0200967 LOGMMCTXP(LOGL_INFO, mm, "<- ROUTING AREA UPDATE ACCEPT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +0800968
Harald Welte6463c072010-05-18 17:04:55 +0200969 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800970
971 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
972 gh->proto_discr = GSM48_PDISC_MM_GPRS;
973 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
974
975 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
976 rua->force_stby = 0; /* not indicated */
977 rua->upd_result = 0; /* RA updated */
978 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200979
Harald Welte6463c072010-05-18 17:04:55 +0200980 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800981
Harald Weltec2e8cc42010-05-31 20:23:38 +0200982#if 0
983 /* Optional: P-TMSI signature */
984 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
985 ptsig = msgb_put(msg, 3);
986 ptsig[0] = mm->p_tmsi_sig >> 16;
987 ptsig[1] = mm->p_tmsi_sig >> 8;
988 ptsig[2] = mm->p_tmsi_sig & 0xff;
989#endif
990
991#ifdef PTMSI_ALLOC
992 /* Optional: Allocated P-TMSI */
993 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
994 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
995 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
996#endif
997
998 /* Option: MS ID, ... */
Harald Welte56a01452010-05-31 22:12:30 +0200999 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001000}
1001
1002/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +02001003static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +08001004{
1005 struct msgb *msg = gsm48_msgb_alloc();
1006 struct gsm48_hdr *gh;
1007
Daniel Willmann46553142014-09-03 17:46:44 +02001008 LOGP(DMM, LOGL_NOTICE, "<- ROUTING AREA UPDATE REJECT\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001009
Harald Welte11d7c102010-05-02 11:54:55 +02001010 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001011
1012 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
1013 gh->proto_discr = GSM48_PDISC_MM_GPRS;
1014 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
1015 gh->data[0] = cause;
1016 gh->data[1] = 0; /* ? */
1017
1018 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +02001019 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001020}
1021
Harald Welte588d44e2010-06-09 10:28:29 +02001022static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx,
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001023 const uint8_t *pdp_status)
Harald Welte588d44e2010-06-09 10:28:29 +02001024{
1025 struct sgsn_pdp_ctx *pdp, *pdp2;
1026 /* 24.008 4.7.5.1.3: If the PDP context status information element is
1027 * included in ROUTING AREA UPDATE REQUEST message, then the network
1028 * shall deactivate all those PDP contexts locally (without peer to
1029 * peer signalling between the MS and the network), which are not in SM
1030 * state PDP-INACTIVE on network side but are indicated by the MS as
1031 * being in state PDP-INACTIVE. */
1032
1033 llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) {
Harald Weltecdf76cf2011-08-05 21:23:46 +02001034 if (pdp->nsapi < 8) {
1035 if (!(pdp_status[0] & (1 << pdp->nsapi))) {
Daniel Willmann46553142014-09-03 17:46:44 +02001036 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +02001037 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1038 pdp->nsapi, pdp_status[1], pdp_status[0]);
1039 sgsn_delete_pdp_ctx(pdp);
1040 }
1041 } else {
1042 if (!(pdp_status[1] & (1 << (pdp->nsapi - 8)))) {
Daniel Willmann46553142014-09-03 17:46:44 +02001043 LOGMMCTXP(LOGL_NOTICE, mmctx, "Dropping PDP context for NSAPI=%u "
Harald Weltecdf76cf2011-08-05 21:23:46 +02001044 "due to PDP CTX STATUS IE= 0x%02x%02x\n",
1045 pdp->nsapi, pdp_status[1], pdp_status[0]);
1046 sgsn_delete_pdp_ctx(pdp);
1047 }
Harald Welte588d44e2010-06-09 10:28:29 +02001048 }
1049 }
1050}
1051
Harald Welte9b455bf2010-03-14 15:45:01 +08001052/* Chapter 9.4.14: Routing area update request */
Harald Welte807a5d82010-06-01 11:53:01 +02001053static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1054 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001055{
Harald Welte943c5bc2010-04-30 16:33:12 +02001056 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001057 uint8_t *cur = gh->data;
Harald Welte588d44e2010-06-09 10:28:29 +02001058 uint8_t ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +08001059 struct gprs_ra_id old_ra_id;
Harald Welte588d44e2010-06-09 10:28:29 +02001060 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +02001061 uint8_t upd_type;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001062 enum gsm48_gmm_cause reject_cause;
1063 int rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001064
1065 /* Update Type 10.5.5.18 */
1066 upd_type = *cur++ & 0x0f;
1067
Daniel Willmann46553142014-09-03 17:46:44 +02001068 LOGP(DMM, LOGL_INFO, "-> GMM RA UPDATE REQUEST type=\"%s\"\n",
Harald Welte2720e732010-05-17 00:44:57 +02001069 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +08001070
1071 /* Old routing area identification 10.5.5.15 */
1072 gsm48_parse_ra(&old_ra_id, cur);
1073 cur += 6;
1074
1075 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte588d44e2010-06-09 10:28:29 +02001076 ms_ra_acc_cap_len = *cur++;
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001077 if (ms_ra_acc_cap_len > 52) {
1078 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1079 goto rejected;
1080 }
Harald Welte4b2ed352011-07-27 23:35:38 +02001081 cur += ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +08001082
1083 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
1084 * DRX parameter, MS network capability */
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001085 tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur,
Harald Welte588d44e2010-06-09 10:28:29 +02001086 (msg->data + msg->len) - cur, 0, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +08001087
1088 switch (upd_type) {
1089 case GPRS_UPD_T_RA_LA:
1090 case GPRS_UPD_T_RA_LA_IMSI_ATT:
Daniel Willmann46553142014-09-03 17:46:44 +02001091 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 +01001092 reject_cause = GMM_CAUSE_PROTO_ERR_UNSPEC;
1093 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +08001094 case GPRS_UPD_T_RA:
1095 case GPRS_UPD_T_PERIODIC:
1096 break;
1097 }
1098
1099 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Weltef6bd3402010-12-23 23:34:43 +01001100 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +08001101 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
Harald Welte2b697a62011-10-16 18:50:56 +02001102 /* send a XID reset to re-set all LLC sequence numbers
1103 * in the MS */
Daniel Willmann46553142014-09-03 17:46:44 +02001104 LOGP(DMM, LOGL_NOTICE, "LLC XID RESET\n");
Harald Welte2b697a62011-10-16 18:50:56 +02001105 gprs_llgmm_reset(llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001106 /* The MS has to perform GPRS attach */
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001107 /* Device is still IMSI attached for CS but initiate GPRS ATTACH,
1108 * see GSM 04.08, 4.7.5.1.4 and G.6 */
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001109 reject_cause = GMM_CAUSE_IMPL_DETACHED;
1110 goto rejected;
Harald Welte9b455bf2010-03-14 15:45:01 +08001111 }
1112
Harald Weltef0901f02010-12-26 10:39:26 +01001113 /* Store new BVCI/NSEI in MM context (FIXME: delay until we ack?) */
1114 msgid2mmctx(mmctx, msg);
1115 /* Bump the statistics of received signalling msgs for this MM context */
1116 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
1117
Harald Welte9f1f3ad2010-05-02 12:56:57 +02001118 /* Update the MM context with the new RA-ID */
1119 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
Harald Weltef0901f02010-12-26 10:39:26 +01001120 /* Update the MM context with the new (i.e. foreign) TLLI */
Harald Welte9f1f3ad2010-05-02 12:56:57 +02001121 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001122 /* FIXME: Update the MM context with the MS radio acc capabilities */
1123 /* FIXME: Update the MM context with the MS network capabilities */
1124
Harald Welte77289c22010-05-18 14:32:29 +02001125 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
1126
Harald Weltec2e8cc42010-05-31 20:23:38 +02001127#ifdef PTMSI_ALLOC
Jacob Erlbeckf6e7d992014-11-06 15:43:10 +01001128 /* Don't change the P-TMSI if a P-TMSI re-assignment is under way */
1129 if (mmctx->mm_state != GMM_COMMON_PROC_INIT) {
1130 mmctx->p_tmsi_old = mmctx->p_tmsi;
1131 mmctx->p_tmsi = sgsn_alloc_ptmsi();
1132 }
Harald Weltec2e8cc42010-05-31 20:23:38 +02001133 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
1134 mmctx->t3350_mode = GMM_T3350_MODE_RAU;
1135 mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001136
1137 mmctx->mm_state = GMM_COMMON_PROC_INIT;
1138#else
1139 /* Make sure we are NORMAL (i.e. not SUSPENDED anymore) */
1140 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001141#endif
Harald Welte807a5d82010-06-01 11:53:01 +02001142 /* Even if there is no P-TMSI allocated, the MS will switch from
1143 * foreign TLLI to local TLLI */
1144 mmctx->tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001145
Harald Welte807a5d82010-06-01 11:53:01 +02001146 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +02001147 gprs_llgmm_assign(mmctx->llme, mmctx->tlli, mmctx->tlli_new,
1148 GPRS_ALGO_GEA0, NULL);
Harald Welte807a5d82010-06-01 11:53:01 +02001149
Harald Welte588d44e2010-06-09 10:28:29 +02001150 /* Look at PDP Context Status IE and see if MS's view of
1151 * activated/deactivated NSAPIs agrees with our view */
1152 if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) {
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001153 const uint8_t *pdp_status = TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS);
Harald Welte588d44e2010-06-09 10:28:29 +02001154 process_ms_ctx_status(mmctx, pdp_status);
1155 }
1156
Harald Welte807a5d82010-06-01 11:53:01 +02001157 /* Send RA UPDATE ACCEPT */
Harald Welte6463c072010-05-18 17:04:55 +02001158 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001159
1160rejected:
1161 /* Send RA UPDATE REJECT */
1162 LOGMMCTXP(LOGL_NOTICE, mmctx,
1163 "Rejecting RA Update Request with cause '%s' (%d)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001164 get_value_string(gsm48_gmm_cause_names, reject_cause), reject_cause);
Jacob Erlbeck80d07e32014-11-06 13:43:41 +01001165 rc = gsm48_tx_gmm_ra_upd_rej(msg, reject_cause);
1166 if (mmctx)
1167 mm_ctx_cleanup_free(mmctx, "GPRS RA UPDATE REJ");
1168 else
1169 /* TLLI unassignment */
1170 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff, GPRS_ALGO_GEA0,
1171 NULL);
1172
1173 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001174}
1175
Harald Welted193cb32010-05-17 22:58:03 +02001176static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001177{
1178 struct gsm48_hdr *gh = msgb_l3(msg);
1179
Daniel Willmann46553142014-09-03 17:46:44 +02001180 LOGMMCTXP(LOGL_INFO, mmctx, "-> GPRS MM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001181 get_value_string(gsm48_gmm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001182
1183 return 0;
1184}
1185
1186/* GPRS Mobility Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001187static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1188 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001189{
Harald Welte943c5bc2010-04-30 16:33:12 +02001190 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001191 int rc;
1192
Harald Welte807a5d82010-06-01 11:53:01 +02001193 /* MMCTX can be NULL when called */
1194
Harald Welted193cb32010-05-17 22:58:03 +02001195 if (!mmctx &&
1196 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
Harald Weltec2e8cc42010-05-31 20:23:38 +02001197 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
Harald Welted193cb32010-05-17 22:58:03 +02001198 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001199 /* 4.7.10 */
Jacob Erlbeck14ae5822014-10-28 09:47:03 +01001200 if (gh->msg_type == GSM48_MT_GMM_STATUS) {
1201 /* TLLI unassignment */
1202 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1203 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001204 return 0;
Jacob Erlbeck14ae5822014-10-28 09:47:03 +01001205 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001206
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001207 /* Don't reply or establish a LLME on DETACH_ACK */
1208 if (gh->msg_type == GSM48_MT_GMM_DETACH_ACK) {
1209 /* TLLI unassignment */
1210 return gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1211 GPRS_ALGO_GEA0, NULL);
1212 }
1213
Jacob Erlbeckc4f9bf32014-11-11 08:55:13 +01001214 gprs_llgmm_reset(llme);
1215
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001216 /* Don't force it into re-attachment */
1217 if (gh->msg_type == GSM48_MT_GMM_DETACH_REQ) {
Jacob Erlbeck5a38f642014-10-21 13:09:55 +02001218 /* Handle Detach Request */
1219 rc = gsm48_rx_gmm_det_req(NULL, msg);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001220
1221 /* TLLI unassignment */
1222 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1223 GPRS_ALGO_GEA0, NULL);
1224 return rc;
1225 }
1226
1227 /* Force the MS to re-attach */
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001228 rc = sgsn_force_reattach_oldmsg(msg);
1229
1230 /* TLLI unassignment */
1231 gprs_llgmm_assign(llme, llme->tlli, 0xffffffff,
1232 GPRS_ALGO_GEA0, NULL);
1233 return rc;
Harald Welted193cb32010-05-17 22:58:03 +02001234 }
1235
Harald Welte9b455bf2010-03-14 15:45:01 +08001236 switch (gh->msg_type) {
1237 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001238 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001239 break;
1240 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001241 rc = gsm48_rx_gmm_att_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001242 break;
Daniel Willmann46553142014-09-03 17:46:44 +02001243 /* For all the following types mmctx can not be NULL */
Harald Welte9b455bf2010-03-14 15:45:01 +08001244 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +02001245 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001246 break;
1247 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001248 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001249 break;
Harald Welte2720e732010-05-17 00:44:57 +02001250 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001251 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +02001252 break;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001253 case GSM48_MT_GMM_DETACH_ACK:
1254 LOGMMCTXP(LOGL_INFO, mmctx, "-> DETACH ACK\n");
1255 mm_ctx_cleanup_free(mmctx, "GPRS DETACH ACK");
1256 rc = 0;
1257 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001258 case GSM48_MT_GMM_ATTACH_COMPL:
1259 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001260 LOGMMCTXP(LOGL_INFO, mmctx, "-> ATTACH COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001261 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001262 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001263 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001264 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001265 /* Unassign the old TLLI */
1266 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001267 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1268 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001269 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001270 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001271 break;
Harald Welteab1d5622010-05-18 19:58:38 +02001272 case GSM48_MT_GMM_RA_UPD_COMPL:
1273 /* only in case SGSN offered new P-TMSI */
Daniel Willmann46553142014-09-03 17:46:44 +02001274 LOGMMCTXP(LOGL_INFO, mmctx, "-> ROUTEING AREA UPDATE COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001275 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001276 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001277 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001278 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001279 /* Unassign the old TLLI */
1280 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001281 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1282 GPRS_ALGO_GEA0, NULL);
Jacob Erlbeck0074a772014-10-28 16:23:46 +01001283 mmctx->mm_state = GMM_REGISTERED_NORMAL;
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001284 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001285 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001286 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
Daniel Willmann46553142014-09-03 17:46:44 +02001287 LOGMMCTXP(LOGL_INFO, mmctx, "-> PTMSI REALLLICATION COMPLETE\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001288 mmctx_timer_stop(mmctx, 3350);
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001289 mmctx->t3350_mode = GMM_T3350_MODE_NONE;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001290 mmctx->p_tmsi_old = 0;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001291 mmctx->pending_req = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001292 /* Unassign the old TLLI */
1293 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001294 //gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new, GPRS_ALGO_GEA0, NULL);
Holger Hans Peter Freyther2b174542011-10-14 23:31:34 +02001295 rc = 0;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001296 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001297 case GSM48_MT_GMM_AUTH_CIPH_RESP:
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001298 rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001299 break;
1300 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001301 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001302 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001303 rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001304 break;
1305 }
1306
1307 return rc;
1308}
1309
Harald Weltec2e8cc42010-05-31 20:23:38 +02001310static void mmctx_timer_cb(void *_mm)
1311{
1312 struct sgsn_mm_ctx *mm = _mm;
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +01001313 struct gsm_auth_tuple *at;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001314
1315 mm->num_T_exp++;
1316
1317 switch (mm->T) {
1318 case 3350: /* waiting for ATTACH COMPLETE */
1319 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001320 LOGMMCTXP(LOGL_NOTICE, mm, "T3350 expired >= 5 times\n");
Jacob Erlbecka7904562014-11-03 10:12:52 +01001321 mm_ctx_cleanup_free(mm, "T3350");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001322 /* FIXME: should we return some error? */
1323 break;
1324 }
1325 /* re-transmit the respective msg and re-start timer */
1326 switch (mm->t3350_mode) {
1327 case GMM_T3350_MODE_ATT:
1328 gsm48_tx_gmm_att_ack(mm);
1329 break;
1330 case GMM_T3350_MODE_RAU:
1331 gsm48_tx_gmm_ra_upd_ack(mm);
1332 break;
1333 case GMM_T3350_MODE_PTMSI_REALL:
1334 /* FIXME */
1335 break;
Jacob Erlbeck93eae8e2014-10-28 12:23:29 +01001336 case GMM_T3350_MODE_NONE:
1337 LOGMMCTXP(LOGL_NOTICE, mm,
1338 "T3350 mode wasn't set, ignoring timeout\n");
1339 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001340 }
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001341 osmo_timer_schedule(&mm->timer, GSM0408_T3350_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001342 break;
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001343 case 3360: /* waiting for AUTH AND CIPH RESP */
1344 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001345 LOGMMCTXP(LOGL_NOTICE, mm, "T3360 expired >= 5 times\n");
Jacob Erlbecka7904562014-11-03 10:12:52 +01001346 mm_ctx_cleanup_free(mm, "T3360");
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001347 break;
1348 }
Jacob Erlbeckbd0cf112014-12-01 12:33:33 +01001349 /* Re-transmit the respective msg and re-start timer */
1350 if (mm->auth_triplet.key_seq == GSM_KEY_SEQ_INVAL) {
1351 LOGMMCTXP(LOGL_ERROR, mm,
1352 "timeout: invalid auth triplet reference\n");
1353 mm_ctx_cleanup_free(mm, "T3360");
1354 break;
1355 }
1356 at = &mm->auth_triplet;
1357
1358 gsm48_tx_gmm_auth_ciph_req(mm, at->rand, at->key_seq, GPRS_ALGO_GEA0);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001359 osmo_timer_schedule(&mm->timer, GSM0408_T3360_SECS, 0);
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001360 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001361 case 3370: /* waiting for IDENTITY RESPONSE */
1362 if (mm->num_T_exp >= 5) {
Daniel Willmann46553142014-09-03 17:46:44 +02001363 LOGMMCTXP(LOGL_NOTICE, mm, "T3370 expired >= 5 times\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001364 gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED);
Jacob Erlbecka7904562014-11-03 10:12:52 +01001365 mm_ctx_cleanup_free(mm, "GPRS ATTACH REJECT (T3370)");
Harald Weltec2e8cc42010-05-31 20:23:38 +02001366 break;
1367 }
1368 /* re-tranmit IDENTITY REQUEST and re-start timer */
1369 gsm48_tx_gmm_id_req(mm, mm->t3370_id_type);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001370 osmo_timer_schedule(&mm->timer, GSM0408_T3370_SECS, 0);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001371 break;
1372 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001373 LOGMMCTXP(LOGL_ERROR, mm, "timer expired in unknown mode %u\n",
Harald Weltec2e8cc42010-05-31 20:23:38 +02001374 mm->T);
1375 }
1376}
1377
1378/* GPRS SESSION MANAGEMENT */
1379
Harald Weltea9b473a2010-12-24 21:13:26 +01001380static void pdpctx_timer_cb(void *_mm);
1381
1382static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T,
1383 unsigned int seconds)
1384{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001385 if (osmo_timer_pending(&pdp->timer))
Harald Weltea9b473a2010-12-24 21:13:26 +01001386 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
1387 "timer %u pending\n", T, pdp->T);
1388 pdp->T = T;
1389 pdp->num_T_exp = 0;
1390
1391 /* FIXME: we should do this only once ? */
1392 pdp->timer.data = pdp;
1393 pdp->timer.cb = &pdpctx_timer_cb;
1394
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001395 osmo_timer_schedule(&pdp->timer, seconds, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001396}
1397
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001398#if 0
Harald Welte421cba42010-05-02 23:11:50 +02001399static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
1400{
1401 uint8_t v[6];
1402
1403 v[0] = PDP_TYPE_ORG_IETF;
1404 v[1] = PDP_TYPE_N_IETF_IPv4;
1405 *(uint32_t *)(v+2) = htonl(ipaddr);
1406
1407 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1408}
1409
1410static void msgb_put_pdp_addr_ppp(struct msgb *msg)
1411{
1412 uint8_t v[2];
1413
1414 v[0] = PDP_TYPE_ORG_ETSI;
1415 v[1] = PDP_TYPE_N_ETSI_PPP;
1416
1417 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1418}
Holger Hans Peter Freyther1768a572014-04-04 12:40:34 +02001419#endif
Harald Welte421cba42010-05-02 23:11:50 +02001420
Harald Welte9b455bf2010-03-14 15:45:01 +08001421/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +02001422int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +08001423{
Harald Welte9b455bf2010-03-14 15:45:01 +08001424 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +08001425 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +02001426 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001427
Daniel Willmann46553142014-09-03 17:46:44 +02001428 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001429
Harald Welte6abf94e2010-05-18 10:35:06 +02001430 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001431
1432 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1433 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1434 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +02001435
1436 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +02001437 msgb_v_put(msg, pdp->sapi);
Harald Welte4e38ac72010-05-18 14:51:18 +02001438
Harald Welte55e0df72010-05-18 13:20:08 +02001439 /* FIXME: copy QoS parameters from original request */
1440 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
1441 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte4e38ac72010-05-18 14:51:18 +02001442
Harald Welte421cba42010-05-02 23:11:50 +02001443 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +02001444 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte4e38ac72010-05-18 14:51:18 +02001445
Harald Welte421cba42010-05-02 23:11:50 +02001446 /* PDP address */
Harald Welte4e38ac72010-05-18 14:51:18 +02001447 /* Highest 4 bits of first byte need to be set to 1, otherwise
1448 * the IE is identical with the 04.08 PDP Address IE */
1449 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte6abf94e2010-05-18 10:35:06 +02001450 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
1451 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001452 pdp->lib->eua.v[0] |= 0xf0;
1453
Harald Welte77289c22010-05-18 14:32:29 +02001454 /* Optional: Protocol configuration options (FIXME: why 'req') */
Holger Hans Peter Freytherd2195072014-04-04 11:55:21 +02001455 if (pdp->lib->pco_req.l)
Harald Welte6abf94e2010-05-18 10:35:06 +02001456 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Welte3c0b9b92010-05-18 14:36:11 +02001457 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001458
Harald Welte421cba42010-05-02 23:11:50 +02001459 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +08001460
Harald Welte8acd88f2010-05-18 10:57:45 +02001461 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001462}
1463
Harald Welte6abf94e2010-05-18 10:35:06 +02001464/* Section 9.5.3: Activate PDP Context reject */
1465int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
1466 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
1467{
1468 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte6abf94e2010-05-18 10:35:06 +02001469 struct gsm48_hdr *gh;
1470 uint8_t transaction_id = tid ^ 0x8; /* flip */
1471
Daniel Willmann46553142014-09-03 17:46:44 +02001472 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte6abf94e2010-05-18 10:35:06 +02001473
1474 mmctx2msgid(msg, mm);
1475
1476 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1477 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1478 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
1479
1480 msgb_v_put(msg, cause);
1481 if (pco_len && pco_v)
1482 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
1483
Harald Welte8acd88f2010-05-18 10:57:45 +02001484 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte6abf94e2010-05-18 10:35:06 +02001485}
1486
Harald Weltea9b473a2010-12-24 21:13:26 +01001487/* Section 9.5.8: Deactivate PDP Context Request */
1488static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
1489 uint8_t sm_cause)
1490{
1491 struct msgb *msg = gsm48_msgb_alloc();
1492 struct gsm48_hdr *gh;
1493 uint8_t transaction_id = tid ^ 0x8; /* flip */
1494
Daniel Willmann46553142014-09-03 17:46:44 +02001495 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001496
1497 mmctx2msgid(msg, mm);
1498
1499 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1500 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1501 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
1502
1503 msgb_v_put(msg, sm_cause);
1504
1505 return gsm48_gmm_sendmsg(msg, 0, mm);
1506}
1507int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause)
1508{
1509 pdpctx_timer_start(pdp, 3395, GSM0408_T3395_SECS);
1510
1511 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause);
1512}
1513
Harald Welte9b455bf2010-03-14 15:45:01 +08001514/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte60ebc022010-06-08 18:08:59 +02001515static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
Harald Welte9b455bf2010-03-14 15:45:01 +08001516{
Harald Welte9b455bf2010-03-14 15:45:01 +08001517 struct msgb *msg = gsm48_msgb_alloc();
1518 struct gsm48_hdr *gh;
Harald Welte60ebc022010-06-08 18:08:59 +02001519 uint8_t transaction_id = tid ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001520
Daniel Willmann46553142014-09-03 17:46:44 +02001521 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
Harald Welte9b455bf2010-03-14 15:45:01 +08001522
Harald Welte60ebc022010-06-08 18:08:59 +02001523 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001524
1525 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1526 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1527 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
1528
Harald Welte60ebc022010-06-08 18:08:59 +02001529 return gsm48_gmm_sendmsg(msg, 0, mm);
1530}
1531int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
1532{
1533 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
Harald Welte9b455bf2010-03-14 15:45:01 +08001534}
1535
1536/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +02001537static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
1538 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001539{
Harald Welte943c5bc2010-04-30 16:33:12 +02001540 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001541 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welte0c3eae02010-05-02 21:07:14 +02001542 uint8_t req_qos_len, req_pdpa_len;
1543 uint8_t *req_qos, *req_pdpa;
1544 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +02001545 uint8_t transaction_id = (gh->proto_discr >> 4);
Harald Welte11ff3e82010-07-02 10:17:24 +02001546 struct sgsn_ggsn_ctx *ggsn;
Harald Welte6abf94e2010-05-18 10:35:06 +02001547 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001548
Daniel Willmann46553142014-09-03 17:46:44 +02001549 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
Harald Welte6abf94e2010-05-18 10:35:06 +02001550 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8acd88f2010-05-18 10:57:45 +02001551
Harald Welte23c13762010-07-02 22:52:04 +02001552 /* FIXME: length checks! */
Harald Welte0c3eae02010-05-02 21:07:14 +02001553 req_qos_len = act_req->data[0];
1554 req_qos = act_req->data + 1; /* 10.5.6.5 */
1555 req_pdpa_len = act_req->data[1 + req_qos_len];
1556 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +08001557
Harald Welte23c13762010-07-02 22:52:04 +02001558 /* Optional: Access Point Name, Protocol Config Options */
1559 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
1560 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
1561 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
1562 else
1563 memset(&tp, 0, sizeof(tp));
1564
Harald Welte0c3eae02010-05-02 21:07:14 +02001565 switch (req_pdpa[0] & 0xf) {
1566 case 0x0:
1567 DEBUGPC(DMM, "ETSI ");
1568 break;
1569 case 0x1:
1570 DEBUGPC(DMM, "IETF ");
1571 break;
1572 case 0xf:
1573 DEBUGPC(DMM, "Empty ");
1574 break;
1575 }
1576
1577 switch (req_pdpa[1]) {
1578 case 0x21:
1579 DEBUGPC(DMM, "IPv4 ");
1580 if (req_pdpa_len >= 6) {
1581 struct in_addr ia;
1582 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
1583 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
1584 }
1585 break;
1586 case 0x57:
1587 DEBUGPC(DMM, "IPv6 ");
1588 if (req_pdpa_len >= 18) {
1589 /* FIXME: print IPv6 address */
1590 }
1591 break;
1592 default:
1593 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
1594 break;
1595 }
1596
Daniel Willmann46553142014-09-03 17:46:44 +02001597 LOGPC(DMM, LOGL_INFO, "\n");
Harald Welte77289c22010-05-18 14:32:29 +02001598
Harald Welte2720e732010-05-17 00:44:57 +02001599 /* put the non-TLV elements in the TLV parser structure to
1600 * pass them on to the SGSN / GTP code */
1601 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
1602 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
1603 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
1604 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
1605
Harald Welte6abf94e2010-05-18 10:35:06 +02001606 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welte0c3eae02010-05-02 21:07:14 +02001607 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte6abf94e2010-05-18 10:35:06 +02001608
1609 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
1610 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
1611 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
1612 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1613 GSM_CAUSE_INV_MAND_INFO,
1614 0, NULL);
1615 }
1616
1617 /* Check if NSAPI is already in use */
Harald Welteb37515e2010-07-02 10:12:58 +02001618 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
1619 if (pdp) {
1620 /* We already have a PDP context for this TLLI + NSAPI tuple */
1621 if (pdp->sapi == act_req->req_llc_sapi &&
1622 pdp->ti == transaction_id) {
1623 /* This apparently is a re-transmission of a PDP CTX
1624 * ACT REQ (our ACT ACK must have got dropped) */
1625 return gsm48_tx_gsm_act_pdp_acc(pdp);
1626 }
1627
1628 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
Harald Welte6abf94e2010-05-18 10:35:06 +02001629 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1630 GSM_CAUSE_NSAPI_IN_USE,
1631 0, NULL);
1632 }
Harald Welte9b455bf2010-03-14 15:45:01 +08001633
Harald Welte19f9e302010-07-02 10:15:49 +02001634 /* Only increment counter for a real activation, after we checked
1635 * for re-transmissions */
1636 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
1637
Harald Welte11ff3e82010-07-02 10:17:24 +02001638 ggsn = sgsn_ggsn_ctx_by_id(0);
1639 if (!ggsn) {
1640 LOGP(DGPRS, LOGL_ERROR, "No GGSN context 0 found!\n");
1641 return -EIO;
Harald Welted193cb32010-05-17 22:58:03 +02001642 }
Harald Welte11ff3e82010-07-02 10:17:24 +02001643 ggsn->gsn = sgsn->gsn;
1644 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, act_req->req_nsapi, &tp);
1645 if (!pdp)
1646 return -1;
1647
1648 /* Store SAPI and Transaction Identifier */
1649 pdp->sapi = act_req->req_llc_sapi;
1650 pdp->ti = transaction_id;
1651
Harald Welte6463c072010-05-18 17:04:55 +02001652 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +08001653}
1654
1655/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welte77289c22010-05-18 14:32:29 +02001656static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001657{
Harald Welte943c5bc2010-04-30 16:33:12 +02001658 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte77289c22010-05-18 14:32:29 +02001659 uint8_t transaction_id = (gh->proto_discr >> 4);
1660 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001661
Daniel Willmann46553142014-09-03 17:46:44 +02001662 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001663 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001664
Harald Welte77289c22010-05-18 14:32:29 +02001665 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1666 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001667 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
Harald Welte77289c22010-05-18 14:32:29 +02001668 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1669 mm->imsi, transaction_id);
Harald Welte60ebc022010-06-08 18:08:59 +02001670 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
Harald Welte77289c22010-05-18 14:32:29 +02001671 }
1672
1673 return sgsn_delete_pdp_ctx(pdp);
Harald Welte9b455bf2010-03-14 15:45:01 +08001674}
1675
Harald Weltea9b473a2010-12-24 21:13:26 +01001676/* Section 9.5.9: Deactivate PDP Context Accept */
1677static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
1678{
1679 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
1680 uint8_t transaction_id = (gh->proto_discr >> 4);
1681 struct sgsn_pdp_ctx *pdp;
1682
Daniel Willmann46553142014-09-03 17:46:44 +02001683 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001684
1685 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1686 if (!pdp) {
Daniel Willmann46553142014-09-03 17:46:44 +02001687 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
Harald Weltea9b473a2010-12-24 21:13:26 +01001688 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1689 mm->imsi, transaction_id);
1690 return 0;
1691 }
1692
1693 return sgsn_delete_pdp_ctx(pdp);
1694}
1695
Harald Welted193cb32010-05-17 22:58:03 +02001696static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001697{
1698 struct gsm48_hdr *gh = msgb_l3(msg);
1699
Daniel Willmann46553142014-09-03 17:46:44 +02001700 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
Jacob Erlbeckafcf2302015-01-05 17:30:02 +01001701 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Harald Welte9b455bf2010-03-14 15:45:01 +08001702
1703 return 0;
1704}
1705
Harald Weltea9b473a2010-12-24 21:13:26 +01001706static void pdpctx_timer_cb(void *_pdp)
1707{
1708 struct sgsn_pdp_ctx *pdp = _pdp;
1709
1710 pdp->num_T_exp++;
1711
1712 switch (pdp->T) {
1713 case 3395: /* waiting for PDP CTX DEACT ACK */
1714 if (pdp->num_T_exp >= 4) {
Daniel Willmann46553142014-09-03 17:46:44 +02001715 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired >= 5 times\n");
Harald Weltea9b473a2010-12-24 21:13:26 +01001716 pdp->state = PDP_STATE_INACTIVE;
1717 sgsn_delete_pdp_ctx(pdp);
1718 break;
1719 }
1720 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001721 osmo_timer_schedule(&pdp->timer, GSM0408_T3395_SECS, 0);
Harald Weltea9b473a2010-12-24 21:13:26 +01001722 break;
1723 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001724 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
Harald Weltea9b473a2010-12-24 21:13:26 +01001725 pdp->T);
1726 }
1727}
1728
1729
Harald Welte9b455bf2010-03-14 15:45:01 +08001730/* GPRS Session Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001731static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1732 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001733{
Harald Welte943c5bc2010-04-30 16:33:12 +02001734 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001735 int rc;
1736
Harald Welte807a5d82010-06-01 11:53:01 +02001737 /* MMCTX can be NULL when called */
1738
Harald Welted193cb32010-05-17 22:58:03 +02001739 if (!mmctx) {
1740 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001741 /* 6.1.3.6 */
1742 if (gh->msg_type == GSM48_MT_GSM_STATUS)
1743 return 0;
1744
1745 return sgsn_force_reattach_oldmsg(msg);
Harald Welted193cb32010-05-17 22:58:03 +02001746 }
1747
Harald Welte9b455bf2010-03-14 15:45:01 +08001748 switch (gh->msg_type) {
1749 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001750 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001751 break;
1752 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001753 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte77289c22010-05-18 14:32:29 +02001754 break;
Harald Weltea9b473a2010-12-24 21:13:26 +01001755 case GSM48_MT_GSM_DEACT_PDP_ACK:
1756 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
1757 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001758 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001759 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001760 break;
1761 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
1762 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
1763 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
Daniel Willmann46553142014-09-03 17:46:44 +02001764 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001765 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001766 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001767 break;
1768 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001769 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001770 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001771 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001772 break;
1773
1774 }
1775
1776 return rc;
1777}
1778
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001779int gsm0408_gprs_force_reattach_oldmsg(struct msgb *msg)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001780{
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001781 int rc;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001782 gprs_llgmm_reset_oldmsg(msg, GPRS_SAPI_GMM);
1783
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001784 rc = gsm48_tx_gmm_detach_req_oldmsg(
1785 msg, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001786
Jacob Erlbeckabdf02b2014-10-31 12:20:49 +01001787 return rc;
1788}
1789
1790int gsm0408_gprs_force_reattach(struct sgsn_mm_ctx *mmctx)
1791{
1792 int rc;
1793 gprs_llgmm_reset(mmctx->llme);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001794
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001795 rc = gsm48_tx_gmm_detach_req(
1796 mmctx, GPRS_DET_T_MT_REATT_REQ, GMM_CAUSE_IMPL_DETACHED);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001797
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001798 mm_ctx_cleanup_free(mmctx, "forced reattach");
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001799
Jacob Erlbeckb1c074f2014-10-31 12:27:11 +01001800 return rc;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001801}
1802
Harald Welte9b455bf2010-03-14 15:45:01 +08001803/* Main entry point for incoming 04.08 GPRS messages */
Harald Welte807a5d82010-06-01 11:53:01 +02001804int gsm0408_gprs_rcvmsg(struct msgb *msg, struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001805{
Harald Welte943c5bc2010-04-30 16:33:12 +02001806 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001807 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +02001808 struct sgsn_mm_ctx *mmctx;
1809 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +08001810 int rc = -EINVAL;
1811
Harald Welted193cb32010-05-17 22:58:03 +02001812 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
1813 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8acd88f2010-05-18 10:57:45 +02001814 if (mmctx) {
Harald Welte6abf94e2010-05-18 10:35:06 +02001815 msgid2mmctx(mmctx, msg);
Harald Welte8acd88f2010-05-18 10:57:45 +02001816 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
Harald Welte807a5d82010-06-01 11:53:01 +02001817 mmctx->llme = llme;
Harald Welte8acd88f2010-05-18 10:57:45 +02001818 }
Harald Welted193cb32010-05-17 22:58:03 +02001819
1820 /* MMCTX can be NULL */
1821
Harald Welte9b455bf2010-03-14 15:45:01 +08001822 switch (pdisc) {
1823 case GSM48_PDISC_MM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001824 rc = gsm0408_rcv_gmm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001825 break;
1826 case GSM48_PDISC_SM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001827 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001828 break;
1829 default:
Daniel Willmann46553142014-09-03 17:46:44 +02001830 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 discriminator 0x%02x\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001831 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +02001832 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +08001833 break;
1834 }
1835
Jacob Erlbeck258ce3d2014-09-30 13:51:45 +02001836 /* MMCTX can be invalid */
1837
Harald Welte9b455bf2010-03-14 15:45:01 +08001838 return rc;
1839}
Harald Welte5bfe4992010-06-09 11:22:47 +02001840
1841int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli)
1842{
1843 struct sgsn_mm_ctx *mmctx;
1844
1845 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1846 if (!mmctx) {
1847 LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown "
1848 "TLLI=%08x\n", tlli);
1849 return -EINVAL;
1850 }
1851
Jacob Erlbeck7dba11f2014-12-22 17:58:18 +01001852 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1853 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmann46553142014-09-03 17:46:44 +02001854 LOGMMCTXP(LOGL_NOTICE, mmctx, "SUSPEND request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001855 "!= REGISTERED (TLLI=%08x)\n", tlli);
1856 return -EINVAL;
1857 }
1858
1859 /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */
1860 mmctx->mm_state = GMM_REGISTERED_SUSPENDED;
1861 return 0;
1862}
1863
1864int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli,
1865 uint8_t suspend_ref)
1866{
1867 struct sgsn_mm_ctx *mmctx;
1868
1869 /* FIXME: make use of suspend reference? */
1870
1871 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1872 if (!mmctx) {
1873 LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown "
1874 "TLLI=%08x\n", tlli);
1875 return -EINVAL;
1876 }
1877
Jacob Erlbeck7dba11f2014-12-22 17:58:18 +01001878 if (mmctx->mm_state != GMM_REGISTERED_NORMAL &&
1879 mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
Daniel Willmann46553142014-09-03 17:46:44 +02001880 LOGMMCTXP(LOGL_NOTICE, mmctx, "RESUME request while state "
Harald Welte5bfe4992010-06-09 11:22:47 +02001881 "!= SUSPENDED (TLLI=%08x)\n", tlli);
1882 /* FIXME: should we not simply ignore it? */
1883 return -EINVAL;
1884 }
1885
1886 /* Transition from SUSPENDED to NORMAL */
1887 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1888 return 0;
1889}