blob: 4a42113f0bca7c5ba8b156edd47076f89814b5d7 [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>
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>
39
Harald Welte9b455bf2010-03-14 15:45:01 +080040#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080042#include <openbsc/gsm_subscriber.h>
43#include <openbsc/gsm_04_08.h>
44#include <openbsc/gsm_04_08_gprs.h>
45#include <openbsc/paging.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080046#include <openbsc/transaction.h>
Harald Welte11d7c102010-05-02 11:54:55 +020047#include <openbsc/gprs_bssgp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080048#include <openbsc/gprs_llc.h>
49#include <openbsc/gprs_sgsn.h>
50
51/* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */
52struct value_string gmm_cause_names[] = {
53 /* FIXME */
54 { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
55 { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
56 { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
57 "Message type non-existant or not implemented" },
58 { GMM_CAUSE_MSGT_INCOMP_P_STATE,
59 "Message type not compatible with protocol state" },
60 { GMM_CAUSE_IE_NOTEXIST_NOTIMPL,
61 "Information element non-existent or not implemented" },
62 { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" },
63 { GMM_CAUSE_MSG_INCOMP_P_STATE,
64 "Message not compatible with protocol state " },
65 { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
66 { 0, NULL }
67};
68
69/* 10.5.6.6 SM Cause / Table 10.5.157 */
70struct value_string gsm_cause_names[] = {
71 { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" },
72 { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" },
73 { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" },
74 { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" },
75 { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" },
76 { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" },
77 { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" },
78 { GSM_CAUSE_REQ_SERV_OPT_NOTSUB,
79 "Requested service option not subscribed" },
80 { GSM_CAUSE_SERV_OPT_TEMP_OOO,
81 "Service option temporarily out of order" },
82 { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" },
83 { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" },
84 { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" },
85 { GSM_CAUSE_NET_FAIL, "Network Failure" },
86 { GSM_CAUSE_REACT_RQD, "Reactivation required" },
87 { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " },
88 { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" },
89 { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" },
90 { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" },
91 { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL,
92 "Message type non-existant or not implemented" },
93 { GSM_CAUSE_MSGT_INCOMP_P_STATE,
94 "Message type not compatible with protocol state" },
95 { GSM_CAUSE_IE_NOTEXIST_NOTIMPL,
96 "Information element non-existent or not implemented" },
97 { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" },
98 { GSM_CAUSE_MSG_INCOMP_P_STATE,
99 "Message not compatible with protocol state " },
100 { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" },
101 { 0, NULL }
102};
103
Harald Welteeaa614c2010-05-02 11:26:34 +0200104static const char *att_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800105{
106 switch (type) {
107 case GPRS_ATT_T_ATTACH:
108 return "GPRS attach";
109 case GPRS_ATT_T_ATT_WHILE_IMSI:
110 return "GPRS attach while IMSI attached";
111 case GPRS_ATT_T_COMBINED:
112 return "Combined GPRS/IMSI attach";
113 default:
114 return "unknown";
115 }
116}
117
Harald Welteeaa614c2010-05-02 11:26:34 +0200118static const char *upd_name(uint8_t type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800119{
120 switch (type) {
121 case GPRS_UPD_T_RA:
122 return "RA updating";
123 case GPRS_UPD_T_RA_LA:
124 return "combined RA/LA updating";
125 case GPRS_UPD_T_RA_LA_IMSI_ATT:
126 return "combined RA/LA updating + IMSI attach";
127 case GPRS_UPD_T_PERIODIC:
128 return "periodic updating";
129 }
130 return "unknown";
131}
132
Harald Welte9b455bf2010-03-14 15:45:01 +0800133/* Send a message through the underlying layer */
134static int gsm48_gmm_sendmsg(struct msgb *msg, int command)
135{
Harald Welte11d7c102010-05-02 11:54:55 +0200136 /* caller needs to provide TLLI, BVCI and NSEI */
Harald Welte9b455bf2010-03-14 15:45:01 +0800137 return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command);
138}
139
Harald Welte11d7c102010-05-02 11:54:55 +0200140/* copy identifiers from old message to new message, this
141 * is required so lower layers can route it correctly */
142static void gmm_copy_id(struct msgb *msg, const struct msgb *old)
143{
144 msgb_tlli(msg) = msgb_tlli(old);
145 msgb_bvci(msg) = msgb_bvci(old);
146 msgb_nsei(msg) = msgb_nsei(old);
147}
148
Harald Welte86fda902010-05-03 10:54:35 +0200149static struct gsm48_qos default_qos = {
150 .delay_class = 4, /* best effort */
151 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
152 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
153 .preced_class = GSM48_QOS_PC_NORMAL,
154 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
155 .traf_class = GSM48_QOS_TC_INTERACTIVE,
156 .deliv_order = GSM48_QOS_DO_UNORDERED,
157 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
158 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
159 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
160 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
161 .resid_ber = GSM48_QOS_RBER_5e_2,
162 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
163 .handling_prio = 3,
164 .xfer_delay = 0x10, /* 200ms */
165 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
166 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
167 .sig_ind = 0, /* not optimised for signalling */
168 .max_bitrate_down_ext = 0, /* use octet 9 */
169 .guar_bitrate_down_ext = 0, /* use octet 13 */
170};
171
Harald Welte9b455bf2010-03-14 15:45:01 +0800172/* Chapter 9.4.2: Attach accept */
173static int gsm48_tx_gmm_att_ack(struct msgb *old_msg)
174{
175 struct msgb *msg = gsm48_msgb_alloc();
176 struct gsm48_hdr *gh;
177 struct gsm48_attach_ack *aa;
Harald Welte11d7c102010-05-02 11:54:55 +0200178 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800179
180 DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n");
181
Harald Welte11d7c102010-05-02 11:54:55 +0200182 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800183
184 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
185 gh->proto_discr = GSM48_PDISC_MM_GPRS;
186 gh->msg_type = GSM48_MT_GMM_ATTACH_ACK;
187
188 aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa));
189 aa->force_stby = 0; /* not indicated */
190 aa->att_result = 1; /* GPRS only */
191 aa->ra_upd_timer = GPRS_TMR_MINUTE | 10;
192 aa->radio_prio = 4; /* lowest */
Harald Welteba021102010-05-02 21:36:36 +0200193 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200194 gsm48_construct_ra(aa->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800195
196 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
197 return gsm48_gmm_sendmsg(msg, 0);
198}
199
200/* Chapter 9.4.5: Attach reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200201static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, uint8_t gmm_cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800202{
203 struct msgb *msg = gsm48_msgb_alloc();
204 struct gsm48_hdr *gh;
205
206 DEBUGP(DMM, "<- GPRS ATTACH REJECT\n");
207
Harald Welte11d7c102010-05-02 11:54:55 +0200208 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800209
210 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
211 gh->proto_discr = GSM48_PDISC_MM_GPRS;
212 gh->msg_type = GSM48_MT_GMM_ATTACH_REJ;
213 gh->data[0] = gmm_cause;
214
215 return gsm48_gmm_sendmsg(msg, 0);
216}
217
218/* Transmit Chapter 9.4.12 Identity Request */
Harald Welteeaa614c2010-05-02 11:26:34 +0200219static int gsm48_tx_gmm_id_req(struct msgb *old_msg, uint8_t id_type)
Harald Welte9b455bf2010-03-14 15:45:01 +0800220{
221 struct msgb *msg = gsm48_msgb_alloc();
222 struct gsm48_hdr *gh;
223
224 DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type);
225
Harald Welte11d7c102010-05-02 11:54:55 +0200226 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800227
228 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
229 gh->proto_discr = GSM48_PDISC_MM_GPRS;
230 gh->msg_type = GSM48_MT_GMM_ID_REQ;
231 /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */
232 gh->data[0] = id_type & 0xf;
233
234 return gsm48_gmm_sendmsg(msg, 0);
235}
236
237/* Check if we can already authorize a subscriber */
238static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg)
239{
240 if (strlen(ctx->imei) && strlen(ctx->imsi)) {
241 ctx->mm_state = GMM_REGISTERED_NORMAL;
242 return gsm48_tx_gmm_att_ack(msg);
243 }
244 if (!strlen(ctx->imei))
245 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI);
246
247 if (!strlen(ctx->imsi))
248 return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI);
249
250 return 0;
251}
252
253/* Parse Chapter 9.4.13 Identity Response */
254static int gsm48_rx_gmm_id_resp(struct msgb *msg)
255{
Harald Welte943c5bc2010-04-30 16:33:12 +0200256 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200257 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Harald Welte9b455bf2010-03-14 15:45:01 +0800258 char mi_string[GSM48_MI_SIZE];
259 struct gprs_ra_id ra_id;
260 struct sgsn_mm_ctx *ctx;
261
262 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
263 DEBUGP(DMM, "GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ",
264 mi_type, mi_string);
265
Harald Welte11d7c102010-05-02 11:54:55 +0200266 bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte943c5bc2010-04-30 16:33:12 +0200267 ctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800268 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200269 DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800270 return -EINVAL;
271 }
272
273 switch (mi_type) {
274 case GSM_MI_TYPE_IMSI:
275 /* we already have a mm context with current TLLI, but no
276 * P-TMSI / IMSI yet. What we now need to do is to fill
277 * this initial context with data from the HLR */
278 strncpy(ctx->imsi, mi_string, sizeof(ctx->imei));
279 break;
280 case GSM_MI_TYPE_IMEI:
281 strncpy(ctx->imei, mi_string, sizeof(ctx->imei));
282 break;
283 case GSM_MI_TYPE_IMEISV:
284 break;
285 }
286
287 DEBUGPC(DMM, "\n");
288 /* Check if we can let the mobile station enter */
289 return gsm48_gmm_authorize(ctx, msg);
290}
291
292static void attach_rej_cb(void *data)
293{
294 struct sgsn_mm_ctx *ctx = data;
295
296 /* FIXME: determine through which BTS/TRX to send this */
297 //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED);
298 ctx->mm_state = GMM_DEREGISTERED;
299 /* FIXME: release the context */
300}
301
302static void schedule_reject(struct sgsn_mm_ctx *ctx)
303{
304 ctx->T = 3370;
305 ctx->timer.cb = attach_rej_cb;
306 ctx->timer.data = ctx;
307 bsc_schedule_timer(&ctx->timer, 6, 0);
308}
309
310/* Section 9.4.1 Attach request */
311static int gsm48_rx_gmm_att_req(struct msgb *msg)
312{
Harald Welte943c5bc2010-04-30 16:33:12 +0200313 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200314 uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info;
315 uint8_t msnc_len, att_type, mi_len, mi_type;
316 uint16_t drx_par;
317 uint32_t tmsi;
Harald Welte9b455bf2010-03-14 15:45:01 +0800318 char mi_string[GSM48_MI_SIZE];
319 struct gprs_ra_id ra_id;
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200320 uint16_t cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800321 struct sgsn_mm_ctx *ctx;
322
323 DEBUGP(DMM, "GMM ATTACH REQUEST ");
324
325 /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either
326 * with a foreign TLLI (P-TMSI that was allocated to the MS before),
327 * or with random TLLI. */
328
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200329 cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg));
Harald Welte9b455bf2010-03-14 15:45:01 +0800330
331 /* MS network capability 10.5.5.12 */
332 msnc_len = *cur++;
333 msnc = cur;
334 if (msnc_len > 2)
335 goto err_inval;
336 cur += msnc_len;
337
338 /* aTTACH Type 10.5.5.2 */
339 att_type = *cur++ & 0x0f;
340
341 /* DRX parameter 10.5.5.6 */
342 drx_par = *cur++;
343 drx_par |= *cur++ << 8;
344
345 /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */
346 mi_len = *cur++;
347 mi = cur;
348 if (mi_len > 8)
349 goto err_inval;
350 mi_type = *mi & GSM_MI_TYPE_MASK;
351 cur += mi_len;
352
353 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
354
355 DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, att_name(att_type));
356
357 /* Old routing area identification 10.5.5.15 */
358 old_ra_info = cur;
359 cur += 6;
360
361 /* MS Radio Access Capability 10.5.5.12a */
362
363 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */
364
365 switch (mi_type) {
366 case GSM_MI_TYPE_IMSI:
367 /* Try to find MM context based on IMSI */
368 ctx = sgsn_mm_ctx_by_imsi(mi_string);
369 if (!ctx) {
370#if 0
371 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN);
372#else
373 /* As a temorary hack, we simply assume that the IMSI exists */
374 ctx = sgsn_mm_ctx_alloc(0, &ra_id);
375 if (!ctx)
376 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL);
377 strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi));
378#endif
379 }
380 /* FIXME: Start some timer */
381 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200382 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800383 break;
384 case GSM_MI_TYPE_TMSI:
385 tmsi = strtoul(mi_string, NULL, 10);
386 /* Try to find MM context based on P-TMSI */
387 ctx = sgsn_mm_ctx_by_ptmsi(tmsi);
388 if (!ctx) {
Harald Welte943c5bc2010-04-30 16:33:12 +0200389 ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800390 /* FIXME: Start some timer */
391 ctx->mm_state = GMM_COMMON_PROC_INIT;
Harald Welte943c5bc2010-04-30 16:33:12 +0200392 ctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800393 }
394 break;
395 default:
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200396 return 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800397 }
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200398 /* Update MM Context with currient RA and Cell ID */
399 ctx->ra = ra_id;
400 ctx->cell_id = cid;
Harald Welte9b455bf2010-03-14 15:45:01 +0800401
402 /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */
403 /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */
404
405 DEBUGPC(DMM, "\n");
406
407 return ctx ? gsm48_gmm_authorize(ctx, msg) : 0;
408
409err_inval:
410 DEBUGPC(DMM, "\n");
411 return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG);
412}
413
414/* Chapter 9.4.15: Routing area update accept */
415static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg)
416{
417 struct msgb *msg = gsm48_msgb_alloc();
418 struct gsm48_hdr *gh;
419 struct gsm48_ra_upd_ack *rua;
Harald Welte11d7c102010-05-02 11:54:55 +0200420 struct gprs_ra_id ra_id;
Harald Welte9b455bf2010-03-14 15:45:01 +0800421
422 DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n");
423
Harald Welte11d7c102010-05-02 11:54:55 +0200424 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800425
426 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
427 gh->proto_discr = GSM48_PDISC_MM_GPRS;
428 gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK;
429
430 rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua));
431 rua->force_stby = 0; /* not indicated */
432 rua->upd_result = 0; /* RA updated */
433 rua->ra_upd_timer = GPRS_TMR_MINUTE | 10;
Harald Welte11d7c102010-05-02 11:54:55 +0200434
Harald Welteba021102010-05-02 21:36:36 +0200435 bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg));
Harald Welte11d7c102010-05-02 11:54:55 +0200436 gsm48_construct_ra(rua->ra_id.digits, &ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800437
438 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
439 return gsm48_gmm_sendmsg(msg, 0);
440}
441
442/* Chapter 9.4.17: Routing area update reject */
Harald Welteeaa614c2010-05-02 11:26:34 +0200443static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause)
Harald Welte9b455bf2010-03-14 15:45:01 +0800444{
445 struct msgb *msg = gsm48_msgb_alloc();
446 struct gsm48_hdr *gh;
447
448 DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n");
449
Harald Welte11d7c102010-05-02 11:54:55 +0200450 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800451
452 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2);
453 gh->proto_discr = GSM48_PDISC_MM_GPRS;
454 gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ;
455 gh->data[0] = cause;
456 gh->data[1] = 0; /* ? */
457
458 /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */
459 return gsm48_gmm_sendmsg(msg, 0);
460}
461
462/* Chapter 9.4.14: Routing area update request */
463static int gsm48_rx_gmm_ra_upd_req(struct msgb *msg)
464{
Harald Welte943c5bc2010-04-30 16:33:12 +0200465 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800466 struct sgsn_mm_ctx *mmctx;
Harald Welteeaa614c2010-05-02 11:26:34 +0200467 uint8_t *cur = gh->data;
Harald Welte9b455bf2010-03-14 15:45:01 +0800468 struct gprs_ra_id old_ra_id;
Harald Welteeaa614c2010-05-02 11:26:34 +0200469 uint8_t upd_type;
Harald Welte9b455bf2010-03-14 15:45:01 +0800470
471 /* Update Type 10.5.5.18 */
472 upd_type = *cur++ & 0x0f;
473
474 DEBUGP(DMM, "GMM RA UPDATE REQUEST type=\"%s\" ", upd_name(upd_type));
475
476 /* Old routing area identification 10.5.5.15 */
477 gsm48_parse_ra(&old_ra_id, cur);
478 cur += 6;
479
480 /* MS Radio Access Capability 10.5.5.12a */
481
482 /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status,
483 * DRX parameter, MS network capability */
484
485 switch (upd_type) {
486 case GPRS_UPD_T_RA_LA:
487 case GPRS_UPD_T_RA_LA_IMSI_ATT:
488 DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n");
489 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC);
490 break;
491 case GPRS_UPD_T_RA:
492 case GPRS_UPD_T_PERIODIC:
493 break;
494 }
495
496 /* Look-up the MM context based on old RA-ID and TLLI */
Harald Welte943c5bc2010-04-30 16:33:12 +0200497 mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id);
Harald Welte9b455bf2010-03-14 15:45:01 +0800498 if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) {
499 /* The MS has to perform GPRS attach */
500 DEBUGPC(DMM, " REJECT\n");
501 return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED);
502 }
503
Harald Welte9f1f3ad2010-05-02 12:56:57 +0200504 /* Update the MM context with the new RA-ID */
505 bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg));
506 /* Update the MM context with the new TLLI */
507 mmctx->tlli = msgb_tlli(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800508 /* FIXME: Update the MM context with the MS radio acc capabilities */
509 /* FIXME: Update the MM context with the MS network capabilities */
510
511 DEBUGPC(DMM, " ACCEPT\n");
512 return gsm48_tx_gmm_ra_upd_ack(msg);
513}
514
515static int gsm48_rx_gmm_status(struct msgb *msg)
516{
517 struct gsm48_hdr *gh = msgb_l3(msg);
518
519 DEBUGP(DMM, "GPRS MM STATUS (cause: %s)\n",
520 get_value_string(gmm_cause_names, gh->data[0]));
521
522 return 0;
523}
524
525/* GPRS Mobility Management */
526static int gsm0408_rcv_gmm(struct msgb *msg)
527{
Harald Welte943c5bc2010-04-30 16:33:12 +0200528 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800529 int rc;
530
531 switch (gh->msg_type) {
532 case GSM48_MT_GMM_RA_UPD_REQ:
533 rc = gsm48_rx_gmm_ra_upd_req(msg);
534 break;
535 case GSM48_MT_GMM_ATTACH_REQ:
536 rc = gsm48_rx_gmm_att_req(msg);
537 break;
538 case GSM48_MT_GMM_ID_RESP:
539 rc = gsm48_rx_gmm_id_resp(msg);
540 break;
541 case GSM48_MT_GMM_STATUS:
542 rc = gsm48_rx_gmm_status(msg);
543 break;
544 case GSM48_MT_GMM_RA_UPD_COMPL:
545 /* only in case SGSN offered new P-TMSI */
546 case GSM48_MT_GMM_ATTACH_COMPL:
547 /* only in case SGSN offered new P-TMSI */
548 case GSM48_MT_GMM_DETACH_REQ:
549 case GSM48_MT_GMM_PTMSI_REALL_COMPL:
550 case GSM48_MT_GMM_AUTH_CIPH_RESP:
551 DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n",
552 gh->msg_type);
553 break;
554 default:
555 DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n",
556 gh->msg_type);
557 break;
558 }
559
560 return rc;
561}
562
Harald Welte421cba42010-05-02 23:11:50 +0200563static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
564{
565 uint8_t v[6];
566
567 v[0] = PDP_TYPE_ORG_IETF;
568 v[1] = PDP_TYPE_N_IETF_IPv4;
569 *(uint32_t *)(v+2) = htonl(ipaddr);
570
571 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
572}
573
574static void msgb_put_pdp_addr_ppp(struct msgb *msg)
575{
576 uint8_t v[2];
577
578 v[0] = PDP_TYPE_ORG_ETSI;
579 v[1] = PDP_TYPE_N_ETSI_PPP;
580
581 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
582}
583
Harald Welte9b455bf2010-03-14 15:45:01 +0800584/* Section 9.5.2: Ativate PDP Context Accept */
585static int gsm48_tx_gsm_act_pdp_acc(struct msgb *old_msg, struct gsm48_act_pdp_ctx_req *req)
586{
Harald Welte943c5bc2010-04-30 16:33:12 +0200587 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800588 struct msgb *msg = gsm48_msgb_alloc();
589 struct gsm48_act_pdp_ctx_ack *act_ack;
590 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200591 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800592
593 DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n");
594
Harald Welte11d7c102010-05-02 11:54:55 +0200595 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800596
597 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
598 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
599 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
Harald Welte421cba42010-05-02 23:11:50 +0200600
601 /* Negotiated LLC SAPI */
602 msgb_v_put(msg, req->req_llc_sapi);
603 /* copy QoS parameters from original request */
Harald Welte86fda902010-05-03 10:54:35 +0200604 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
Harald Welte421cba42010-05-02 23:11:50 +0200605 /* Radio priority 10.5.7.2 */
606 msgb_v_put(msg, 4);
607 /* PDP address */
608 msgb_put_pdp_addr_ipv4(msg, 0x01020304);
609 /* Optional: Protocol configuration options */
610 /* Optional: Packet Flow Identifier */
Harald Welte9b455bf2010-03-14 15:45:01 +0800611
612 return gsm48_gmm_sendmsg(msg, 0);
613}
614
615/* Section 9.5.9: Deactivate PDP Context Accept */
616static int gsm48_tx_gsm_deact_pdp_acc(struct msgb *old_msg)
617{
Harald Welte943c5bc2010-04-30 16:33:12 +0200618 struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800619 struct msgb *msg = gsm48_msgb_alloc();
620 struct gsm48_hdr *gh;
Harald Welteeaa614c2010-05-02 11:26:34 +0200621 uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */
Harald Welte9b455bf2010-03-14 15:45:01 +0800622
623 DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n");
624
Harald Welte11d7c102010-05-02 11:54:55 +0200625 gmm_copy_id(msg, old_msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800626
627 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
628 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
629 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
630
631 return gsm48_gmm_sendmsg(msg, 0);
632}
633
634/* Section 9.5.1: Activate PDP Context Request */
635static int gsm48_rx_gsm_act_pdp_req(struct msgb *msg)
636{
Harald Welte943c5bc2010-04-30 16:33:12 +0200637 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800638 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
Harald Welteeaa614c2010-05-02 11:26:34 +0200639 uint8_t *pdp_addr_lv = act_req->data;
Harald Welte0c3eae02010-05-02 21:07:14 +0200640 uint8_t req_qos_len, req_pdpa_len;
641 uint8_t *req_qos, *req_pdpa;
642 struct tlv_parsed tp;
Harald Welte9b455bf2010-03-14 15:45:01 +0800643
Harald Welte0c3eae02010-05-02 21:07:14 +0200644 DEBUGP(DMM, "ACTIVATE PDP CONTEXT REQ: ");
645 req_qos_len = act_req->data[0];
646 req_qos = act_req->data + 1; /* 10.5.6.5 */
647 req_pdpa_len = act_req->data[1 + req_qos_len];
648 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800649
Harald Welte0c3eae02010-05-02 21:07:14 +0200650 switch (req_pdpa[0] & 0xf) {
651 case 0x0:
652 DEBUGPC(DMM, "ETSI ");
653 break;
654 case 0x1:
655 DEBUGPC(DMM, "IETF ");
656 break;
657 case 0xf:
658 DEBUGPC(DMM, "Empty ");
659 break;
660 }
661
662 switch (req_pdpa[1]) {
663 case 0x21:
664 DEBUGPC(DMM, "IPv4 ");
665 if (req_pdpa_len >= 6) {
666 struct in_addr ia;
667 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
668 DEBUGPC(DMM, "%s ", inet_ntoa(ia));
669 }
670 break;
671 case 0x57:
672 DEBUGPC(DMM, "IPv6 ");
673 if (req_pdpa_len >= 18) {
674 /* FIXME: print IPv6 address */
675 }
676 break;
677 default:
678 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
679 break;
680 }
681
682 /* FIXME: parse TLV for AP name and protocol config options */
683 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {}
684 if (TLVP_PRESENT(&tp, GSM48_IE_GSM_PROTO_CONF_OPT)) {}
Harald Welte9b455bf2010-03-14 15:45:01 +0800685
686 return gsm48_tx_gsm_act_pdp_acc(msg, act_req);
687}
688
689/* Section 9.5.8: Deactivate PDP Context Request */
690static int gsm48_rx_gsm_deact_pdp_req(struct msgb *msg)
691{
Harald Welte943c5bc2010-04-30 16:33:12 +0200692 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800693
694 DEBUGP(DMM, "DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
695 get_value_string(gsm_cause_names, gh->data[0]));
696
697 return gsm48_tx_gsm_deact_pdp_acc(msg);
698}
699
700static int gsm48_rx_gsm_status(struct msgb *msg)
701{
702 struct gsm48_hdr *gh = msgb_l3(msg);
703
704 DEBUGP(DMM, "GPRS SM STATUS (cause: %s)\n",
705 get_value_string(gsm_cause_names, gh->data[0]));
706
707 return 0;
708}
709
710/* GPRS Session Management */
711static int gsm0408_rcv_gsm(struct msgb *msg)
712{
Harald Welte943c5bc2010-04-30 16:33:12 +0200713 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800714 int rc;
715
716 switch (gh->msg_type) {
717 case GSM48_MT_GSM_ACT_PDP_REQ:
718 rc = gsm48_rx_gsm_act_pdp_req(msg);
719 break;
720 case GSM48_MT_GSM_DEACT_PDP_REQ:
721 rc = gsm48_rx_gsm_deact_pdp_req(msg);
722 case GSM48_MT_GSM_STATUS:
723 rc = gsm48_rx_gsm_status(msg);
724 break;
725 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
726 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
727 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
728 DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n",
729 gh->msg_type);
730 break;
731 default:
732 DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n",
733 gh->msg_type);
734 break;
735
736 }
737
738 return rc;
739}
740
741/* Main entry point for incoming 04.08 GPRS messages */
742int gsm0408_gprs_rcvmsg(struct msgb *msg)
743{
Harald Welte943c5bc2010-04-30 16:33:12 +0200744 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
Harald Welteeaa614c2010-05-02 11:26:34 +0200745 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte9b455bf2010-03-14 15:45:01 +0800746 int rc = -EINVAL;
747
748 switch (pdisc) {
749 case GSM48_PDISC_MM_GPRS:
750 rc = gsm0408_rcv_gmm(msg);
751 break;
752 case GSM48_PDISC_SM_GPRS:
753 rc = gsm0408_rcv_gsm(msg);
754 break;
755 default:
756 DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n",
757 pdisc);
758 break;
759 }
760
761 return rc;
762}