blob: 77cf3b1b6f1d32c8983ca0f76a875feb7076a617 [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>
34#include <osmocore/msgb.h>
35#include <osmocore/tlv.h>
Harald Welted6057092010-05-02 12:57:45 +020036#include <osmocore/gsm_utils.h>
37#include <osmocore/signal.h>
38#include <osmocore/talloc.h>
Harald Welte8acd88f2010-05-18 10:57:45 +020039#include <osmocore/rate_ctr.h>
Harald Welted6057092010-05-02 12:57:45 +020040
Harald Welte9b455bf2010-03-14 15:45:01 +080041#include <openbsc/debug.h>
42#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080043#include <openbsc/gsm_subscriber.h>
44#include <openbsc/gsm_04_08.h>
45#include <openbsc/gsm_04_08_gprs.h>
46#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080047#include <openbsc/transaction.h>
Harald Welte11d7c102010-05-02 11:54:55 +020048#include <openbsc/gprs_bssgp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080049#include <openbsc/gprs_llc.h>
50#include <openbsc/gprs_sgsn.h>
Harald Welte6abf94e2010-05-18 10:35:06 +020051#include <openbsc/gprs_gmm.h>
Harald Welted193cb32010-05-17 22:58:03 +020052#include <openbsc/sgsn.h>
53
Harald Welte6abf94e2010-05-18 10:35:06 +020054#include <pdp.h>
55
Harald Weltec2e8cc42010-05-31 20:23:38 +020056#define PTMSI_ALLOC
57
58/* Section 11.2.2 / Table 11.4 MM timers netowkr side */
59#define GSM0408_T3322_SECS 6 /* DETACH_REQ -> DETACH_ACC */
60#define GSM0408_T3350_SECS 6 /* waiting for ATT/RAU/TMSI COMPL */
61#define GSM0408_T3360_SECS 6 /* waiting for AUTH/CIPH RESP */
62#define GSM0408_T3370_SECS 6 /* waiting for ID RESP */
63
64/* Section 11.2.2 / Table 11.4a MM timers netowkr side */
65#define GSM0408_T3313_SECS 30 /* waiting for paging response */
66#define GSM0408_T3314_SECS 44 /* force to STBY on expiry */
67#define GSM0408_T3316_SECS 44
68
Harald Weltea9b473a2010-12-24 21:13:26 +010069/* Section 11.3 / Table 11.2d Timers of Session Management - network side */
70#define GSM0408_T3385_SECS 8 /* wait for ACT PDP CTX REQ */
71#define GSM0408_T3386_SECS 8 /* wait for MODIFY PDP CTX ACK */
72#define GSM0408_T3395_SECS 8 /* wait for DEACT PDP CTX ACK */
73#define GSM0408_T3397_SECS 8 /* wait for DEACT AA PDP CTX ACK */
74
Harald Welted193cb32010-05-17 22:58:03 +020075extern struct sgsn_instance *sgsn;
Harald Welte9b455bf2010-03-14 15:45:01 +080076
Harald Welte2720e732010-05-17 00:44:57 +020077/* Protocol related stuff, should go into libosmocore */
78
Harald Welte9b455bf2010-03-14 15:45:01 +080079/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
Harald Welte2720e732010-05-17 00:44:57 +020080const struct value_string gmm_cause_names[] = {
Harald Weltefd29a1f2010-07-01 12:26:59 +020081 { GMM_CAUSE_IMSI_UNKNOWN, "IMSI unknown in HLR" },
82 { GMM_CAUSE_ILLEGAL_MS, "Illegal MS" },
83 { GMM_CAUSE_ILLEGAL_ME, "Illegal ME" },
84 { GMM_CAUSE_GPRS_NOTALLOWED, "GPRS services not allowed" },
85 { GMM_CAUSE_GPRS_OTHER_NOTALLOWED,
86 "GPRS services and non-GPRS services not allowed" },
87 { GMM_CAUSE_MS_ID_NOT_DERIVED,
88 "MS identity cannot be derived by the network" },
89 { GMM_CAUSE_IMPL_DETACHED, "Implicitly detached" },
90 { GMM_CAUSE_PLMN_NOTALLOWED, "PLMN not allowed" },
91 { GMM_CAUSE_LA_NOTALLOWED, "Location Area not allowed" },
92 { GMM_CAUSE_ROAMING_NOTALLOWED,
93 "Roaming not allowed in this location area" },
94 { GMM_CAUSE_NO_GPRS_PLMN,
95 "GPRS services not allowed in this PLMN" },
96 { GMM_CAUSE_MSC_TEMP_NOTREACH, "MSC temporarily not reachable" },
97 { GMM_CAUSE_NET_FAIL, "Network failure" },
98 { GMM_CAUSE_CONGESTION, "Congestion" },
99 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
Harald Welte9b455bf2010-03-14 15:45:01 +0800100 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
101 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
102 "Message type non-existant or not implemented" },
103 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
104 "Message type not compatible with protocol state" },
105 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
106 "Information element non-existent or not implemented" },
Harald Weltefd29a1f2010-07-01 12:26:59 +0200107 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
Harald Welte9b455bf2010-03-14 15:45:01 +0800108 { GMM_CAUSE_MSG_INCOMP_P_STATE,
109 "Message not compatible with protocol state " },
Harald Weltefd29a1f2010-07-01 12:26:59 +0200110 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
Harald Welte9b455bf2010-03-14 15:45:01 +0800111 { 0, NULL }
112};
113
114/* 10.5.6.6 SM Cause / Table 10.5.157 */
Harald Welte2720e732010-05-17 00:44:57 +0200115const struct value_string gsm_cause_names[] = {
Harald Welte9b455bf2010-03-14 15:45:01 +0800116 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
117 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
118 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
119 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
120 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
121 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
122 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
123 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
124 "Requested service option not subscribed" },
125 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
126 "Service option temporarily out of order" },
127 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
128 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
129 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
130 { GSM_CAUSE_NET_FAIL, "Network Failure" },
131 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
132 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
133 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
134 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
135 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
136 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
137 "Message type non-existant or not implemented" },
138 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
139 "Message type not compatible with protocol state" },
140 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
141 "Information element non-existent or not implemented" },
142 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
143 { GSM_CAUSE_MSG_INCOMP_P_STATE,
144 "Message not compatible with protocol state " },
145 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
146 { 0, NULL }
147};
148
Harald Welte2720e732010-05-17 00:44:57 +0200149/* 10.5.5.2 */
150const struct value_string gprs_att_t_strs[] = {
151 { GPRS_ATT_T_ATTACH, "GPRS attach" },
152 { GPRS_ATT_T_ATT_WHILE_IMSI, "GPRS attach while IMSI attached" },
153 { GPRS_ATT_T_COMBINED, "Combined GPRS/IMSI attach" },
154 { 0, NULL }
155};
Harald Welte9b455bf2010-03-14 15:45:01 +0800156
Harald Welte2720e732010-05-17 00:44:57 +0200157const struct value_string gprs_upd_t_strs[] = {
158 { GPRS_UPD_T_RA, "RA updating" },
159 { GPRS_UPD_T_RA_LA, "combined RA/LA updating" },
160 { GPRS_UPD_T_RA_LA_IMSI_ATT, "combined RA/LA updating + IMSI attach" },
161 { GPRS_UPD_T_PERIODIC, "periodic updating" },
162 { 0, NULL }
163};
164
165/* 10.5.5.5 */
166const struct value_string gprs_det_t_mo_strs[] = {
167 { GPRS_DET_T_MO_GPRS, "GPRS detach" },
168 { GPRS_DET_T_MO_IMSI, "IMSI detach" },
169 { GPRS_DET_T_MO_COMBINED, "Combined GPRS/IMSI detach" },
170 { 0, NULL }
171};
172
Harald Welte588d44e2010-06-09 10:28:29 +0200173static const struct tlv_definition gsm48_gmm_att_tlvdef = {
174 .def = {
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200175 [GSM48_IE_GMM_CIPH_CKSN] = { TLV_TYPE_FIXED, 1 },
Harald Welte588d44e2010-06-09 10:28:29 +0200176 [GSM48_IE_GMM_TIMER_READY] = { TLV_TYPE_TV, 1 },
177 [GSM48_IE_GMM_ALLOC_PTMSI] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200178 [GSM48_IE_GMM_PTMSI_SIG] = { TLV_TYPE_FIXED, 3 },
179 [GSM48_IE_GMM_AUTH_RAND] = { TLV_TYPE_FIXED, 16 },
180 [GSM48_IE_GMM_AUTH_SRES] = { TLV_TYPE_FIXED, 4 },
Harald Welte588d44e2010-06-09 10:28:29 +0200181 [GSM48_IE_GMM_IMEISV] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200182 [GSM48_IE_GMM_DRX_PARAM] = { TLV_TYPE_FIXED, 2 },
Harald Welte588d44e2010-06-09 10:28:29 +0200183 [GSM48_IE_GMM_MS_NET_CAPA] = { TLV_TYPE_TLV, 0 },
184 [GSM48_IE_GMM_PDP_CTX_STATUS] = { TLV_TYPE_TLV, 0 },
185 [GSM48_IE_GMM_PS_LCS_CAPA] = { TLV_TYPE_TLV, 0 },
186 [GSM48_IE_GMM_GMM_MBMS_CTX_ST] = { TLV_TYPE_TLV, 0 },
187 },
188};
189
190static const struct tlv_definition gsm48_sm_att_tlvdef = {
191 .def = {
192 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
193 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
194 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
195 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
196 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
197 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200198 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
199 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
Harald Welte588d44e2010-06-09 10:28:29 +0200200 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
201 },
202};
203
Harald Welte2720e732010-05-17 00:44:57 +0200204/* Our implementation, should be kept in SGSN */
Harald Welte9b455bf2010-03-14 15:45:01 +0800205
Harald Weltec2e8cc42010-05-31 20:23:38 +0200206static void mmctx_timer_cb(void *_mm);
207
208static void mmctx_timer_start(struct sgsn_mm_ctx *mm, unsigned int T,
209 unsigned int seconds)
210{
211 if (bsc_timer_pending(&mm->timer))
212 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
213 "timer %u pending\n", T, mm->T);
214 mm->T = T;
215 mm->num_T_exp = 0;
216
217 /* FIXME: we should do this only once ? */
218 mm->timer.data = mm;
219 mm->timer.cb = &mmctx_timer_cb;
220
221 bsc_schedule_timer(&mm->timer, seconds, 0);
222}
223
224static void mmctx_timer_stop(struct sgsn_mm_ctx *mm, unsigned int T)
225{
226 if (mm->T != T)
227 LOGP(DMM, LOGL_ERROR, "Stopping MM timer %u but "
228 "%u is running\n", T, mm->T);
229 bsc_del_timer(&mm->timer);
230}
231
Harald Welte9b455bf2010-03-14 15:45:01 +0800232/* Send a message through the underlying layer */
Harald Welte8acd88f2010-05-18 10:57:45 +0200233static int gsm48_gmm_sendmsg(struct msgb *msg, int command,
234 const struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800235{
Harald Welte8acd88f2010-05-18 10:57:45 +0200236 if (mm)
237 rate_ctr_inc(&mm->ctrg->ctr[GMM_CTR_PKTS_SIG_OUT]);
238
Harald Welte11d7c102010-05-02 11:54:55 +0200239 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte56a01452010-05-31 22:12:30 +0200240 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800241}
242
Harald Welte11d7c102010-05-02 11:54:55 +0200243/* copy identifiers from old message to new message, this
244 * is required so lower layers can route it correctly */
245static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
246{
247 msgb_tlli(msg) = msgb_tlli(old);
248 msgb_bvci(msg) = msgb_bvci(old);
249 msgb_nsei(msg) = msgb_nsei(old);
250}
251
Harald Welte6abf94e2010-05-18 10:35:06 +0200252/* Store BVCI/NSEI in MM context */
253static void msgid2mmctx(struct sgsn_mm_ctx *mm, const struct msgb *msg)
254{
255 mm->bvci = msgb_bvci(msg);
256 mm->nsei = msgb_nsei(msg);
257}
258
259/* Store BVCI/NSEI in MM context */
260static void mmctx2msgid(struct msgb *msg, const struct sgsn_mm_ctx *mm)
261{
262 msgb_tlli(msg) = mm->tlli;
263 msgb_bvci(msg) = mm->bvci;
264 msgb_nsei(msg) = mm->nsei;
265}
266
Harald Weltef54e7e22010-06-09 10:51:23 +0200267/* Chapter 9.4.18 */
268static int _tx_status(struct msgb *msg, uint8_t cause,
269 struct sgsn_mm_ctx *mmctx, int sm)
270{
271 struct gsm48_hdr *gh;
272
273 /* MMCTX might be NULL! */
274
275 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
276 get_value_string(gmm_cause_names, cause));
277
278 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
279 if (sm) {
280 gh->proto_discr = GSM48_PDISC_SM_GPRS;
281 gh->msg_type = GSM48_MT_GSM_STATUS;
282 } else {
283 gh->proto_discr = GSM48_PDISC_MM_GPRS;
284 gh->msg_type = GSM48_MT_GMM_STATUS;
285 }
286 gh->data[0] = cause;
287
288 return gsm48_gmm_sendmsg(msg, 0, mmctx);
289}
290static int gsm48_tx_gmm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
291{
292 struct msgb *msg = gsm48_msgb_alloc();
293
294 mmctx2msgid(msg, mmctx);
295 return _tx_status(msg, cause, mmctx, 0);
296};
297static int gsm48_tx_gmm_status_oldmsg(struct msgb *oldmsg, uint8_t cause)
298{
299 struct msgb *msg = gsm48_msgb_alloc();
300
301 gmm_copy_id(msg, oldmsg);
302 return _tx_status(msg, cause, NULL, 0);
303}
304static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
305{
306 struct msgb *msg = gsm48_msgb_alloc();
307
308 mmctx2msgid(msg, mmctx);
309 return _tx_status(msg, cause, mmctx, 1);
310};
311static int gsm48_tx_sm_status_oldmsg(struct msgb *oldmsg, uint8_t cause)
312{
313 struct msgb *msg = gsm48_msgb_alloc();
314
315 gmm_copy_id(msg, oldmsg);
316 return _tx_status(msg, cause, NULL, 1);
317}
318
319
Harald Welte86fda902010-05-03 10:54:35 +0200320static struct gsm48_qos default_qos = {
321 .delay_class = 4, /* best effort */
322 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
323 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
324 .preced_class = GSM48_QOS_PC_NORMAL,
325 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
326 .traf_class = GSM48_QOS_TC_INTERACTIVE,
327 .deliv_order = GSM48_QOS_DO_UNORDERED,
328 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
329 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
330 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
331 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
332 .resid_ber = GSM48_QOS_RBER_5e_2,
333 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
334 .handling_prio = 3,
335 .xfer_delay = 0x10, /* 200ms */
336 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
337 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
338 .sig_ind = 0, /* not optimised for signalling */
339 .max_bitrate_down_ext = 0, /* use octet 9 */
340 .guar_bitrate_down_ext = 0, /* use octet 13 */
341};
342
Harald Welte9b455bf2010-03-14 15:45:01 +0800343/* Chapter 9.4.2: Attach accept */
Harald Welte6463c072010-05-18 17:04:55 +0200344static int gsm48_tx_gmm_att_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800345{
346 struct msgb *msg = gsm48_msgb_alloc();
347 struct gsm48_hdr *gh;
348 struct gsm48_attach_ack *aa;
Harald Weltecd4dd4d2010-05-18 17:20:49 +0200349 uint8_t *ptsig, *mid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800350
Harald Weltec2e8cc42010-05-31 20:23:38 +0200351 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT (new P-TMSI=0x%08x)\n", mm->p_tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800352
Harald Welte6463c072010-05-18 17:04:55 +0200353 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800354
355 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
356 gh->proto_discr = GSM48_PDISC_MM_GPRS;
357 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
358
359 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
360 aa->force_stby = 0; /* not indicated */
361 aa->att_result = 1; /* GPRS only */
362 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
363 aa->radio_prio = 4; /* lowest */
Harald Welte6463c072010-05-18 17:04:55 +0200364 gsm48_construct_ra(aa->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800365
Harald Welteab1d5622010-05-18 19:58:38 +0200366#if 0
Harald Welte6463c072010-05-18 17:04:55 +0200367 /* Optional: P-TMSI signature */
368 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
369 ptsig = msgb_put(msg, 3);
370 ptsig[0] = mm->p_tmsi_sig >> 16;
371 ptsig[1] = mm->p_tmsi_sig >> 8;
372 ptsig[2] = mm->p_tmsi_sig & 0xff;
373
374 /* Optional: Negotiated Ready timer value */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200375#endif
Harald Welte6463c072010-05-18 17:04:55 +0200376
Harald Weltec2e8cc42010-05-31 20:23:38 +0200377#ifdef PTMSI_ALLOC
Harald Welte6463c072010-05-18 17:04:55 +0200378 /* Optional: Allocated P-TMSI */
Harald Welte6463c072010-05-18 17:04:55 +0200379 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
380 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
Harald Welteab1d5622010-05-18 19:58:38 +0200381 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
382#endif
Harald Weltec2e8cc42010-05-31 20:23:38 +0200383
Harald Welte6463c072010-05-18 17:04:55 +0200384 /* Optional: MS-identity (combined attach) */
385 /* Optional: GMM cause (partial attach result for combined attach) */
386
Harald Welte56a01452010-05-31 22:12:30 +0200387 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800388}
389
390/* Chapter 9.4.5: Attach reject */
Harald Welte6463c072010-05-18 17:04:55 +0200391static int _tx_gmm_att_rej(struct msgb *msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800392{
Harald Welte9b455bf2010-03-14 15:45:01 +0800393 struct gsm48_hdr *gh;
394
395 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
396
Harald Welte9b455bf2010-03-14 15:45:01 +0800397 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
398 gh->proto_discr = GSM48_PDISC_MM_GPRS;
399 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
400 gh->data[0] = gmm_cause;
401
Harald Welte8acd88f2010-05-18 10:57:45 +0200402 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800403}
Harald Welte6463c072010-05-18 17:04:55 +0200404static int gsm48_tx_gmm_att_rej_oldmsg(const struct msgb *old_msg,
405 uint8_t gmm_cause)
406{
407 struct msgb *msg = gsm48_msgb_alloc();
408 gmm_copy_id(msg, old_msg);
409 return _tx_gmm_att_rej(msg, gmm_cause);
410}
411static int gsm48_tx_gmm_att_rej(struct sgsn_mm_ctx *mm,
412 uint8_t gmm_cause)
413{
414 struct msgb *msg = gsm48_msgb_alloc();
415 mmctx2msgid(msg, mm);
416 return _tx_gmm_att_rej(msg, gmm_cause);
417}
Harald Welte9b455bf2010-03-14 15:45:01 +0800418
Harald Welte2720e732010-05-17 00:44:57 +0200419/* Chapter 9.4.6.2 Detach accept */
Harald Weltecd4dd4d2010-05-18 17:20:49 +0200420static int gsm48_tx_gmm_det_ack(struct sgsn_mm_ctx *mm, uint8_t force_stby)
Harald Welte2720e732010-05-17 00:44:57 +0200421{
422 struct msgb *msg = gsm48_msgb_alloc();
423 struct gsm48_hdr *gh;
424
425 DEBUGP(DMM, "<- GPRS DETACH ACCEPT\n");
426
Harald Welte6463c072010-05-18 17:04:55 +0200427 mmctx2msgid(msg, mm);
428
Harald Welte2720e732010-05-17 00:44:57 +0200429 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
430 gh->proto_discr = GSM48_PDISC_MM_GPRS;
431 gh->msg_type = GSM48_MT_GMM_DETACH_ACK;
432 gh->data[0] = force_stby;
433
Harald Welte56a01452010-05-31 22:12:30 +0200434 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte2720e732010-05-17 00:44:57 +0200435}
436
Harald Welte9b455bf2010-03-14 15:45:01 +0800437/* Transmit Chapter 9.4.12 Identity Request */
Harald Welte6463c072010-05-18 17:04:55 +0200438static int gsm48_tx_gmm_id_req(struct sgsn_mm_ctx *mm, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800439{
440 struct msgb *msg = gsm48_msgb_alloc();
441 struct gsm48_hdr *gh;
442
Harald Weltedce12552010-05-31 22:31:04 +0200443 DEBUGP(DMM, "<- GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
Harald Welte9b455bf2010-03-14 15:45:01 +0800444
Harald Welte6463c072010-05-18 17:04:55 +0200445 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800446
447 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
448 gh->proto_discr = GSM48_PDISC_MM_GPRS;
449 gh->msg_type = GSM48_MT_GMM_ID_REQ;
450 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
451 gh->data[0] = id_type & 0xf;
452
Harald Weltedce12552010-05-31 22:31:04 +0200453 return gsm48_gmm_sendmsg(msg, 1, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800454}
455
Harald Welte4b6ac1e2010-07-03 11:09:06 +0200456/* Section 9.4.9: Authentication and Ciphering Request */
457static int gsm48_tx_gmm_auth_ciph_req(struct sgsn_mm_ctx *mm, uint8_t *rand,
458 uint8_t key_seq, uint8_t algo)
459{
460 struct msgb *msg = gsm48_msgb_alloc();
461 struct gsm48_hdr *gh;
462 struct gsm48_auth_ciph_req *acreq;
463 uint8_t *m_rand, *m_cksn;
464
465 DEBUGP(DMM, "<- GPRS AUTH AND CIPHERING REQ (rand = %s)\n",
466 hexdump(rand, 16));
467
468 mmctx2msgid(msg, mm);
469
470 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
471 gh->proto_discr = GSM48_PDISC_MM_GPRS;
472 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REQ;
473
474 acreq = (struct gsm48_auth_ciph_req *) msgb_put(msg, sizeof(*acreq));
475 acreq->ciph_alg = algo & 0xf;
476 acreq->imeisv_req = 0x1;
477 acreq->force_stby = 0x0;
478 acreq->ac_ref_nr = 0x0; /* FIXME: increment this? */
479
480 /* Only if authentication is requested we need to set RAND + CKSN */
481 if (rand) {
482 m_rand = msgb_put(msg, 16+1);
483 m_rand[0] = GSM48_IE_GMM_AUTH_RAND;
484 memcpy(m_rand+1, rand, 16);
485
486 m_cksn = msgb_put(msg, 1+1);
487 m_cksn[0] = GSM48_IE_GMM_CIPH_CKSN;
488 m_cksn[1] = key_seq;
489 }
490
491 /* Start T3360 */
492 mmctx_timer_start(mm, 3360, GSM0408_T3360_SECS);
493
494 /* FIXME: make sure we don't send any other messages to the MS */
495
496 return gsm48_gmm_sendmsg(msg, 1, mm);
497}
498
499/* Section 9.4.11: Authentication and Ciphering Reject */
500static int gsm48_tx_gmm_auth_ciph_rej(struct sgsn_mm_ctx *mm)
501{
502 struct msgb *msg = gsm48_msgb_alloc();
503 struct gsm48_hdr *gh;
504
505 DEBUGP(DMM, "<- GPRS AUTH AND CIPH REJECT\n");
506
507 mmctx2msgid(msg, mm);
508
509 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
510 gh->proto_discr = GSM48_PDISC_MM_GPRS;
511 gh->msg_type = GSM48_MT_GMM_AUTH_CIPH_REJ;
512
513 return gsm48_gmm_sendmsg(msg, 0, mm);
514}
515
516/* Section 9.4.10: Authentication and Ciphering Response */
517static int gsm48_rx_gmm_auth_ciph_resp(struct sgsn_mm_ctx *ctx,
518 struct msgb *msg)
519{
520 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
521 struct gsm48_auth_ciph_resp *acr = (struct gsm48_auth_ciph_resp *)gh->data;
522 struct tlv_parsed tp;
523 int rc;
524
525 /* FIXME: Stop T3360 */
526
527 rc = tlv_parse(&tp, &gsm48_gmm_att_tlvdef, acr->data,
528 (msg->data + msg->len) - acr->data, 0, 0);
529
530 /* FIXME: compare ac_ref? */
531
532 if (!TLVP_PRESENT(&tp, GSM48_IE_GMM_AUTH_SRES) ||
533 !TLVP_PRESENT(&tp, GSM48_IE_GMM_IMEISV)) {
534 /* FIXME: missing mandatory IE */
535 }
536
537 /* FIXME: compare SRES with what we expected */
538 /* FIXME: enable LLC cipheirng */
539 return 0;
540}
541
Harald Welte9b455bf2010-03-14 15:45:01 +0800542/* Check if we can already authorize a subscriber */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200543static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx,
544 enum gprs_t3350_mode t3350_mode)
Harald Welte9b455bf2010-03-14 15:45:01 +0800545{
546 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
Harald Weltec2e8cc42010-05-31 20:23:38 +0200547#ifdef PTMSI_ALLOC
548 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
549 ctx->t3350_mode = t3350_mode;
550 mmctx_timer_start(ctx, 3350, GSM0408_T3350_SECS);
551#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800552 ctx->mm_state = GMM_REGISTERED_NORMAL;
Harald Welte6463c072010-05-18 17:04:55 +0200553 return gsm48_tx_gmm_att_ack(ctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800554 }
Harald Weltec2e8cc42010-05-31 20:23:38 +0200555 if (!strlen(ctx->imei)) {
556 ctx->mm_state = GMM_COMMON_PROC_INIT;
557 ctx->t3370_id_type = GSM_MI_TYPE_IMEI;
558 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200559 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMEI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200560 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800561
Harald Weltec2e8cc42010-05-31 20:23:38 +0200562 if (!strlen(ctx->imsi)) {
563 ctx->mm_state = GMM_COMMON_PROC_INIT;
564 ctx->t3370_id_type = GSM_MI_TYPE_IMSI;
565 mmctx_timer_start(ctx, 3370, GSM0408_T3370_SECS);
Harald Welte6463c072010-05-18 17:04:55 +0200566 return gsm48_tx_gmm_id_req(ctx, GSM_MI_TYPE_IMSI);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200567 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800568
569 return 0;
570}
571
572/* Parse Chapter 9.4.13 Identity Response */
Harald Welted193cb32010-05-17 22:58:03 +0200573static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800574{
Harald Welte943c5bc2010-04-30 16:33:12 +0200575 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200576 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800577 char mi_string[GSM48_MI_SIZE];
Harald Welte9b455bf2010-03-14 15:45:01 +0800578
579 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte2720e732010-05-17 00:44:57 +0200580 DEBUGP(DMM, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
Harald Welte9b455bf2010-03-14 15:45:01 +0800581 mi_type, mi_string);
582
Harald Welte9b455bf2010-03-14 15:45:01 +0800583 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200584 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800585 return -EINVAL;
586 }
587
Harald Weltec2e8cc42010-05-31 20:23:38 +0200588 if (mi_type == ctx->t3370_id_type)
589 mmctx_timer_stop(ctx, 3370);
590
Harald Welte9b455bf2010-03-14 15:45:01 +0800591 switch (mi_type) {
592 case GSM_MI_TYPE_IMSI:
593 /* we already have a mm context with current TLLI, but no
594 * P-TMSI / IMSI yet. What we now need to do is to fill
595 * this initial context with data from the HLR */
Harald Weltec728eea2010-12-24 23:07:18 +0100596 if (strlen(ctx->imsi) == 0) {
597 /* Check if we already have a MM context for this IMSI */
598 struct sgsn_mm_ctx *ictx;
599 ictx = sgsn_mm_ctx_by_imsi(mi_string);
600 if (ictx) {
601 DEBUGP(DMM, "Deleting old MM Context for same IMSI ",
602 "p_tmsi_old=0x%08x, p_tmsi_new=0x%08x\n",
603 ictx->p_tmsi, ctx->p_tmsi);
604 gprs_llgmm_assign(ictx->llme, ictx->tlli,
605 0xffffffff, GPRS_ALGO_GEA0, NULL);
606 sgsn_mm_ctx_free(ictx);
607 }
608 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800609 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
610 break;
611 case GSM_MI_TYPE_IMEI:
612 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
613 break;
614 case GSM_MI_TYPE_IMEISV:
615 break;
616 }
617
618 DEBUGPC(DMM, "\n");
619 /* Check if we can let the mobile station enter */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200620 return gsm48_gmm_authorize(ctx, ctx->t3350_mode);
Harald Welte9b455bf2010-03-14 15:45:01 +0800621}
622
623/* Section 9.4.1 Attach request */
Harald Welte807a5d82010-06-01 11:53:01 +0200624static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *ctx, struct msgb *msg,
625 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800626{
Harald Welte943c5bc2010-04-30 16:33:12 +0200627 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte56a01452010-05-31 22:12:30 +0200628 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info, *ms_ra_acc_cap;
629 uint8_t msnc_len, att_type, mi_len, mi_type, ms_ra_acc_cap_len;
Harald Welteeaa614c2010-05-02 11:26:34 +0200630 uint16_t drx_par;
631 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800632 char mi_string[GSM48_MI_SIZE];
633 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200634 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800635
Harald Welte2720e732010-05-17 00:44:57 +0200636 DEBUGP(DMM, "-> GMM ATTACH REQUEST ");
Harald Welte9b455bf2010-03-14 15:45:01 +0800637
638 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
639 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
640 * or with random TLLI. */
641
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200642 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800643
644 /* MS network capability 10.5.5.12 */
645 msnc_len = *cur++;
646 msnc = cur;
Harald Weltec48ac472010-07-03 21:20:06 +0200647 if (msnc_len > 8)
Harald Welte9b455bf2010-03-14 15:45:01 +0800648 goto err_inval;
649 cur += msnc_len;
650
651 /* aTTACH Type 10.5.5.2 */
652 att_type = *cur++ & 0x0f;
653
654 /* DRX parameter 10.5.5.6 */
Harald Welte56a01452010-05-31 22:12:30 +0200655 drx_par = *cur++ << 8;
656 drx_par |= *cur++;
Harald Welte9b455bf2010-03-14 15:45:01 +0800657
658 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
659 mi_len = *cur++;
660 mi = cur;
661 if (mi_len > 8)
662 goto err_inval;
663 mi_type = *mi & GSM_MI_TYPE_MASK;
664 cur += mi_len;
665
666 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
667
Harald Welte2720e732010-05-17 00:44:57 +0200668 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string,
669 get_value_string(gprs_att_t_strs, att_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800670
671 /* Old routing area identification 10.5.5.15 */
672 old_ra_info = cur;
673 cur += 6;
674
675 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte56a01452010-05-31 22:12:30 +0200676 ms_ra_acc_cap_len = *cur++;
677 ms_ra_acc_cap = cur;
Harald Weltec48ac472010-07-03 21:20:06 +0200678 if (ms_ra_acc_cap_len > 51)
679 goto err_inval;
Harald Welte9b455bf2010-03-14 15:45:01 +0800680
681 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
682
683 switch (mi_type) {
684 case GSM_MI_TYPE_IMSI:
685 /* Try to find MM context based on IMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200686 if (!ctx)
687 ctx = sgsn_mm_ctx_by_imsi(mi_string);
Harald Welte9b455bf2010-03-14 15:45:01 +0800688 if (!ctx) {
689#if 0
690 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
691#else
Harald Welteeafe22c2010-12-24 15:39:08 +0100692 /* As a temorary hack, we simply assume that the IMSI exists,
693 * as long as it is part of 'our' network */
694 char mccmnc[16];
695 snprintf(mccmnc, sizeof(mccmnc), "%03d%02d", ra_id.mcc, ra_id.mnc);
696 if (strncmp(mccmnc, mi_string, 5)) {
697 LOGP(DMM, LOGL_INFO, "Rejecting ATTACH REQUESET IMSI=%s\n",
698 mi_string);
699 return gsm48_tx_gmm_att_rej_oldmsg(msg,
700 GMM_CAUSE_GPRS_NOTALLOWED);
701 }
Harald Welte9b455bf2010-03-14 15:45:01 +0800702 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
703 if (!ctx)
Harald Welte6463c072010-05-18 17:04:55 +0200704 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_NET_FAIL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800705 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
706#endif
707 }
Harald Welte943c5bc2010-04-30 16:33:12 +0200708 ctx->tlli = msgb_tlli(msg);
Harald Welte807a5d82010-06-01 11:53:01 +0200709 ctx->llme = llme;
Harald Welte68b99a42010-05-18 12:05:42 +0200710 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800711 break;
712 case GSM_MI_TYPE_TMSI:
Harald Welteab1d5622010-05-18 19:58:38 +0200713 memcpy(&tmsi, mi+1, 4);
714 tmsi = ntohl(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800715 /* Try to find MM context based on P-TMSI */
Harald Welte807a5d82010-06-01 11:53:01 +0200716 if (!ctx)
717 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
Harald Welte9b455bf2010-03-14 15:45:01 +0800718 if (!ctx) {
Harald Weltec728eea2010-12-24 23:07:18 +0100719 /* Allocate a context as most of our code expects one.
720 * Context will not have an IMSI ultil ID RESP is received */
Harald Welte943c5bc2010-04-30 16:33:12 +0200721 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte807a5d82010-06-01 11:53:01 +0200722 ctx->p_tmsi = tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800723 }
Harald Welte807a5d82010-06-01 11:53:01 +0200724 ctx->tlli = msgb_tlli(msg);
725 ctx->llme = llme;
726 msgid2mmctx(ctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800727 break;
728 default:
Harald Weltec2e8cc42010-05-31 20:23:38 +0200729 LOGP(DMM, LOGL_NOTICE, "Rejecting ATTACH REQUEST with "
730 "MI type %u\n", mi_type);
731 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_MS_ID_NOT_DERIVED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800732 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200733 /* Update MM Context with currient RA and Cell ID */
734 ctx->ra = ra_id;
735 ctx->cell_id = cid;
Harald Welte56a01452010-05-31 22:12:30 +0200736 /* Update MM Context with other data */
737 ctx->drx_parms = drx_par;
738 ctx->ms_radio_access_capa.len = ms_ra_acc_cap_len;
739 memcpy(ctx->ms_radio_access_capa.buf, ms_ra_acc_cap, ms_ra_acc_cap_len);
740 ctx->ms_network_capa.len = msnc_len;
741 memcpy(ctx->ms_network_capa.buf, msnc, msnc_len);
742
Harald Weltec2e8cc42010-05-31 20:23:38 +0200743#ifdef PTMSI_ALLOC
Harald Welte807a5d82010-06-01 11:53:01 +0200744 /* Allocate a new P-TMSI (+ P-TMSI signature) and update TLLI */
Harald Weltec2e8cc42010-05-31 20:23:38 +0200745 ctx->p_tmsi_old = ctx->p_tmsi;
Harald Welte6463c072010-05-18 17:04:55 +0200746 ctx->p_tmsi = sgsn_alloc_ptmsi();
Harald Welteab1d5622010-05-18 19:58:38 +0200747#endif
Harald Welte807a5d82010-06-01 11:53:01 +0200748 /* Even if there is no P-TMSI allocated, the MS will switch from
749 * foreign TLLI to local TLLI */
750 ctx->tlli_new = gprs_tmsi2tlli(ctx->p_tmsi, TLLI_LOCAL);
751
752 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +0200753 gprs_llgmm_assign(ctx->llme, ctx->tlli, ctx->tlli_new,
754 GPRS_ALGO_GEA0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800755
756 DEBUGPC(DMM, "\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +0200757 return ctx ? gsm48_gmm_authorize(ctx, GMM_T3350_MODE_ATT) : 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800758
759err_inval:
760 DEBUGPC(DMM, "\n");
Harald Welte6463c072010-05-18 17:04:55 +0200761 return gsm48_tx_gmm_att_rej_oldmsg(msg, GMM_CAUSE_SEM_INCORR_MSG);
Harald Welte9b455bf2010-03-14 15:45:01 +0800762}
763
Harald Welte2720e732010-05-17 00:44:57 +0200764/* Section 4.7.4.1 / 9.4.5.2 MO Detach request */
Harald Welted193cb32010-05-17 22:58:03 +0200765static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte2720e732010-05-17 00:44:57 +0200766{
767 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Weltec0d59502010-06-28 19:11:41 +0200768 struct sgsn_pdp_ctx *pdp, *pdp2;
Harald Welte2720e732010-05-17 00:44:57 +0200769 uint8_t detach_type, power_off;
Harald Weltebd5c9122010-06-28 22:18:53 +0200770 int rc;
Harald Welte2720e732010-05-17 00:44:57 +0200771
772 detach_type = gh->data[0] & 0x7;
773 power_off = gh->data[0] & 0x8;
774
775 /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */
776
777 DEBUGP(DMM, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n",
778 msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type),
779 power_off ? "Power-off" : "");
780
781 /* Mark MM state as deregistered */
782 ctx->mm_state = GMM_DEREGISTERED;
783
Harald Weltec0d59502010-06-28 19:11:41 +0200784 /* delete all existing PDP contexts for this MS */
785 llist_for_each_entry_safe(pdp, pdp2, &ctx->pdp_list, list) {
786 LOGP(DMM, LOGL_NOTICE, "Dropping PDP context for NSAPI=%u "
787 "due to GPRS DETACH REQUEST\n", pdp->nsapi);
788 sgsn_delete_pdp_ctx(pdp);
789 /* FIXME: the callback wants to transmit a DEACT PDP CTX ACK,
790 * which is quite stupid for a MS that has just detached.. */
791 }
792
Harald Welte2720e732010-05-17 00:44:57 +0200793 /* force_stby = 0 */
Harald Weltebd5c9122010-06-28 22:18:53 +0200794 rc = gsm48_tx_gmm_det_ack(ctx, 0);
795
796 /* TLLI unassignment */
Harald Welted6f582b2010-06-30 23:18:57 +0200797 gprs_llgmm_assign(ctx->llme, ctx->tlli, 0xffffffff,
798 GPRS_ALGO_GEA0, NULL);
Harald Weltebd5c9122010-06-28 22:18:53 +0200799
800 return rc;
Harald Welte2720e732010-05-17 00:44:57 +0200801}
802
Harald Welte9b455bf2010-03-14 15:45:01 +0800803/* Chapter 9.4.15: Routing area update accept */
Harald Welte6463c072010-05-18 17:04:55 +0200804static int gsm48_tx_gmm_ra_upd_ack(struct sgsn_mm_ctx *mm)
Harald Welte9b455bf2010-03-14 15:45:01 +0800805{
806 struct msgb *msg = gsm48_msgb_alloc();
807 struct gsm48_hdr *gh;
808 struct gsm48_ra_upd_ack *rua;
Harald Weltec2e8cc42010-05-31 20:23:38 +0200809 uint8_t *mid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800810
811 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
812
Harald Welte6463c072010-05-18 17:04:55 +0200813 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800814
815 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
816 gh->proto_discr = GSM48_PDISC_MM_GPRS;
817 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
818
819 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
820 rua->force_stby = 0; /* not indicated */
821 rua->upd_result = 0; /* RA updated */
822 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200823
Harald Welte6463c072010-05-18 17:04:55 +0200824 gsm48_construct_ra(rua->ra_id.digits, &mm->ra);
Harald Welte9b455bf2010-03-14 15:45:01 +0800825
Harald Weltec2e8cc42010-05-31 20:23:38 +0200826#if 0
827 /* Optional: P-TMSI signature */
828 msgb_v_put(msg, GSM48_IE_GMM_PTMSI_SIG);
829 ptsig = msgb_put(msg, 3);
830 ptsig[0] = mm->p_tmsi_sig >> 16;
831 ptsig[1] = mm->p_tmsi_sig >> 8;
832 ptsig[2] = mm->p_tmsi_sig & 0xff;
833#endif
834
835#ifdef PTMSI_ALLOC
836 /* Optional: Allocated P-TMSI */
837 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
838 gsm48_generate_mid_from_tmsi(mid, mm->p_tmsi);
839 mid[0] = GSM48_IE_GMM_ALLOC_PTMSI;
840#endif
841
842 /* Option: MS ID, ... */
Harald Welte56a01452010-05-31 22:12:30 +0200843 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +0800844}
845
846/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200847static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800848{
849 struct msgb *msg = gsm48_msgb_alloc();
850 struct gsm48_hdr *gh;
851
852 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
853
Harald Welte11d7c102010-05-02 11:54:55 +0200854 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800855
856 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
857 gh->proto_discr = GSM48_PDISC_MM_GPRS;
858 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
859 gh->data[0] = cause;
860 gh->data[1] = 0; /* ? */
861
862 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
Harald Welte8acd88f2010-05-18 10:57:45 +0200863 return gsm48_gmm_sendmsg(msg, 0, NULL);
Harald Welte9b455bf2010-03-14 15:45:01 +0800864}
865
Harald Welte588d44e2010-06-09 10:28:29 +0200866static void process_ms_ctx_status(struct sgsn_mm_ctx *mmctx,
867 uint16_t pdp_status)
868{
869 struct sgsn_pdp_ctx *pdp, *pdp2;
870 /* 24.008 4.7.5.1.3: If the PDP context status information element is
871 * included in ROUTING AREA UPDATE REQUEST message, then the network
872 * shall deactivate all those PDP contexts locally (without peer to
873 * peer signalling between the MS and the network), which are not in SM
874 * state PDP-INACTIVE on network side but are indicated by the MS as
875 * being in state PDP-INACTIVE. */
876
877 llist_for_each_entry_safe(pdp, pdp2, &mmctx->pdp_list, list) {
878 if (!(pdp_status & (1 << pdp->nsapi))) {
Harald Weltef54e7e22010-06-09 10:51:23 +0200879 LOGP(DMM, LOGL_NOTICE, "Dropping PDP context for NSAPI=%u "
Harald Welte588d44e2010-06-09 10:28:29 +0200880 "due to PDP CTX STATUS IE= 0x%04x\n",
881 pdp->nsapi, pdp_status);
882 sgsn_delete_pdp_ctx(pdp);
883 }
884 }
885}
886
Harald Welte9b455bf2010-03-14 15:45:01 +0800887/* Chapter 9.4.14: Routing area update request */
Harald Welte807a5d82010-06-01 11:53:01 +0200888static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
889 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800890{
Harald Welte943c5bc2010-04-30 16:33:12 +0200891 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200892 uint8_t *cur = gh->data;
Harald Welte588d44e2010-06-09 10:28:29 +0200893 uint8_t *ms_ra_acc_cap;
894 uint8_t ms_ra_acc_cap_len;
Harald Welte9b455bf2010-03-14 15:45:01 +0800895 struct gprs_ra_id old_ra_id;
Harald Welte588d44e2010-06-09 10:28:29 +0200896 struct tlv_parsed tp;
Harald Welteeaa614c2010-05-02 11:26:34 +0200897 uint8_t upd_type;
Harald Welte807a5d82010-06-01 11:53:01 +0200898 int rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800899
900 /* Update Type 10.5.5.18 */
901 upd_type = *cur++ & 0x0f;
902
Harald Welte2720e732010-05-17 00:44:57 +0200903 DEBUGP(DMM, "-> GMM RA UPDATE REQUEST type=\"%s\" ",
904 get_value_string(gprs_upd_t_strs, upd_type));
Harald Welte9b455bf2010-03-14 15:45:01 +0800905
906 /* Old routing area identification 10.5.5.15 */
907 gsm48_parse_ra(&old_ra_id, cur);
908 cur += 6;
909
910 /* MS Radio Access Capability 10.5.5.12a */
Harald Welte588d44e2010-06-09 10:28:29 +0200911 ms_ra_acc_cap_len = *cur++;
912 ms_ra_acc_cap = cur;
Harald Welte9b455bf2010-03-14 15:45:01 +0800913
914 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
915 * DRX parameter, MS network capability */
Harald Welte588d44e2010-06-09 10:28:29 +0200916 rc = tlv_parse(&tp, &gsm48_gmm_att_tlvdef, cur,
917 (msg->data + msg->len) - cur, 0, 0);
Harald Welte9b455bf2010-03-14 15:45:01 +0800918
919 switch (upd_type) {
920 case GPRS_UPD_T_RA_LA:
921 case GPRS_UPD_T_RA_LA_IMSI_ATT:
922 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
923 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
924 break;
925 case GPRS_UPD_T_RA:
926 case GPRS_UPD_T_PERIODIC:
927 break;
928 }
929
930 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Weltef6bd3402010-12-23 23:34:43 +0100931 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800932 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
933 /* The MS has to perform GPRS attach */
934 DEBUGPC(DMM, " REJECT\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +0200935 /* Device is still IMSI atached for CS but initiate GPRS ATTACH */
936 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_MS_ID_NOT_DERIVED);
Harald Welte9b455bf2010-03-14 15:45:01 +0800937 }
938
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200939 /* Update the MM context with the new RA-ID */
940 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
941 /* Update the MM context with the new TLLI */
942 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800943 /* FIXME: Update the MM context with the MS radio acc capabilities */
944 /* FIXME: Update the MM context with the MS network capabilities */
945
Harald Welte77289c22010-05-18 14:32:29 +0200946 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_RA_UPDATE]);
947
Harald Welte9b455bf2010-03-14 15:45:01 +0800948 DEBUGPC(DMM, " ACCEPT\n");
Harald Weltec2e8cc42010-05-31 20:23:38 +0200949#ifdef PTMSI_ALLOC
950 mmctx->p_tmsi_old = mmctx->p_tmsi;
951 mmctx->p_tmsi = sgsn_alloc_ptmsi();
952 /* Start T3350 and re-transmit up to 5 times until ATTACH COMPLETE */
953 mmctx->t3350_mode = GMM_T3350_MODE_RAU;
954 mmctx_timer_start(mmctx, 3350, GSM0408_T3350_SECS);
955#endif
Harald Welte807a5d82010-06-01 11:53:01 +0200956 /* Even if there is no P-TMSI allocated, the MS will switch from
957 * foreign TLLI to local TLLI */
958 mmctx->tlli_new = gprs_tmsi2tlli(mmctx->p_tmsi, TLLI_LOCAL);
Harald Weltec2e8cc42010-05-31 20:23:38 +0200959
Harald Welte807a5d82010-06-01 11:53:01 +0200960 /* Inform LLC layer about new TLLI but keep old active */
Harald Welted6f582b2010-06-30 23:18:57 +0200961 gprs_llgmm_assign(mmctx->llme, mmctx->tlli, mmctx->tlli_new,
962 GPRS_ALGO_GEA0, NULL);
Harald Welte807a5d82010-06-01 11:53:01 +0200963
Harald Welte588d44e2010-06-09 10:28:29 +0200964 /* Look at PDP Context Status IE and see if MS's view of
965 * activated/deactivated NSAPIs agrees with our view */
966 if (TLVP_PRESENT(&tp, GSM48_IE_GMM_PDP_CTX_STATUS)) {
967 uint16_t pdp_status = ntohs(*(uint16_t *)
968 TLVP_VAL(&tp, GSM48_IE_GMM_PDP_CTX_STATUS));
969 process_ms_ctx_status(mmctx, pdp_status);
970 }
971
Harald Welte807a5d82010-06-01 11:53:01 +0200972 /* Send RA UPDATE ACCEPT */
Harald Welte6463c072010-05-18 17:04:55 +0200973 return gsm48_tx_gmm_ra_upd_ack(mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800974}
975
Harald Welted193cb32010-05-17 22:58:03 +0200976static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +0800977{
978 struct gsm48_hdr *gh = msgb_l3(msg);
979
Harald Welte2720e732010-05-17 00:44:57 +0200980 DEBUGP(DMM, "-> GPRS MM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +0800981 get_value_string(gmm_cause_names, gh->data[0]));
982
983 return 0;
984}
985
986/* GPRS Mobility Management */
Harald Welte807a5d82010-06-01 11:53:01 +0200987static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
988 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +0800989{
Harald Welte943c5bc2010-04-30 16:33:12 +0200990 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800991 int rc;
992
Harald Welte807a5d82010-06-01 11:53:01 +0200993 /* MMCTX can be NULL when called */
994
Harald Welted193cb32010-05-17 22:58:03 +0200995 if (!mmctx &&
996 gh->msg_type != GSM48_MT_GMM_ATTACH_REQ &&
Harald Weltec2e8cc42010-05-31 20:23:38 +0200997 gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) {
Harald Welted193cb32010-05-17 22:58:03 +0200998 LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n");
Harald Weltef54e7e22010-06-09 10:51:23 +0200999 return gsm48_tx_gmm_status_oldmsg(msg, GMM_CAUSE_MS_ID_NOT_DERIVED);
Harald Welted193cb32010-05-17 22:58:03 +02001000 }
1001
Harald Welte9b455bf2010-03-14 15:45:01 +08001002 switch (gh->msg_type) {
1003 case GSM48_MT_GMM_RA_UPD_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001004 rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001005 break;
1006 case GSM48_MT_GMM_ATTACH_REQ:
Harald Welte807a5d82010-06-01 11:53:01 +02001007 rc = gsm48_rx_gmm_att_req(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001008 break;
1009 case GSM48_MT_GMM_ID_RESP:
Harald Welted193cb32010-05-17 22:58:03 +02001010 rc = gsm48_rx_gmm_id_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001011 break;
1012 case GSM48_MT_GMM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001013 rc = gsm48_rx_gmm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001014 break;
Harald Welte2720e732010-05-17 00:44:57 +02001015 case GSM48_MT_GMM_DETACH_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001016 rc = gsm48_rx_gmm_det_req(mmctx, msg);
Harald Welte2720e732010-05-17 00:44:57 +02001017 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001018 case GSM48_MT_GMM_ATTACH_COMPL:
1019 /* only in case SGSN offered new P-TMSI */
Harald Weltec2e8cc42010-05-31 20:23:38 +02001020 DEBUGP(DMM, "-> ATTACH COMPLETE\n");
1021 mmctx_timer_stop(mmctx, 3350);
1022 mmctx->p_tmsi_old = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001023 /* Unassign the old TLLI */
1024 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001025 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1026 GPRS_ALGO_GEA0, NULL);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001027 break;
Harald Welteab1d5622010-05-18 19:58:38 +02001028 case GSM48_MT_GMM_RA_UPD_COMPL:
1029 /* only in case SGSN offered new P-TMSI */
Harald Weltec2e8cc42010-05-31 20:23:38 +02001030 DEBUGP(DMM, "-> ROUTEING AREA UPDATE COMPLETE\n");
1031 mmctx_timer_stop(mmctx, 3350);
1032 mmctx->p_tmsi_old = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001033 /* Unassign the old TLLI */
1034 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001035 gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new,
1036 GPRS_ALGO_GEA0, NULL);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001037 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001038 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
Harald Weltec2e8cc42010-05-31 20:23:38 +02001039 DEBUGP(DMM, "-> PTMSI REALLLICATION COMPLETE\n");
1040 mmctx_timer_stop(mmctx, 3350);
1041 mmctx->p_tmsi_old = 0;
Harald Welte807a5d82010-06-01 11:53:01 +02001042 /* Unassign the old TLLI */
1043 mmctx->tlli = mmctx->tlli_new;
Harald Welted6f582b2010-06-30 23:18:57 +02001044 //gprs_llgmm_assign(mmctx->llme, 0xffffffff, mmctx->tlli_new, GPRS_ALGO_GEA0, NULL);
Harald Weltec2e8cc42010-05-31 20:23:38 +02001045 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001046 case GSM48_MT_GMM_AUTH_CIPH_RESP:
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001047 rc = gsm48_rx_gmm_auth_ciph_resp(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001048 break;
1049 default:
1050 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
1051 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001052 rc = gsm48_tx_gmm_status(mmctx, GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001053 break;
1054 }
1055
1056 return rc;
1057}
1058
Harald Weltec2e8cc42010-05-31 20:23:38 +02001059static void mmctx_timer_cb(void *_mm)
1060{
1061 struct sgsn_mm_ctx *mm = _mm;
1062
1063 mm->num_T_exp++;
1064
1065 switch (mm->T) {
1066 case 3350: /* waiting for ATTACH COMPLETE */
1067 if (mm->num_T_exp >= 5) {
1068 LOGP(DMM, LOGL_NOTICE, "T3350 expired >= 5 times\n");
1069 mm->mm_state = GMM_DEREGISTERED;
1070 /* FIXME: should we return some error? */
1071 break;
1072 }
1073 /* re-transmit the respective msg and re-start timer */
1074 switch (mm->t3350_mode) {
1075 case GMM_T3350_MODE_ATT:
1076 gsm48_tx_gmm_att_ack(mm);
1077 break;
1078 case GMM_T3350_MODE_RAU:
1079 gsm48_tx_gmm_ra_upd_ack(mm);
1080 break;
1081 case GMM_T3350_MODE_PTMSI_REALL:
1082 /* FIXME */
1083 break;
1084 }
1085 bsc_schedule_timer(&mm->timer, GSM0408_T3350_SECS, 0);
1086 break;
Harald Welte4b6ac1e2010-07-03 11:09:06 +02001087 case 3360: /* waiting for AUTH AND CIPH RESP */
1088 if (mm->num_T_exp >= 5) {
1089 LOGP(DMM, LOGL_NOTICE, "T3360 expired >= 5 times\n");
1090 mm->mm_state = GMM_DEREGISTERED;
1091 break;
1092 }
1093 /* FIXME: re-transmit the respective msg and re-start timer */
1094 bsc_schedule_timer(&mm->timer, GSM0408_T3360_SECS, 0);
1095 break;
Harald Weltec2e8cc42010-05-31 20:23:38 +02001096 case 3370: /* waiting for IDENTITY RESPONSE */
1097 if (mm->num_T_exp >= 5) {
1098 LOGP(DMM, LOGL_NOTICE, "T3370 expired >= 5 times\n");
1099 gsm48_tx_gmm_att_rej(mm, GMM_CAUSE_MS_ID_NOT_DERIVED);
1100 mm->mm_state = GMM_DEREGISTERED;
1101 break;
1102 }
1103 /* re-tranmit IDENTITY REQUEST and re-start timer */
1104 gsm48_tx_gmm_id_req(mm, mm->t3370_id_type);
1105 bsc_schedule_timer(&mm->timer, GSM0408_T3370_SECS, 0);
1106 break;
1107 default:
1108 LOGP(DMM, LOGL_ERROR, "timer expired in unknown mode %u\n",
1109 mm->T);
1110 }
1111}
1112
1113/* GPRS SESSION MANAGEMENT */
1114
Harald Weltea9b473a2010-12-24 21:13:26 +01001115static void pdpctx_timer_cb(void *_mm);
1116
1117static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T,
1118 unsigned int seconds)
1119{
1120 if (bsc_timer_pending(&pdp->timer))
1121 LOGP(DMM, LOGL_ERROR, "Starting MM timer %u while old "
1122 "timer %u pending\n", T, pdp->T);
1123 pdp->T = T;
1124 pdp->num_T_exp = 0;
1125
1126 /* FIXME: we should do this only once ? */
1127 pdp->timer.data = pdp;
1128 pdp->timer.cb = &pdpctx_timer_cb;
1129
1130 bsc_schedule_timer(&pdp->timer, seconds, 0);
1131}
1132
1133
Harald Welte421cba42010-05-02 23:11:50 +02001134static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
1135{
1136 uint8_t v[6];
1137
1138 v[0] = PDP_TYPE_ORG_IETF;
1139 v[1] = PDP_TYPE_N_IETF_IPv4;
1140 *(uint32_t *)(v+2) = htonl(ipaddr);
1141
1142 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1143}
1144
1145static void msgb_put_pdp_addr_ppp(struct msgb *msg)
1146{
1147 uint8_t v[2];
1148
1149 v[0] = PDP_TYPE_ORG_ETSI;
1150 v[1] = PDP_TYPE_N_ETSI_PPP;
1151
1152 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
1153}
1154
Harald Welte9b455bf2010-03-14 15:45:01 +08001155/* Section 9.5.2: Ativate PDP Context Accept */
Harald Welte6abf94e2010-05-18 10:35:06 +02001156int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
Harald Welte9b455bf2010-03-14 15:45:01 +08001157{
Harald Welte9b455bf2010-03-14 15:45:01 +08001158 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte9b455bf2010-03-14 15:45:01 +08001159 struct gsm48_hdr *gh;
Harald Welte6abf94e2010-05-18 10:35:06 +02001160 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001161
1162 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
1163
Harald Welte6abf94e2010-05-18 10:35:06 +02001164 mmctx2msgid(msg, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001165
1166 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1167 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1168 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +02001169
1170 /* Negotiated LLC SAPI */
Harald Welte6abf94e2010-05-18 10:35:06 +02001171 msgb_v_put(msg, pdp->sapi);
Harald Welte4e38ac72010-05-18 14:51:18 +02001172
Harald Welte55e0df72010-05-18 13:20:08 +02001173 /* FIXME: copy QoS parameters from original request */
1174 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
1175 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte4e38ac72010-05-18 14:51:18 +02001176
Harald Welte421cba42010-05-02 23:11:50 +02001177 /* Radio priority 10.5.7.2 */
Harald Welte6abf94e2010-05-18 10:35:06 +02001178 msgb_v_put(msg, pdp->lib->radio_pri);
Harald Welte4e38ac72010-05-18 14:51:18 +02001179
Harald Welte421cba42010-05-02 23:11:50 +02001180 /* PDP address */
Harald Welte4e38ac72010-05-18 14:51:18 +02001181 /* Highest 4 bits of first byte need to be set to 1, otherwise
1182 * the IE is identical with the 04.08 PDP Address IE */
1183 pdp->lib->eua.v[0] &= ~0xf0;
Harald Welte6abf94e2010-05-18 10:35:06 +02001184 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
1185 pdp->lib->eua.l, pdp->lib->eua.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001186 pdp->lib->eua.v[0] |= 0xf0;
1187
Harald Welte77289c22010-05-18 14:32:29 +02001188 /* Optional: Protocol configuration options (FIXME: why 'req') */
1189 if (pdp->lib->pco_req.l && pdp->lib->pco_req.v)
Harald Welte6abf94e2010-05-18 10:35:06 +02001190 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
Harald Welte3c0b9b92010-05-18 14:36:11 +02001191 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
Harald Welte4e38ac72010-05-18 14:51:18 +02001192
Harald Welte421cba42010-05-02 23:11:50 +02001193 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +08001194
Harald Welte8acd88f2010-05-18 10:57:45 +02001195 return gsm48_gmm_sendmsg(msg, 0, pdp->mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001196}
1197
Harald Welte6abf94e2010-05-18 10:35:06 +02001198/* Section 9.5.3: Activate PDP Context reject */
1199int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
1200 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
1201{
1202 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte6abf94e2010-05-18 10:35:06 +02001203 struct gsm48_hdr *gh;
1204 uint8_t transaction_id = tid ^ 0x8; /* flip */
1205
Harald Welte77289c22010-05-18 14:32:29 +02001206 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT REJ(cause=%u)\n", cause);
Harald Welte6abf94e2010-05-18 10:35:06 +02001207
1208 mmctx2msgid(msg, mm);
1209
1210 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1211 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1212 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
1213
1214 msgb_v_put(msg, cause);
1215 if (pco_len && pco_v)
1216 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
1217
Harald Welte8acd88f2010-05-18 10:57:45 +02001218 return gsm48_gmm_sendmsg(msg, 0, mm);
Harald Welte6abf94e2010-05-18 10:35:06 +02001219}
1220
Harald Weltea9b473a2010-12-24 21:13:26 +01001221/* Section 9.5.8: Deactivate PDP Context Request */
1222static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
1223 uint8_t sm_cause)
1224{
1225 struct msgb *msg = gsm48_msgb_alloc();
1226 struct gsm48_hdr *gh;
1227 uint8_t transaction_id = tid ^ 0x8; /* flip */
1228
1229 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT REQ\n");
1230
1231 mmctx2msgid(msg, mm);
1232
1233 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1234 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1235 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
1236
1237 msgb_v_put(msg, sm_cause);
1238
1239 return gsm48_gmm_sendmsg(msg, 0, mm);
1240}
1241int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause)
1242{
1243 pdpctx_timer_start(pdp, 3395, GSM0408_T3395_SECS);
1244
1245 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause);
1246}
1247
Harald Welte9b455bf2010-03-14 15:45:01 +08001248/* Section 9.5.9: Deactivate PDP Context Accept */
Harald Welte60ebc022010-06-08 18:08:59 +02001249static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
Harald Welte9b455bf2010-03-14 15:45:01 +08001250{
Harald Welte9b455bf2010-03-14 15:45:01 +08001251 struct msgb *msg = gsm48_msgb_alloc();
1252 struct gsm48_hdr *gh;
Harald Welte60ebc022010-06-08 18:08:59 +02001253 uint8_t transaction_id = tid ^ 0x8; /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +08001254
1255 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
1256
Harald Welte60ebc022010-06-08 18:08:59 +02001257 mmctx2msgid(msg, mm);
Harald Welte9b455bf2010-03-14 15:45:01 +08001258
1259 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1260 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
1261 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
1262
Harald Welte60ebc022010-06-08 18:08:59 +02001263 return gsm48_gmm_sendmsg(msg, 0, mm);
1264}
1265int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
1266{
1267 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
Harald Welte9b455bf2010-03-14 15:45:01 +08001268}
1269
1270/* Section 9.5.1: Activate PDP Context Request */
Harald Welted193cb32010-05-17 22:58:03 +02001271static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
1272 struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001273{
Harald Welte943c5bc2010-04-30 16:33:12 +02001274 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001275 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welte0c3eae02010-05-02 21:07:14 +02001276 uint8_t req_qos_len, req_pdpa_len;
1277 uint8_t *req_qos, *req_pdpa;
1278 struct tlv_parsed tp;
Harald Welte6abf94e2010-05-18 10:35:06 +02001279 uint8_t transaction_id = (gh->proto_discr >> 4);
Harald Welte11ff3e82010-07-02 10:17:24 +02001280 struct sgsn_ggsn_ctx *ggsn;
Harald Welte6abf94e2010-05-18 10:35:06 +02001281 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001282
Harald Welte6abf94e2010-05-18 10:35:06 +02001283 DEBUGP(DMM, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
1284 act_req->req_llc_sapi, act_req->req_nsapi);
Harald Welte8acd88f2010-05-18 10:57:45 +02001285
Harald Welte23c13762010-07-02 22:52:04 +02001286 /* FIXME: length checks! */
Harald Welte0c3eae02010-05-02 21:07:14 +02001287 req_qos_len = act_req->data[0];
1288 req_qos = act_req->data + 1; /* 10.5.6.5 */
1289 req_pdpa_len = act_req->data[1 + req_qos_len];
1290 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +08001291
Harald Welte23c13762010-07-02 22:52:04 +02001292 /* Optional: Access Point Name, Protocol Config Options */
1293 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
1294 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
1295 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
1296 else
1297 memset(&tp, 0, sizeof(tp));
1298
Harald Welte0c3eae02010-05-02 21:07:14 +02001299 switch (req_pdpa[0] & 0xf) {
1300 case 0x0:
1301 DEBUGPC(DMM, "ETSI ");
1302 break;
1303 case 0x1:
1304 DEBUGPC(DMM, "IETF ");
1305 break;
1306 case 0xf:
1307 DEBUGPC(DMM, "Empty ");
1308 break;
1309 }
1310
1311 switch (req_pdpa[1]) {
1312 case 0x21:
1313 DEBUGPC(DMM, "IPv4 ");
1314 if (req_pdpa_len >= 6) {
1315 struct in_addr ia;
1316 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
1317 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
1318 }
1319 break;
1320 case 0x57:
1321 DEBUGPC(DMM, "IPv6 ");
1322 if (req_pdpa_len >= 18) {
1323 /* FIXME: print IPv6 address */
1324 }
1325 break;
1326 default:
1327 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
1328 break;
1329 }
1330
Harald Welte77289c22010-05-18 14:32:29 +02001331 DEBUGPC(DMM, "\n");
1332
Harald Welte2720e732010-05-17 00:44:57 +02001333 /* put the non-TLV elements in the TLV parser structure to
1334 * pass them on to the SGSN / GTP code */
1335 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
1336 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
1337 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
1338 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
1339
Harald Welte6abf94e2010-05-18 10:35:06 +02001340 /* FIXME: determine GGSN based on APN and subscription options */
Harald Welte0c3eae02010-05-02 21:07:14 +02001341 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
Harald Welte6abf94e2010-05-18 10:35:06 +02001342
1343 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
1344 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
1345 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
1346 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1347 GSM_CAUSE_INV_MAND_INFO,
1348 0, NULL);
1349 }
1350
1351 /* Check if NSAPI is already in use */
Harald Welteb37515e2010-07-02 10:12:58 +02001352 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
1353 if (pdp) {
1354 /* We already have a PDP context for this TLLI + NSAPI tuple */
1355 if (pdp->sapi == act_req->req_llc_sapi &&
1356 pdp->ti == transaction_id) {
1357 /* This apparently is a re-transmission of a PDP CTX
1358 * ACT REQ (our ACT ACK must have got dropped) */
1359 return gsm48_tx_gsm_act_pdp_acc(pdp);
1360 }
1361
1362 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
Harald Welte6abf94e2010-05-18 10:35:06 +02001363 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
1364 GSM_CAUSE_NSAPI_IN_USE,
1365 0, NULL);
1366 }
Harald Welte9b455bf2010-03-14 15:45:01 +08001367
Harald Welte19f9e302010-07-02 10:15:49 +02001368 /* Only increment counter for a real activation, after we checked
1369 * for re-transmissions */
1370 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PDP_CTX_ACT]);
1371
Harald Welte11ff3e82010-07-02 10:17:24 +02001372 ggsn = sgsn_ggsn_ctx_by_id(0);
1373 if (!ggsn) {
1374 LOGP(DGPRS, LOGL_ERROR, "No GGSN context 0 found!\n");
1375 return -EIO;
Harald Welted193cb32010-05-17 22:58:03 +02001376 }
Harald Welte11ff3e82010-07-02 10:17:24 +02001377 ggsn->gsn = sgsn->gsn;
1378 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, act_req->req_nsapi, &tp);
1379 if (!pdp)
1380 return -1;
1381
1382 /* Store SAPI and Transaction Identifier */
1383 pdp->sapi = act_req->req_llc_sapi;
1384 pdp->ti = transaction_id;
1385
Harald Welte6463c072010-05-18 17:04:55 +02001386 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +08001387}
1388
1389/* Section 9.5.8: Deactivate PDP Context Request */
Harald Welte77289c22010-05-18 14:32:29 +02001390static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001391{
Harald Welte943c5bc2010-04-30 16:33:12 +02001392 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte77289c22010-05-18 14:32:29 +02001393 uint8_t transaction_id = (gh->proto_discr >> 4);
1394 struct sgsn_pdp_ctx *pdp;
Harald Welte9b455bf2010-03-14 15:45:01 +08001395
Harald Welte2720e732010-05-17 00:44:57 +02001396 DEBUGP(DMM, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001397 get_value_string(gsm_cause_names, gh->data[0]));
1398
Harald Welte77289c22010-05-18 14:32:29 +02001399 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1400 if (!pdp) {
1401 LOGP(DMM, LOGL_NOTICE, "Deactivate PDP Context Request for "
1402 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1403 mm->imsi, transaction_id);
Harald Welte60ebc022010-06-08 18:08:59 +02001404 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
Harald Welte77289c22010-05-18 14:32:29 +02001405 }
1406
1407 return sgsn_delete_pdp_ctx(pdp);
Harald Welte9b455bf2010-03-14 15:45:01 +08001408}
1409
Harald Weltea9b473a2010-12-24 21:13:26 +01001410/* Section 9.5.9: Deactivate PDP Context Accept */
1411static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
1412{
1413 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
1414 uint8_t transaction_id = (gh->proto_discr >> 4);
1415 struct sgsn_pdp_ctx *pdp;
1416
1417 DEBUGP(DMM, "-> DEACTIVATE PDP CONTEXT ACK\n");
1418
1419 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
1420 if (!pdp) {
1421 LOGP(DMM, LOGL_NOTICE, "Deactivate PDP Context Accept for "
1422 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
1423 mm->imsi, transaction_id);
1424 return 0;
1425 }
1426
1427 return sgsn_delete_pdp_ctx(pdp);
1428}
1429
Harald Welted193cb32010-05-17 22:58:03 +02001430static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
Harald Welte9b455bf2010-03-14 15:45:01 +08001431{
1432 struct gsm48_hdr *gh = msgb_l3(msg);
1433
Harald Welte2720e732010-05-17 00:44:57 +02001434 DEBUGP(DMM, "-> GPRS SM STATUS (cause: %s)\n",
Harald Welte9b455bf2010-03-14 15:45:01 +08001435 get_value_string(gsm_cause_names, gh->data[0]));
1436
1437 return 0;
1438}
1439
Harald Weltea9b473a2010-12-24 21:13:26 +01001440static void pdpctx_timer_cb(void *_pdp)
1441{
1442 struct sgsn_pdp_ctx *pdp = _pdp;
1443
1444 pdp->num_T_exp++;
1445
1446 switch (pdp->T) {
1447 case 3395: /* waiting for PDP CTX DEACT ACK */
1448 if (pdp->num_T_exp >= 4) {
1449 LOGP(DMM, LOGL_NOTICE, "T3395 expired >= 5 times\n");
1450 pdp->state = PDP_STATE_INACTIVE;
1451 sgsn_delete_pdp_ctx(pdp);
1452 break;
1453 }
1454 gsm48_tx_gsm_deact_pdp_req(pdp, GSM_CAUSE_NET_FAIL);
1455 bsc_schedule_timer(&pdp->timer, GSM0408_T3395_SECS, 0);
1456 break;
1457 default:
1458 LOGP(DMM, LOGL_ERROR, "timer expired in unknown mode %u\n",
1459 pdp->T);
1460 }
1461}
1462
1463
Harald Welte9b455bf2010-03-14 15:45:01 +08001464/* GPRS Session Management */
Harald Welte807a5d82010-06-01 11:53:01 +02001465static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
1466 struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001467{
Harald Welte943c5bc2010-04-30 16:33:12 +02001468 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001469 int rc;
1470
Harald Welte807a5d82010-06-01 11:53:01 +02001471 /* MMCTX can be NULL when called */
1472
Harald Welted193cb32010-05-17 22:58:03 +02001473 if (!mmctx) {
1474 LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
Harald Welte18d14522010-07-02 16:18:18 +02001475 gsm48_tx_gmm_status_oldmsg(msg, GMM_CAUSE_IMPL_DETACHED);
1476 return gsm48_tx_sm_status_oldmsg(msg, GSM_CAUSE_PROTO_ERR_UNSPEC);
Harald Welted193cb32010-05-17 22:58:03 +02001477 }
1478
Harald Welte9b455bf2010-03-14 15:45:01 +08001479 switch (gh->msg_type) {
1480 case GSM48_MT_GSM_ACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001481 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001482 break;
1483 case GSM48_MT_GSM_DEACT_PDP_REQ:
Harald Welted193cb32010-05-17 22:58:03 +02001484 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
Harald Welte77289c22010-05-18 14:32:29 +02001485 break;
Harald Weltea9b473a2010-12-24 21:13:26 +01001486 case GSM48_MT_GSM_DEACT_PDP_ACK:
1487 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
1488 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001489 case GSM48_MT_GSM_STATUS:
Harald Welted193cb32010-05-17 22:58:03 +02001490 rc = gsm48_rx_gsm_status(mmctx, msg);
Harald Welte9b455bf2010-03-14 15:45:01 +08001491 break;
1492 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
1493 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
1494 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
1495 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
1496 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001497 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001498 break;
1499 default:
1500 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
1501 gh->msg_type);
Harald Weltef54e7e22010-06-09 10:51:23 +02001502 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
Harald Welte9b455bf2010-03-14 15:45:01 +08001503 break;
1504
1505 }
1506
1507 return rc;
1508}
1509
1510/* Main entry point for incoming 04.08 GPRS messages */
Harald Welte807a5d82010-06-01 11:53:01 +02001511int gsm0408_gprs_rcvmsg(struct msgb *msg, struct gprs_llc_llme *llme)
Harald Welte9b455bf2010-03-14 15:45:01 +08001512{
Harald Welte943c5bc2010-04-30 16:33:12 +02001513 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +02001514 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welted193cb32010-05-17 22:58:03 +02001515 struct sgsn_mm_ctx *mmctx;
1516 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +08001517 int rc = -EINVAL;
1518
Harald Welted193cb32010-05-17 22:58:03 +02001519 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
1520 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte8acd88f2010-05-18 10:57:45 +02001521 if (mmctx) {
Harald Welte6abf94e2010-05-18 10:35:06 +02001522 msgid2mmctx(mmctx, msg);
Harald Welte8acd88f2010-05-18 10:57:45 +02001523 rate_ctr_inc(&mmctx->ctrg->ctr[GMM_CTR_PKTS_SIG_IN]);
Harald Welte807a5d82010-06-01 11:53:01 +02001524 mmctx->llme = llme;
Harald Welte8acd88f2010-05-18 10:57:45 +02001525 }
Harald Welted193cb32010-05-17 22:58:03 +02001526
1527 /* MMCTX can be NULL */
1528
Harald Welte9b455bf2010-03-14 15:45:01 +08001529 switch (pdisc) {
1530 case GSM48_PDISC_MM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001531 rc = gsm0408_rcv_gmm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001532 break;
1533 case GSM48_PDISC_SM_GPRS:
Harald Welte807a5d82010-06-01 11:53:01 +02001534 rc = gsm0408_rcv_gsm(mmctx, msg, llme);
Harald Welte9b455bf2010-03-14 15:45:01 +08001535 break;
1536 default:
1537 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
1538 pdisc);
Harald Welte6abf94e2010-05-18 10:35:06 +02001539 /* FIXME: return status message */
Harald Welte9b455bf2010-03-14 15:45:01 +08001540 break;
1541 }
1542
1543 return rc;
1544}
Harald Welte5bfe4992010-06-09 11:22:47 +02001545
1546int gprs_gmm_rx_suspend(struct gprs_ra_id *raid, uint32_t tlli)
1547{
1548 struct sgsn_mm_ctx *mmctx;
1549
1550 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1551 if (!mmctx) {
1552 LOGP(DMM, LOGL_NOTICE, "SUSPEND request for unknown "
1553 "TLLI=%08x\n", tlli);
1554 return -EINVAL;
1555 }
1556
1557 if (mmctx->mm_state != GMM_REGISTERED_NORMAL) {
1558 LOGP(DMM, LOGL_NOTICE, "SUSPEND request while state "
1559 "!= REGISTERED (TLLI=%08x)\n", tlli);
1560 return -EINVAL;
1561 }
1562
1563 /* Transition from REGISTERED_NORMAL to REGISTERED_SUSPENDED */
1564 mmctx->mm_state = GMM_REGISTERED_SUSPENDED;
1565 return 0;
1566}
1567
1568int gprs_gmm_rx_resume(struct gprs_ra_id *raid, uint32_t tlli,
1569 uint8_t suspend_ref)
1570{
1571 struct sgsn_mm_ctx *mmctx;
1572
1573 /* FIXME: make use of suspend reference? */
1574
1575 mmctx = sgsn_mm_ctx_by_tlli(tlli, raid);
1576 if (!mmctx) {
1577 LOGP(DMM, LOGL_NOTICE, "RESUME request for unknown "
1578 "TLLI=%08x\n", tlli);
1579 return -EINVAL;
1580 }
1581
1582 if (mmctx->mm_state != GMM_REGISTERED_SUSPENDED) {
1583 LOGP(DMM, LOGL_NOTICE, "RESUME request while state "
1584 "!= SUSPENDED (TLLI=%08x)\n", tlli);
1585 /* FIXME: should we not simply ignore it? */
1586 return -EINVAL;
1587 }
1588
1589 /* Transition from SUSPENDED to NORMAL */
1590 mmctx->mm_state = GMM_REGISTERED_NORMAL;
1591 return 0;
1592}