blob: 1980c2266e60ed212cab32ac248cf0e48b64bc59 [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 Welte9b455bf2010-03-14 15:45:01 +08005 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
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>
31
32#include <openbsc/db.h>
33#include <osmocore/msgb.h>
34#include <osmocore/tlv.h>
35#include <openbsc/debug.h>
36#include <openbsc/gsm_data.h>
37#include <osmocore/gsm_utils.h>
38#include <openbsc/gsm_subscriber.h>
39#include <openbsc/gsm_04_08.h>
40#include <openbsc/gsm_04_08_gprs.h>
41#include <openbsc/paging.h>
42#include <osmocore/signal.h>
43#include <osmocore/talloc.h>
44#include <openbsc/transaction.h>
Harald Welte11d7c102010-05-02 11:54:55 +020045#include <openbsc/gprs_bssgp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080046#include <openbsc/gprs_llc.h>
47#include <openbsc/gprs_sgsn.h>
48
49/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
50struct value_string gmm_cause_names[] = {
51 /* FIXME */
52 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
53 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
54 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
55 "Message type non-existant or not implemented" },
56 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
57 "Message type not compatible with protocol state" },
58 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
59 "Information element non-existent or not implemented" },
60 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
61 { GMM_CAUSE_MSG_INCOMP_P_STATE,
62 "Message not compatible with protocol state " },
63 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
64 { 0, NULL }
65};
66
67/* 10.5.6.6 SM Cause / Table 10.5.157 */
68struct value_string gsm_cause_names[] = {
69 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
70 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
71 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
72 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
73 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
74 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
75 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
76 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
77 "Requested service option not subscribed" },
78 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
79 "Service option temporarily out of order" },
80 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
81 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
82 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
83 { GSM_CAUSE_NET_FAIL, "Network Failure" },
84 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
85 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
86 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
87 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
88 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
89 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
90 "Message type non-existant or not implemented" },
91 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
92 "Message type not compatible with protocol state" },
93 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
94 "Information element non-existent or not implemented" },
95 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
96 { GSM_CAUSE_MSG_INCOMP_P_STATE,
97 "Message not compatible with protocol state " },
98 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
99 { 0, NULL }
100};
101
Harald Welteeaa614c2010-05-02 11:26:34 +0200102static const char *att_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800103{
104 switch (type) {
105 case GPRS_ATT_T_ATTACH:
106 return "GPRS attach";
107 case GPRS_ATT_T_ATT_WHILE_IMSI:
108 return "GPRS attach while IMSI attached";
109 case GPRS_ATT_T_COMBINED:
110 return "Combined GPRS/IMSI attach";
111 default:
112 return "unknown";
113 }
114}
115
Harald Welteeaa614c2010-05-02 11:26:34 +0200116static const char *upd_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800117{
118 switch (type) {
119 case GPRS_UPD_T_RA:
120 return "RA updating";
121 case GPRS_UPD_T_RA_LA:
122 return "combined RA/LA updating";
123 case GPRS_UPD_T_RA_LA_IMSI_ATT:
124 return "combined RA/LA updating + IMSI attach";
125 case GPRS_UPD_T_PERIODIC:
126 return "periodic updating";
127 }
128 return "unknown";
129}
130
Harald Welte9b455bf2010-03-14 15:45:01 +0800131/* Send a message through the underlying layer */
132static int gsm48_gmm_sendmsg(struct msgb *msg, int command)
133{
Harald Welte11d7c102010-05-02 11:54:55 +0200134 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800135 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command);
136}
137
Harald Welte11d7c102010-05-02 11:54:55 +0200138/* copy identifiers from old message to new message, this
139 * is required so lower layers can route it correctly */
140static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
141{
142 msgb_tlli(msg) = msgb_tlli(old);
143 msgb_bvci(msg) = msgb_bvci(old);
144 msgb_nsei(msg) = msgb_nsei(old);
145}
146
Harald Welte9b455bf2010-03-14 15:45:01 +0800147/* Chapter 9.4.2: Attach accept */
148static int gsm48_tx_gmm_att_ack(struct msgb *old_msg)
149{
150 struct msgb *msg = gsm48_msgb_alloc();
151 struct gsm48_hdr *gh;
152 struct gsm48_attach_ack *aa;
Harald Welte11d7c102010-05-02 11:54:55 +0200153 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800154
155 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n");
156
Harald Welte11d7c102010-05-02 11:54:55 +0200157 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800158
159 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
160 gh->proto_discr = GSM48_PDISC_MM_GPRS;
161 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
162
163 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
164 aa->force_stby = 0; /* not indicated */
165 aa->att_result = 1; /* GPRS only */
166 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
167 aa->radio_prio = 4; /* lowest */
Harald Welte11d7c102010-05-02 11:54:55 +0200168 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
169 gsm48_construct_ra(aa->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800170
171 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
172 return gsm48_gmm_sendmsg(msg, 0);
173}
174
175/* Chapter 9.4.5: Attach reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200176static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800177{
178 struct msgb *msg = gsm48_msgb_alloc();
179 struct gsm48_hdr *gh;
180
181 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
182
Harald Welte11d7c102010-05-02 11:54:55 +0200183 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800184
185 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
186 gh->proto_discr = GSM48_PDISC_MM_GPRS;
187 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
188 gh->data[0] = gmm_cause;
189
190 return gsm48_gmm_sendmsg(msg, 0);
191}
192
193/* Transmit Chapter 9.4.12 Identity Request */
Harald Welteeaa614c2010-05-02 11:26:34 +0200194static int gsm48_tx_gmm_id_req(struct msgb *old_msg, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800195{
196 struct msgb *msg = gsm48_msgb_alloc();
197 struct gsm48_hdr *gh;
198
199 DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
200
Harald Welte11d7c102010-05-02 11:54:55 +0200201 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800202
203 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
204 gh->proto_discr = GSM48_PDISC_MM_GPRS;
205 gh->msg_type = GSM48_MT_GMM_ID_REQ;
206 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
207 gh->data[0] = id_type & 0xf;
208
209 return gsm48_gmm_sendmsg(msg, 0);
210}
211
212/* Check if we can already authorize a subscriber */
213static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg)
214{
215 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
216 ctx->mm_state = GMM_REGISTERED_NORMAL;
217 return gsm48_tx_gmm_att_ack(msg);
218 }
219 if (!strlen(ctx->imei))
220 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI);
221
222 if (!strlen(ctx->imsi))
223 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI);
224
225 return 0;
226}
227
228/* Parse Chapter 9.4.13 Identity Response */
229static int gsm48_rx_gmm_id_resp(struct msgb *msg)
230{
Harald Welte943c5bc2010-04-30 16:33:12 +0200231 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200232 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800233 char mi_string[GSM48_MI_SIZE];
234 struct gprs_ra_id ra_id;
235 struct sgsn_mm_ctx *ctx;
236
237 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
238 DEBUGP(DMM, "GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
239 mi_type, mi_string);
240
Harald Welte11d7c102010-05-02 11:54:55 +0200241 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte943c5bc2010-04-30 16:33:12 +0200242 ctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800243 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200244 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800245 return -EINVAL;
246 }
247
248 switch (mi_type) {
249 case GSM_MI_TYPE_IMSI:
250 /* we already have a mm context with current TLLI, but no
251 * P-TMSI / IMSI yet. What we now need to do is to fill
252 * this initial context with data from the HLR */
253 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
254 break;
255 case GSM_MI_TYPE_IMEI:
256 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
257 break;
258 case GSM_MI_TYPE_IMEISV:
259 break;
260 }
261
262 DEBUGPC(DMM, "\n");
263 /* Check if we can let the mobile station enter */
264 return gsm48_gmm_authorize(ctx, msg);
265}
266
267static void attach_rej_cb(void *data)
268{
269 struct sgsn_mm_ctx *ctx = data;
270
271 /* FIXME: determine through which BTS/TRX to send this */
272 //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED);
273 ctx->mm_state = GMM_DEREGISTERED;
274 /* FIXME: release the context */
275}
276
277static void schedule_reject(struct sgsn_mm_ctx *ctx)
278{
279 ctx->T = 3370;
280 ctx->timer.cb = attach_rej_cb;
281 ctx->timer.data = ctx;
282 bsc_schedule_timer(&ctx->timer, 6, 0);
283}
284
285/* Section 9.4.1 Attach request */
286static int gsm48_rx_gmm_att_req(struct msgb *msg)
287{
Harald Welte943c5bc2010-04-30 16:33:12 +0200288 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200289 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info;
290 uint8_t msnc_len, att_type, mi_len, mi_type;
291 uint16_t drx_par;
292 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800293 char mi_string[GSM48_MI_SIZE];
294 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200295 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800296 struct sgsn_mm_ctx *ctx;
297
298 DEBUGP(DMM, "GMM ATTACH REQUEST ");
299
300 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
301 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
302 * or with random TLLI. */
303
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200304 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800305
306 /* MS network capability 10.5.5.12 */
307 msnc_len = *cur++;
308 msnc = cur;
309 if (msnc_len > 2)
310 goto err_inval;
311 cur += msnc_len;
312
313 /* aTTACH Type 10.5.5.2 */
314 att_type = *cur++ & 0x0f;
315
316 /* DRX parameter 10.5.5.6 */
317 drx_par = *cur++;
318 drx_par |= *cur++ << 8;
319
320 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
321 mi_len = *cur++;
322 mi = cur;
323 if (mi_len > 8)
324 goto err_inval;
325 mi_type = *mi & GSM_MI_TYPE_MASK;
326 cur += mi_len;
327
328 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
329
330 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, att_name(att_type));
331
332 /* Old routing area identification 10.5.5.15 */
333 old_ra_info = cur;
334 cur += 6;
335
336 /* MS Radio Access Capability 10.5.5.12a */
337
338 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
339
340 switch (mi_type) {
341 case GSM_MI_TYPE_IMSI:
342 /* Try to find MM context based on IMSI */
343 ctx = sgsn_mm_ctx_by_imsi(mi_string);
344 if (!ctx) {
345#if 0
346 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
347#else
348 /* As a temorary hack, we simply assume that the IMSI exists */
349 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
350 if (!ctx)
351 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL);
352 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
353#endif
354 }
355 /* FIXME: Start some timer */
356 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200357 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800358 break;
359 case GSM_MI_TYPE_TMSI:
360 tmsi = strtoul(mi_string, NULL, 10);
361 /* Try to find MM context based on P-TMSI */
362 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
363 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200364 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800365 /* FIXME: Start some timer */
366 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200367 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800368 }
369 break;
370 default:
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200371 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800372 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200373 /* Update MM Context with currient RA and Cell ID */
374 ctx->ra = ra_id;
375 ctx->cell_id = cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800376
377 /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */
378 /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */
379
380 DEBUGPC(DMM, "\n");
381
382 return ctx ? gsm48_gmm_authorize(ctx, msg) : 0;
383
384err_inval:
385 DEBUGPC(DMM, "\n");
386 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG);
387}
388
389/* Chapter 9.4.15: Routing area update accept */
390static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg)
391{
392 struct msgb *msg = gsm48_msgb_alloc();
393 struct gsm48_hdr *gh;
394 struct gsm48_ra_upd_ack *rua;
Harald Welte11d7c102010-05-02 11:54:55 +0200395 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800396
397 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
398
Harald Welte11d7c102010-05-02 11:54:55 +0200399 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800400
401 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
402 gh->proto_discr = GSM48_PDISC_MM_GPRS;
403 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
404
405 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
406 rua->force_stby = 0; /* not indicated */
407 rua->upd_result = 0; /* RA updated */
408 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200409
410 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
411 gsm48_construct_ra(rua->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800412
413 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
414 return gsm48_gmm_sendmsg(msg, 0);
415}
416
417/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200418static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800419{
420 struct msgb *msg = gsm48_msgb_alloc();
421 struct gsm48_hdr *gh;
422
423 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
424
Harald Welte11d7c102010-05-02 11:54:55 +0200425 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800426
427 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
428 gh->proto_discr = GSM48_PDISC_MM_GPRS;
429 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
430 gh->data[0] = cause;
431 gh->data[1] = 0; /* ? */
432
433 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
434 return gsm48_gmm_sendmsg(msg, 0);
435}
436
437/* Chapter 9.4.14: Routing area update request */
438static int gsm48_rx_gmm_ra_upd_req(struct msgb *msg)
439{
Harald Welte943c5bc2010-04-30 16:33:12 +0200440 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800441 struct sgsn_mm_ctx *mmctx;
Harald Welteeaa614c2010-05-02 11:26:34 +0200442 uint8_t *cur = gh->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800443 struct gprs_ra_id old_ra_id;
Harald Welteeaa614c2010-05-02 11:26:34 +0200444 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800445
446 /* Update Type 10.5.5.18 */
447 upd_type = *cur++ & 0x0f;
448
449 DEBUGP(DMM, "GMM RA UPDATE REQUEST type=\"%s\" ", upd_name(upd_type));
450
451 /* Old routing area identification 10.5.5.15 */
452 gsm48_parse_ra(&old_ra_id, cur);
453 cur += 6;
454
455 /* MS Radio Access Capability 10.5.5.12a */
456
457 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
458 * DRX parameter, MS network capability */
459
460 switch (upd_type) {
461 case GPRS_UPD_T_RA_LA:
462 case GPRS_UPD_T_RA_LA_IMSI_ATT:
463 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
464 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
465 break;
466 case GPRS_UPD_T_RA:
467 case GPRS_UPD_T_PERIODIC:
468 break;
469 }
470
471 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte943c5bc2010-04-30 16:33:12 +0200472 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800473 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
474 /* The MS has to perform GPRS attach */
475 DEBUGPC(DMM, " REJECT\n");
476 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED);
477 }
478
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200479 /* Update the MM context with the new RA-ID */
480 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
481 /* Update the MM context with the new TLLI */
482 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800483 /* FIXME: Update the MM context with the MS radio acc capabilities */
484 /* FIXME: Update the MM context with the MS network capabilities */
485
486 DEBUGPC(DMM, " ACCEPT\n");
487 return gsm48_tx_gmm_ra_upd_ack(msg);
488}
489
490static int gsm48_rx_gmm_status(struct msgb *msg)
491{
492 struct gsm48_hdr *gh = msgb_l3(msg);
493
494 DEBUGP(DMM, "GPRS MM STATUS (cause: %s)\n",
495 get_value_string(gmm_cause_names, gh->data[0]));
496
497 return 0;
498}
499
500/* GPRS Mobility Management */
501static int gsm0408_rcv_gmm(struct msgb *msg)
502{
Harald Welte943c5bc2010-04-30 16:33:12 +0200503 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800504 int rc;
505
506 switch (gh->msg_type) {
507 case GSM48_MT_GMM_RA_UPD_REQ:
508 rc = gsm48_rx_gmm_ra_upd_req(msg);
509 break;
510 case GSM48_MT_GMM_ATTACH_REQ:
511 rc = gsm48_rx_gmm_att_req(msg);
512 break;
513 case GSM48_MT_GMM_ID_RESP:
514 rc = gsm48_rx_gmm_id_resp(msg);
515 break;
516 case GSM48_MT_GMM_STATUS:
517 rc = gsm48_rx_gmm_status(msg);
518 break;
519 case GSM48_MT_GMM_RA_UPD_COMPL:
520 /* only in case SGSN offered new P-TMSI */
521 case GSM48_MT_GMM_ATTACH_COMPL:
522 /* only in case SGSN offered new P-TMSI */
523 case GSM48_MT_GMM_DETACH_REQ:
524 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
525 case GSM48_MT_GMM_AUTH_CIPH_RESP:
526 DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n",
527 gh->msg_type);
528 break;
529 default:
530 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
531 gh->msg_type);
532 break;
533 }
534
535 return rc;
536}
537
538/* Section 9.5.2: Ativate PDP Context Accept */
539static int gsm48_tx_gsm_act_pdp_acc(struct msgb *old_msg, struct gsm48_act_pdp_ctx_req *req)
540{
Harald Welte943c5bc2010-04-30 16:33:12 +0200541 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800542 struct msgb *msg = gsm48_msgb_alloc();
543 struct gsm48_act_pdp_ctx_ack *act_ack;
544 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200545 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800546
547 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
548
Harald Welte11d7c102010-05-02 11:54:55 +0200549 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800550
551 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
552 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
553 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
554 act_ack = (struct gsm48_act_pdp_ctx_ack *)
555 msgb_put(msg, sizeof(*act_ack));
556 act_ack->llc_sapi = req->req_llc_sapi;
557 memcpy(act_ack->qos_lv, req->req_qos_lv, sizeof(act_ack->qos_lv));
558 //act_ack->radio_prio = 4;
559
560 return gsm48_gmm_sendmsg(msg, 0);
561}
562
563/* Section 9.5.9: Deactivate PDP Context Accept */
564static int gsm48_tx_gsm_deact_pdp_acc(struct msgb *old_msg)
565{
Harald Welte943c5bc2010-04-30 16:33:12 +0200566 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800567 struct msgb *msg = gsm48_msgb_alloc();
568 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200569 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800570
571 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
572
Harald Welte11d7c102010-05-02 11:54:55 +0200573 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800574
575 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
576 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
577 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
578
579 return gsm48_gmm_sendmsg(msg, 0);
580}
581
582/* Section 9.5.1: Activate PDP Context Request */
583static int gsm48_rx_gsm_act_pdp_req(struct msgb *msg)
584{
Harald Welte943c5bc2010-04-30 16:33:12 +0200585 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800586 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteeaa614c2010-05-02 11:26:34 +0200587 uint8_t *pdp_addr_lv = act_req->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800588
589 DEBUGP(DMM, "ACTIVATE PDP CONTEXT REQ\n");
590
591 /* FIXME: parse access point name + IPCP config options */
592
593 return gsm48_tx_gsm_act_pdp_acc(msg, act_req);
594}
595
596/* Section 9.5.8: Deactivate PDP Context Request */
597static int gsm48_rx_gsm_deact_pdp_req(struct msgb *msg)
598{
Harald Welte943c5bc2010-04-30 16:33:12 +0200599 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800600
601 DEBUGP(DMM, "DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
602 get_value_string(gsm_cause_names, gh->data[0]));
603
604 return gsm48_tx_gsm_deact_pdp_acc(msg);
605}
606
607static int gsm48_rx_gsm_status(struct msgb *msg)
608{
609 struct gsm48_hdr *gh = msgb_l3(msg);
610
611 DEBUGP(DMM, "GPRS SM STATUS (cause: %s)\n",
612 get_value_string(gsm_cause_names, gh->data[0]));
613
614 return 0;
615}
616
617/* GPRS Session Management */
618static int gsm0408_rcv_gsm(struct msgb *msg)
619{
Harald Welte943c5bc2010-04-30 16:33:12 +0200620 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800621 int rc;
622
623 switch (gh->msg_type) {
624 case GSM48_MT_GSM_ACT_PDP_REQ:
625 rc = gsm48_rx_gsm_act_pdp_req(msg);
626 break;
627 case GSM48_MT_GSM_DEACT_PDP_REQ:
628 rc = gsm48_rx_gsm_deact_pdp_req(msg);
629 case GSM48_MT_GSM_STATUS:
630 rc = gsm48_rx_gsm_status(msg);
631 break;
632 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
633 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
634 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
635 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
636 gh->msg_type);
637 break;
638 default:
639 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
640 gh->msg_type);
641 break;
642
643 }
644
645 return rc;
646}
647
648/* Main entry point for incoming 04.08 GPRS messages */
649int gsm0408_gprs_rcvmsg(struct msgb *msg)
650{
Harald Welte943c5bc2010-04-30 16:33:12 +0200651 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200652 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte9b455bf2010-03-14 15:45:01 +0800653 int rc = -EINVAL;
654
655 switch (pdisc) {
656 case GSM48_PDISC_MM_GPRS:
657 rc = gsm0408_rcv_gmm(msg);
658 break;
659 case GSM48_PDISC_SM_GPRS:
660 rc = gsm0408_rcv_gsm(msg);
661 break;
662 default:
663 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
664 pdisc);
665 break;
666 }
667
668 return rc;
669}