blob: 1eb4ab213d6e3baaed0a809f826916358b8fcc54 [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>
Harald Welted6057092010-05-02 12:57:45 +020035#include <osmocore/gsm_utils.h>
36#include <osmocore/signal.h>
37#include <osmocore/talloc.h>
38
Harald Welte9b455bf2010-03-14 15:45:01 +080039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080041#include <openbsc/gsm_subscriber.h>
42#include <openbsc/gsm_04_08.h>
43#include <openbsc/gsm_04_08_gprs.h>
44#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080045#include <openbsc/transaction.h>
Harald Welte11d7c102010-05-02 11:54:55 +020046#include <openbsc/gprs_bssgp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080047#include <openbsc/gprs_llc.h>
48#include <openbsc/gprs_sgsn.h>
49
50/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
51struct value_string gmm_cause_names[] = {
52 /* FIXME */
53 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
54 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
55 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
56 "Message type non-existant or not implemented" },
57 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
58 "Message type not compatible with protocol state" },
59 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
60 "Information element non-existent or not implemented" },
61 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
62 { GMM_CAUSE_MSG_INCOMP_P_STATE,
63 "Message not compatible with protocol state " },
64 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
65 { 0, NULL }
66};
67
68/* 10.5.6.6 SM Cause / Table 10.5.157 */
69struct value_string gsm_cause_names[] = {
70 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
71 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
72 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
73 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
74 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
75 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
76 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
77 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
78 "Requested service option not subscribed" },
79 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
80 "Service option temporarily out of order" },
81 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
82 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
83 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
84 { GSM_CAUSE_NET_FAIL, "Network Failure" },
85 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
86 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
87 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
88 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
89 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
90 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
91 "Message type non-existant or not implemented" },
92 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
93 "Message type not compatible with protocol state" },
94 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
95 "Information element non-existent or not implemented" },
96 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
97 { GSM_CAUSE_MSG_INCOMP_P_STATE,
98 "Message not compatible with protocol state " },
99 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
100 { 0, NULL }
101};
102
Harald Welteeaa614c2010-05-02 11:26:34 +0200103static const char *att_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800104{
105 switch (type) {
106 case GPRS_ATT_T_ATTACH:
107 return "GPRS attach";
108 case GPRS_ATT_T_ATT_WHILE_IMSI:
109 return "GPRS attach while IMSI attached";
110 case GPRS_ATT_T_COMBINED:
111 return "Combined GPRS/IMSI attach";
112 default:
113 return "unknown";
114 }
115}
116
Harald Welteeaa614c2010-05-02 11:26:34 +0200117static const char *upd_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800118{
119 switch (type) {
120 case GPRS_UPD_T_RA:
121 return "RA updating";
122 case GPRS_UPD_T_RA_LA:
123 return "combined RA/LA updating";
124 case GPRS_UPD_T_RA_LA_IMSI_ATT:
125 return "combined RA/LA updating + IMSI attach";
126 case GPRS_UPD_T_PERIODIC:
127 return "periodic updating";
128 }
129 return "unknown";
130}
131
Harald Welte9b455bf2010-03-14 15:45:01 +0800132/* Send a message through the underlying layer */
133static int gsm48_gmm_sendmsg(struct msgb *msg, int command)
134{
Harald Welte11d7c102010-05-02 11:54:55 +0200135 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800136 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command);
137}
138
Harald Welte11d7c102010-05-02 11:54:55 +0200139/* copy identifiers from old message to new message, this
140 * is required so lower layers can route it correctly */
141static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
142{
143 msgb_tlli(msg) = msgb_tlli(old);
144 msgb_bvci(msg) = msgb_bvci(old);
145 msgb_nsei(msg) = msgb_nsei(old);
146}
147
Harald Welte9b455bf2010-03-14 15:45:01 +0800148/* Chapter 9.4.2: Attach accept */
149static int gsm48_tx_gmm_att_ack(struct msgb *old_msg)
150{
151 struct msgb *msg = gsm48_msgb_alloc();
152 struct gsm48_hdr *gh;
153 struct gsm48_attach_ack *aa;
Harald Welte11d7c102010-05-02 11:54:55 +0200154 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800155
156 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n");
157
Harald Welte11d7c102010-05-02 11:54:55 +0200158 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800159
160 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
161 gh->proto_discr = GSM48_PDISC_MM_GPRS;
162 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
163
164 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
165 aa->force_stby = 0; /* not indicated */
166 aa->att_result = 1; /* GPRS only */
167 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
168 aa->radio_prio = 4; /* lowest */
Harald Welteba021102010-05-02 21:36:36 +0200169 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200170 gsm48_construct_ra(aa->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800171
172 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
173 return gsm48_gmm_sendmsg(msg, 0);
174}
175
176/* Chapter 9.4.5: Attach reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200177static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800178{
179 struct msgb *msg = gsm48_msgb_alloc();
180 struct gsm48_hdr *gh;
181
182 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
183
Harald Welte11d7c102010-05-02 11:54:55 +0200184 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800185
186 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
187 gh->proto_discr = GSM48_PDISC_MM_GPRS;
188 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
189 gh->data[0] = gmm_cause;
190
191 return gsm48_gmm_sendmsg(msg, 0);
192}
193
194/* Transmit Chapter 9.4.12 Identity Request */
Harald Welteeaa614c2010-05-02 11:26:34 +0200195static int gsm48_tx_gmm_id_req(struct msgb *old_msg, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800196{
197 struct msgb *msg = gsm48_msgb_alloc();
198 struct gsm48_hdr *gh;
199
200 DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
201
Harald Welte11d7c102010-05-02 11:54:55 +0200202 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800203
204 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
205 gh->proto_discr = GSM48_PDISC_MM_GPRS;
206 gh->msg_type = GSM48_MT_GMM_ID_REQ;
207 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
208 gh->data[0] = id_type & 0xf;
209
210 return gsm48_gmm_sendmsg(msg, 0);
211}
212
213/* Check if we can already authorize a subscriber */
214static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg)
215{
216 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
217 ctx->mm_state = GMM_REGISTERED_NORMAL;
218 return gsm48_tx_gmm_att_ack(msg);
219 }
220 if (!strlen(ctx->imei))
221 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI);
222
223 if (!strlen(ctx->imsi))
224 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI);
225
226 return 0;
227}
228
229/* Parse Chapter 9.4.13 Identity Response */
230static int gsm48_rx_gmm_id_resp(struct msgb *msg)
231{
Harald Welte943c5bc2010-04-30 16:33:12 +0200232 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200233 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800234 char mi_string[GSM48_MI_SIZE];
235 struct gprs_ra_id ra_id;
236 struct sgsn_mm_ctx *ctx;
237
238 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
239 DEBUGP(DMM, "GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
240 mi_type, mi_string);
241
Harald Welte11d7c102010-05-02 11:54:55 +0200242 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte943c5bc2010-04-30 16:33:12 +0200243 ctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800244 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200245 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800246 return -EINVAL;
247 }
248
249 switch (mi_type) {
250 case GSM_MI_TYPE_IMSI:
251 /* we already have a mm context with current TLLI, but no
252 * P-TMSI / IMSI yet. What we now need to do is to fill
253 * this initial context with data from the HLR */
254 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
255 break;
256 case GSM_MI_TYPE_IMEI:
257 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
258 break;
259 case GSM_MI_TYPE_IMEISV:
260 break;
261 }
262
263 DEBUGPC(DMM, "\n");
264 /* Check if we can let the mobile station enter */
265 return gsm48_gmm_authorize(ctx, msg);
266}
267
268static void attach_rej_cb(void *data)
269{
270 struct sgsn_mm_ctx *ctx = data;
271
272 /* FIXME: determine through which BTS/TRX to send this */
273 //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED);
274 ctx->mm_state = GMM_DEREGISTERED;
275 /* FIXME: release the context */
276}
277
278static void schedule_reject(struct sgsn_mm_ctx *ctx)
279{
280 ctx->T = 3370;
281 ctx->timer.cb = attach_rej_cb;
282 ctx->timer.data = ctx;
283 bsc_schedule_timer(&ctx->timer, 6, 0);
284}
285
286/* Section 9.4.1 Attach request */
287static int gsm48_rx_gmm_att_req(struct msgb *msg)
288{
Harald Welte943c5bc2010-04-30 16:33:12 +0200289 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200290 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info;
291 uint8_t msnc_len, att_type, mi_len, mi_type;
292 uint16_t drx_par;
293 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800294 char mi_string[GSM48_MI_SIZE];
295 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200296 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800297 struct sgsn_mm_ctx *ctx;
298
299 DEBUGP(DMM, "GMM ATTACH REQUEST ");
300
301 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
302 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
303 * or with random TLLI. */
304
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200305 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800306
307 /* MS network capability 10.5.5.12 */
308 msnc_len = *cur++;
309 msnc = cur;
310 if (msnc_len > 2)
311 goto err_inval;
312 cur += msnc_len;
313
314 /* aTTACH Type 10.5.5.2 */
315 att_type = *cur++ & 0x0f;
316
317 /* DRX parameter 10.5.5.6 */
318 drx_par = *cur++;
319 drx_par |= *cur++ << 8;
320
321 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
322 mi_len = *cur++;
323 mi = cur;
324 if (mi_len > 8)
325 goto err_inval;
326 mi_type = *mi & GSM_MI_TYPE_MASK;
327 cur += mi_len;
328
329 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
330
331 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, att_name(att_type));
332
333 /* Old routing area identification 10.5.5.15 */
334 old_ra_info = cur;
335 cur += 6;
336
337 /* MS Radio Access Capability 10.5.5.12a */
338
339 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
340
341 switch (mi_type) {
342 case GSM_MI_TYPE_IMSI:
343 /* Try to find MM context based on IMSI */
344 ctx = sgsn_mm_ctx_by_imsi(mi_string);
345 if (!ctx) {
346#if 0
347 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
348#else
349 /* As a temorary hack, we simply assume that the IMSI exists */
350 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
351 if (!ctx)
352 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL);
353 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
354#endif
355 }
356 /* FIXME: Start some timer */
357 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200358 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800359 break;
360 case GSM_MI_TYPE_TMSI:
361 tmsi = strtoul(mi_string, NULL, 10);
362 /* Try to find MM context based on P-TMSI */
363 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
364 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200365 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800366 /* FIXME: Start some timer */
367 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200368 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800369 }
370 break;
371 default:
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200372 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800373 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200374 /* Update MM Context with currient RA and Cell ID */
375 ctx->ra = ra_id;
376 ctx->cell_id = cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800377
378 /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */
379 /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */
380
381 DEBUGPC(DMM, "\n");
382
383 return ctx ? gsm48_gmm_authorize(ctx, msg) : 0;
384
385err_inval:
386 DEBUGPC(DMM, "\n");
387 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG);
388}
389
390/* Chapter 9.4.15: Routing area update accept */
391static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg)
392{
393 struct msgb *msg = gsm48_msgb_alloc();
394 struct gsm48_hdr *gh;
395 struct gsm48_ra_upd_ack *rua;
Harald Welte11d7c102010-05-02 11:54:55 +0200396 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800397
398 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
399
Harald Welte11d7c102010-05-02 11:54:55 +0200400 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800401
402 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
403 gh->proto_discr = GSM48_PDISC_MM_GPRS;
404 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
405
406 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
407 rua->force_stby = 0; /* not indicated */
408 rua->upd_result = 0; /* RA updated */
409 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200410
Harald Welteba021102010-05-02 21:36:36 +0200411 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200412 gsm48_construct_ra(rua->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800413
414 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
415 return gsm48_gmm_sendmsg(msg, 0);
416}
417
418/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200419static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800420{
421 struct msgb *msg = gsm48_msgb_alloc();
422 struct gsm48_hdr *gh;
423
424 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
425
Harald Welte11d7c102010-05-02 11:54:55 +0200426 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800427
428 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
429 gh->proto_discr = GSM48_PDISC_MM_GPRS;
430 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
431 gh->data[0] = cause;
432 gh->data[1] = 0; /* ? */
433
434 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
435 return gsm48_gmm_sendmsg(msg, 0);
436}
437
438/* Chapter 9.4.14: Routing area update request */
439static int gsm48_rx_gmm_ra_upd_req(struct msgb *msg)
440{
Harald Welte943c5bc2010-04-30 16:33:12 +0200441 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800442 struct sgsn_mm_ctx *mmctx;
Harald Welteeaa614c2010-05-02 11:26:34 +0200443 uint8_t *cur = gh->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800444 struct gprs_ra_id old_ra_id;
Harald Welteeaa614c2010-05-02 11:26:34 +0200445 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800446
447 /* Update Type 10.5.5.18 */
448 upd_type = *cur++ & 0x0f;
449
450 DEBUGP(DMM, "GMM RA UPDATE REQUEST type=\"%s\" ", upd_name(upd_type));
451
452 /* Old routing area identification 10.5.5.15 */
453 gsm48_parse_ra(&old_ra_id, cur);
454 cur += 6;
455
456 /* MS Radio Access Capability 10.5.5.12a */
457
458 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
459 * DRX parameter, MS network capability */
460
461 switch (upd_type) {
462 case GPRS_UPD_T_RA_LA:
463 case GPRS_UPD_T_RA_LA_IMSI_ATT:
464 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
465 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
466 break;
467 case GPRS_UPD_T_RA:
468 case GPRS_UPD_T_PERIODIC:
469 break;
470 }
471
472 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte943c5bc2010-04-30 16:33:12 +0200473 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800474 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
475 /* The MS has to perform GPRS attach */
476 DEBUGPC(DMM, " REJECT\n");
477 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED);
478 }
479
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200480 /* Update the MM context with the new RA-ID */
481 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
482 /* Update the MM context with the new TLLI */
483 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800484 /* FIXME: Update the MM context with the MS radio acc capabilities */
485 /* FIXME: Update the MM context with the MS network capabilities */
486
487 DEBUGPC(DMM, " ACCEPT\n");
488 return gsm48_tx_gmm_ra_upd_ack(msg);
489}
490
491static int gsm48_rx_gmm_status(struct msgb *msg)
492{
493 struct gsm48_hdr *gh = msgb_l3(msg);
494
495 DEBUGP(DMM, "GPRS MM STATUS (cause: %s)\n",
496 get_value_string(gmm_cause_names, gh->data[0]));
497
498 return 0;
499}
500
501/* GPRS Mobility Management */
502static int gsm0408_rcv_gmm(struct msgb *msg)
503{
Harald Welte943c5bc2010-04-30 16:33:12 +0200504 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800505 int rc;
506
507 switch (gh->msg_type) {
508 case GSM48_MT_GMM_RA_UPD_REQ:
509 rc = gsm48_rx_gmm_ra_upd_req(msg);
510 break;
511 case GSM48_MT_GMM_ATTACH_REQ:
512 rc = gsm48_rx_gmm_att_req(msg);
513 break;
514 case GSM48_MT_GMM_ID_RESP:
515 rc = gsm48_rx_gmm_id_resp(msg);
516 break;
517 case GSM48_MT_GMM_STATUS:
518 rc = gsm48_rx_gmm_status(msg);
519 break;
520 case GSM48_MT_GMM_RA_UPD_COMPL:
521 /* only in case SGSN offered new P-TMSI */
522 case GSM48_MT_GMM_ATTACH_COMPL:
523 /* only in case SGSN offered new P-TMSI */
524 case GSM48_MT_GMM_DETACH_REQ:
525 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
526 case GSM48_MT_GMM_AUTH_CIPH_RESP:
527 DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n",
528 gh->msg_type);
529 break;
530 default:
531 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
532 gh->msg_type);
533 break;
534 }
535
536 return rc;
537}
538
539/* Section 9.5.2: Ativate PDP Context Accept */
540static int gsm48_tx_gsm_act_pdp_acc(struct msgb *old_msg, struct gsm48_act_pdp_ctx_req *req)
541{
Harald Welte943c5bc2010-04-30 16:33:12 +0200542 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800543 struct msgb *msg = gsm48_msgb_alloc();
544 struct gsm48_act_pdp_ctx_ack *act_ack;
545 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200546 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800547
548 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
549
Harald Welte11d7c102010-05-02 11:54:55 +0200550 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800551
552 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
553 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
554 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
555 act_ack = (struct gsm48_act_pdp_ctx_ack *)
556 msgb_put(msg, sizeof(*act_ack));
557 act_ack->llc_sapi = req->req_llc_sapi;
Harald Welte0c3eae02010-05-02 21:07:14 +0200558 memcpy(act_ack->qos_lv, req->data, sizeof(act_ack->qos_lv));
Harald Welte9b455bf2010-03-14 15:45:01 +0800559 //act_ack->radio_prio = 4;
560
561 return gsm48_gmm_sendmsg(msg, 0);
562}
563
564/* Section 9.5.9: Deactivate PDP Context Accept */
565static int gsm48_tx_gsm_deact_pdp_acc(struct msgb *old_msg)
566{
Harald Welte943c5bc2010-04-30 16:33:12 +0200567 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800568 struct msgb *msg = gsm48_msgb_alloc();
569 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200570 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800571
572 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
573
Harald Welte11d7c102010-05-02 11:54:55 +0200574 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800575
576 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
577 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
578 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
579
580 return gsm48_gmm_sendmsg(msg, 0);
581}
582
583/* Section 9.5.1: Activate PDP Context Request */
584static int gsm48_rx_gsm_act_pdp_req(struct msgb *msg)
585{
Harald Welte943c5bc2010-04-30 16:33:12 +0200586 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800587 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteeaa614c2010-05-02 11:26:34 +0200588 uint8_t *pdp_addr_lv = act_req->data;
Harald Welte0c3eae02010-05-02 21:07:14 +0200589 uint8_t req_qos_len, req_pdpa_len;
590 uint8_t *req_qos, *req_pdpa;
591 struct tlv_parsed tp;
Harald Welte9b455bf2010-03-14 15:45:01 +0800592
Harald Welte0c3eae02010-05-02 21:07:14 +0200593 DEBUGP(DMM, "ACTIVATE PDP CONTEXT REQ: ");
594 req_qos_len = act_req->data[0];
595 req_qos = act_req->data + 1; /* 10.5.6.5 */
596 req_pdpa_len = act_req->data[1 + req_qos_len];
597 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800598
Harald Welte0c3eae02010-05-02 21:07:14 +0200599 switch (req_pdpa[0] & 0xf) {
600 case 0x0:
601 DEBUGPC(DMM, "ETSI ");
602 break;
603 case 0x1:
604 DEBUGPC(DMM, "IETF ");
605 break;
606 case 0xf:
607 DEBUGPC(DMM, "Empty ");
608 break;
609 }
610
611 switch (req_pdpa[1]) {
612 case 0x21:
613 DEBUGPC(DMM, "IPv4 ");
614 if (req_pdpa_len >= 6) {
615 struct in_addr ia;
616 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
617 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
618 }
619 break;
620 case 0x57:
621 DEBUGPC(DMM, "IPv6 ");
622 if (req_pdpa_len >= 18) {
623 /* FIXME: print IPv6 address */
624 }
625 break;
626 default:
627 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
628 break;
629 }
630
631 /* FIXME: parse TLV for AP name and protocol config options */
632 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
633 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_PROTO_CONF_OPT)) {}
Harald Welte9b455bf2010-03-14 15:45:01 +0800634
635 return gsm48_tx_gsm_act_pdp_acc(msg, act_req);
636}
637
638/* Section 9.5.8: Deactivate PDP Context Request */
639static int gsm48_rx_gsm_deact_pdp_req(struct msgb *msg)
640{
Harald Welte943c5bc2010-04-30 16:33:12 +0200641 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800642
643 DEBUGP(DMM, "DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
644 get_value_string(gsm_cause_names, gh->data[0]));
645
646 return gsm48_tx_gsm_deact_pdp_acc(msg);
647}
648
649static int gsm48_rx_gsm_status(struct msgb *msg)
650{
651 struct gsm48_hdr *gh = msgb_l3(msg);
652
653 DEBUGP(DMM, "GPRS SM STATUS (cause: %s)\n",
654 get_value_string(gsm_cause_names, gh->data[0]));
655
656 return 0;
657}
658
659/* GPRS Session Management */
660static int gsm0408_rcv_gsm(struct msgb *msg)
661{
Harald Welte943c5bc2010-04-30 16:33:12 +0200662 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800663 int rc;
664
665 switch (gh->msg_type) {
666 case GSM48_MT_GSM_ACT_PDP_REQ:
667 rc = gsm48_rx_gsm_act_pdp_req(msg);
668 break;
669 case GSM48_MT_GSM_DEACT_PDP_REQ:
670 rc = gsm48_rx_gsm_deact_pdp_req(msg);
671 case GSM48_MT_GSM_STATUS:
672 rc = gsm48_rx_gsm_status(msg);
673 break;
674 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
675 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
676 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
677 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
678 gh->msg_type);
679 break;
680 default:
681 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
682 gh->msg_type);
683 break;
684
685 }
686
687 return rc;
688}
689
690/* Main entry point for incoming 04.08 GPRS messages */
691int gsm0408_gprs_rcvmsg(struct msgb *msg)
692{
Harald Welte943c5bc2010-04-30 16:33:12 +0200693 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200694 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte9b455bf2010-03-14 15:45:01 +0800695 int rc = -EINVAL;
696
697 switch (pdisc) {
698 case GSM48_PDISC_MM_GPRS:
699 rc = gsm0408_rcv_gmm(msg);
700 break;
701 case GSM48_PDISC_SM_GPRS:
702 rc = gsm0408_rcv_gsm(msg);
703 break;
704 default:
705 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
706 pdisc);
707 break;
708 }
709
710 return rc;
711}