Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | /* 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 | |
| 4 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 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> |
| 27 | #include <errno.h> |
| 28 | |
| 29 | #include <netinet/in.h> |
| 30 | |
| 31 | #include <openbsc/db.h> |
| 32 | #include <osmocore/msgb.h> |
| 33 | #include <osmocore/tlv.h> |
| 34 | #include <openbsc/debug.h> |
| 35 | #include <openbsc/gsm_data.h> |
| 36 | #include <osmocore/gsm_utils.h> |
| 37 | #include <openbsc/gsm_subscriber.h> |
| 38 | #include <openbsc/gsm_04_08.h> |
| 39 | #include <openbsc/gsm_04_08_gprs.h> |
| 40 | #include <openbsc/paging.h> |
| 41 | #include <osmocore/signal.h> |
| 42 | #include <osmocore/talloc.h> |
| 43 | #include <openbsc/transaction.h> |
| 44 | #include <openbsc/gprs_llc.h> |
| 45 | #include <openbsc/gprs_sgsn.h> |
| 46 | |
| 47 | /* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */ |
| 48 | struct value_string gmm_cause_names[] = { |
| 49 | /* FIXME */ |
| 50 | { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" }, |
| 51 | { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" }, |
| 52 | { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL, |
| 53 | "Message type non-existant or not implemented" }, |
| 54 | { GMM_CAUSE_MSGT_INCOMP_P_STATE, |
| 55 | "Message type not compatible with protocol state" }, |
| 56 | { GMM_CAUSE_IE_NOTEXIST_NOTIMPL, |
| 57 | "Information element non-existent or not implemented" }, |
| 58 | { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" }, |
| 59 | { GMM_CAUSE_MSG_INCOMP_P_STATE, |
| 60 | "Message not compatible with protocol state " }, |
| 61 | { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
| 62 | { 0, NULL } |
| 63 | }; |
| 64 | |
| 65 | /* 10.5.6.6 SM Cause / Table 10.5.157 */ |
| 66 | struct value_string gsm_cause_names[] = { |
| 67 | { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" }, |
| 68 | { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" }, |
| 69 | { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" }, |
| 70 | { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" }, |
| 71 | { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" }, |
| 72 | { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" }, |
| 73 | { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" }, |
| 74 | { GSM_CAUSE_REQ_SERV_OPT_NOTSUB, |
| 75 | "Requested service option not subscribed" }, |
| 76 | { GSM_CAUSE_SERV_OPT_TEMP_OOO, |
| 77 | "Service option temporarily out of order" }, |
| 78 | { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" }, |
| 79 | { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" }, |
| 80 | { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" }, |
| 81 | { GSM_CAUSE_NET_FAIL, "Network Failure" }, |
| 82 | { GSM_CAUSE_REACT_RQD, "Reactivation required" }, |
| 83 | { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " }, |
| 84 | { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" }, |
| 85 | { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" }, |
| 86 | { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" }, |
| 87 | { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL, |
| 88 | "Message type non-existant or not implemented" }, |
| 89 | { GSM_CAUSE_MSGT_INCOMP_P_STATE, |
| 90 | "Message type not compatible with protocol state" }, |
| 91 | { GSM_CAUSE_IE_NOTEXIST_NOTIMPL, |
| 92 | "Information element non-existent or not implemented" }, |
| 93 | { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" }, |
| 94 | { GSM_CAUSE_MSG_INCOMP_P_STATE, |
| 95 | "Message not compatible with protocol state " }, |
| 96 | { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
| 97 | { 0, NULL } |
| 98 | }; |
| 99 | |
| 100 | static const char *att_name(u_int8_t type) |
| 101 | { |
| 102 | switch (type) { |
| 103 | case GPRS_ATT_T_ATTACH: |
| 104 | return "GPRS attach"; |
| 105 | case GPRS_ATT_T_ATT_WHILE_IMSI: |
| 106 | return "GPRS attach while IMSI attached"; |
| 107 | case GPRS_ATT_T_COMBINED: |
| 108 | return "Combined GPRS/IMSI attach"; |
| 109 | default: |
| 110 | return "unknown"; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static const char *upd_name(u_int8_t type) |
| 115 | { |
| 116 | switch (type) { |
| 117 | case GPRS_UPD_T_RA: |
| 118 | return "RA updating"; |
| 119 | case GPRS_UPD_T_RA_LA: |
| 120 | return "combined RA/LA updating"; |
| 121 | case GPRS_UPD_T_RA_LA_IMSI_ATT: |
| 122 | return "combined RA/LA updating + IMSI attach"; |
| 123 | case GPRS_UPD_T_PERIODIC: |
| 124 | return "periodic updating"; |
| 125 | } |
| 126 | return "unknown"; |
| 127 | } |
| 128 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 129 | /* Send a message through the underlying layer */ |
| 130 | static int gsm48_gmm_sendmsg(struct msgb *msg, int command) |
| 131 | { |
| 132 | return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command); |
| 133 | } |
| 134 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 135 | /* Chapter 9.4.2: Attach accept */ |
| 136 | static int gsm48_tx_gmm_att_ack(struct msgb *old_msg) |
| 137 | { |
| 138 | struct msgb *msg = gsm48_msgb_alloc(); |
| 139 | struct gsm48_hdr *gh; |
| 140 | struct gsm48_attach_ack *aa; |
| 141 | |
| 142 | DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n"); |
| 143 | |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 144 | msgb_tlli(msg) = msgb_tlli(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 145 | msg->trx = old_msg->trx; |
| 146 | |
| 147 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 148 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 149 | gh->msg_type = GSM48_MT_GMM_ATTACH_ACK; |
| 150 | |
| 151 | aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa)); |
| 152 | aa->force_stby = 0; /* not indicated */ |
| 153 | aa->att_result = 1; /* GPRS only */ |
| 154 | aa->ra_upd_timer = GPRS_TMR_MINUTE | 10; |
| 155 | aa->radio_prio = 4; /* lowest */ |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 156 | //FIXME gsm48_ra_id_by_bts(aa->ra_id.digits, old_msg->trx->bts); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 157 | |
| 158 | /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */ |
| 159 | return gsm48_gmm_sendmsg(msg, 0); |
| 160 | } |
| 161 | |
| 162 | /* Chapter 9.4.5: Attach reject */ |
| 163 | static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, u_int8_t gmm_cause) |
| 164 | { |
| 165 | struct msgb *msg = gsm48_msgb_alloc(); |
| 166 | struct gsm48_hdr *gh; |
| 167 | |
| 168 | DEBUGP(DMM, "<- GPRS ATTACH REJECT\n"); |
| 169 | |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 170 | msgb_tlli(msg) = msgb_tlli(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 171 | msg->trx = old_msg->trx; |
| 172 | |
| 173 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 174 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 175 | gh->msg_type = GSM48_MT_GMM_ATTACH_REJ; |
| 176 | gh->data[0] = gmm_cause; |
| 177 | |
| 178 | return gsm48_gmm_sendmsg(msg, 0); |
| 179 | } |
| 180 | |
| 181 | /* Transmit Chapter 9.4.12 Identity Request */ |
| 182 | static int gsm48_tx_gmm_id_req(struct msgb *old_msg, u_int8_t id_type) |
| 183 | { |
| 184 | struct msgb *msg = gsm48_msgb_alloc(); |
| 185 | struct gsm48_hdr *gh; |
| 186 | |
| 187 | DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type); |
| 188 | |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 189 | msgb_tlli(msg) = msgb_tlli(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 190 | msg->trx = old_msg->trx; |
| 191 | |
| 192 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 193 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 194 | gh->msg_type = GSM48_MT_GMM_ID_REQ; |
| 195 | /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */ |
| 196 | gh->data[0] = id_type & 0xf; |
| 197 | |
| 198 | return gsm48_gmm_sendmsg(msg, 0); |
| 199 | } |
| 200 | |
| 201 | /* Check if we can already authorize a subscriber */ |
| 202 | static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
| 203 | { |
| 204 | if (strlen(ctx->imei) && strlen(ctx->imsi)) { |
| 205 | ctx->mm_state = GMM_REGISTERED_NORMAL; |
| 206 | return gsm48_tx_gmm_att_ack(msg); |
| 207 | } |
| 208 | if (!strlen(ctx->imei)) |
| 209 | return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI); |
| 210 | |
| 211 | if (!strlen(ctx->imsi)) |
| 212 | return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* Parse Chapter 9.4.13 Identity Response */ |
| 218 | static int gsm48_rx_gmm_id_resp(struct msgb *msg) |
| 219 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 220 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 221 | u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
| 222 | char mi_string[GSM48_MI_SIZE]; |
| 223 | struct gprs_ra_id ra_id; |
| 224 | struct sgsn_mm_ctx *ctx; |
| 225 | |
| 226 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); |
| 227 | DEBUGP(DMM, "GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ", |
| 228 | mi_type, mi_string); |
| 229 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 230 | //FIXME gprs_ra_id_by_bts(&ra_id, msg->trx->bts); |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 231 | ctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 232 | if (!ctx) { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 233 | DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 234 | return -EINVAL; |
| 235 | } |
| 236 | |
| 237 | switch (mi_type) { |
| 238 | case GSM_MI_TYPE_IMSI: |
| 239 | /* we already have a mm context with current TLLI, but no |
| 240 | * P-TMSI / IMSI yet. What we now need to do is to fill |
| 241 | * this initial context with data from the HLR */ |
| 242 | strncpy(ctx->imsi, mi_string, sizeof(ctx->imei)); |
| 243 | break; |
| 244 | case GSM_MI_TYPE_IMEI: |
| 245 | strncpy(ctx->imei, mi_string, sizeof(ctx->imei)); |
| 246 | break; |
| 247 | case GSM_MI_TYPE_IMEISV: |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | DEBUGPC(DMM, "\n"); |
| 252 | /* Check if we can let the mobile station enter */ |
| 253 | return gsm48_gmm_authorize(ctx, msg); |
| 254 | } |
| 255 | |
| 256 | static void attach_rej_cb(void *data) |
| 257 | { |
| 258 | struct sgsn_mm_ctx *ctx = data; |
| 259 | |
| 260 | /* FIXME: determine through which BTS/TRX to send this */ |
| 261 | //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED); |
| 262 | ctx->mm_state = GMM_DEREGISTERED; |
| 263 | /* FIXME: release the context */ |
| 264 | } |
| 265 | |
| 266 | static void schedule_reject(struct sgsn_mm_ctx *ctx) |
| 267 | { |
| 268 | ctx->T = 3370; |
| 269 | ctx->timer.cb = attach_rej_cb; |
| 270 | ctx->timer.data = ctx; |
| 271 | bsc_schedule_timer(&ctx->timer, 6, 0); |
| 272 | } |
| 273 | |
| 274 | /* Section 9.4.1 Attach request */ |
| 275 | static int gsm48_rx_gmm_att_req(struct msgb *msg) |
| 276 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 277 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 278 | u_int8_t *cur = gh->data, *msnc, *mi, *old_ra_info; |
| 279 | u_int8_t msnc_len, att_type, mi_len, mi_type; |
| 280 | u_int16_t drx_par; |
| 281 | u_int32_t tmsi; |
| 282 | char mi_string[GSM48_MI_SIZE]; |
| 283 | struct gprs_ra_id ra_id; |
| 284 | struct sgsn_mm_ctx *ctx; |
| 285 | |
| 286 | DEBUGP(DMM, "GMM ATTACH REQUEST "); |
| 287 | |
| 288 | /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either |
| 289 | * with a foreign TLLI (P-TMSI that was allocated to the MS before), |
| 290 | * or with random TLLI. */ |
| 291 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 292 | //FIXME gprs_ra_id_by_bts(&ra_id, msg->trx->bts); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 293 | |
| 294 | /* MS network capability 10.5.5.12 */ |
| 295 | msnc_len = *cur++; |
| 296 | msnc = cur; |
| 297 | if (msnc_len > 2) |
| 298 | goto err_inval; |
| 299 | cur += msnc_len; |
| 300 | |
| 301 | /* aTTACH Type 10.5.5.2 */ |
| 302 | att_type = *cur++ & 0x0f; |
| 303 | |
| 304 | /* DRX parameter 10.5.5.6 */ |
| 305 | drx_par = *cur++; |
| 306 | drx_par |= *cur++ << 8; |
| 307 | |
| 308 | /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */ |
| 309 | mi_len = *cur++; |
| 310 | mi = cur; |
| 311 | if (mi_len > 8) |
| 312 | goto err_inval; |
| 313 | mi_type = *mi & GSM_MI_TYPE_MASK; |
| 314 | cur += mi_len; |
| 315 | |
| 316 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 317 | |
| 318 | DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, att_name(att_type)); |
| 319 | |
| 320 | /* Old routing area identification 10.5.5.15 */ |
| 321 | old_ra_info = cur; |
| 322 | cur += 6; |
| 323 | |
| 324 | /* MS Radio Access Capability 10.5.5.12a */ |
| 325 | |
| 326 | /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */ |
| 327 | |
| 328 | switch (mi_type) { |
| 329 | case GSM_MI_TYPE_IMSI: |
| 330 | /* Try to find MM context based on IMSI */ |
| 331 | ctx = sgsn_mm_ctx_by_imsi(mi_string); |
| 332 | if (!ctx) { |
| 333 | #if 0 |
| 334 | return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN); |
| 335 | #else |
| 336 | /* As a temorary hack, we simply assume that the IMSI exists */ |
| 337 | ctx = sgsn_mm_ctx_alloc(0, &ra_id); |
| 338 | if (!ctx) |
| 339 | return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL); |
| 340 | strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi)); |
| 341 | #endif |
| 342 | } |
| 343 | /* FIXME: Start some timer */ |
| 344 | ctx->mm_state = GMM_COMMON_PROC_INIT; |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 345 | ctx->tlli = msgb_tlli(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 346 | break; |
| 347 | case GSM_MI_TYPE_TMSI: |
| 348 | tmsi = strtoul(mi_string, NULL, 10); |
| 349 | /* Try to find MM context based on P-TMSI */ |
| 350 | ctx = sgsn_mm_ctx_by_ptmsi(tmsi); |
| 351 | if (!ctx) { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 352 | ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 353 | /* FIXME: Start some timer */ |
| 354 | ctx->mm_state = GMM_COMMON_PROC_INIT; |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 355 | ctx->tlli = msgb_tlli(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 356 | } |
| 357 | break; |
| 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */ |
| 363 | /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */ |
| 364 | |
| 365 | DEBUGPC(DMM, "\n"); |
| 366 | |
| 367 | return ctx ? gsm48_gmm_authorize(ctx, msg) : 0; |
| 368 | |
| 369 | err_inval: |
| 370 | DEBUGPC(DMM, "\n"); |
| 371 | return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG); |
| 372 | } |
| 373 | |
| 374 | /* Chapter 9.4.15: Routing area update accept */ |
| 375 | static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg) |
| 376 | { |
| 377 | struct msgb *msg = gsm48_msgb_alloc(); |
| 378 | struct gsm48_hdr *gh; |
| 379 | struct gsm48_ra_upd_ack *rua; |
| 380 | |
| 381 | DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n"); |
| 382 | |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 383 | msgb_tlli(msg) = msgb_tlli(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 384 | msg->trx = old_msg->trx; |
| 385 | |
| 386 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 387 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 388 | gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK; |
| 389 | |
| 390 | rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua)); |
| 391 | rua->force_stby = 0; /* not indicated */ |
| 392 | rua->upd_result = 0; /* RA updated */ |
| 393 | rua->ra_upd_timer = GPRS_TMR_MINUTE | 10; |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 394 | //FIXME gsm48_ra_id_by_bts(rua->ra_id.digits, old_msg->trx->bts); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 395 | |
| 396 | /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */ |
| 397 | return gsm48_gmm_sendmsg(msg, 0); |
| 398 | } |
| 399 | |
| 400 | /* Chapter 9.4.17: Routing area update reject */ |
| 401 | static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, u_int8_t cause) |
| 402 | { |
| 403 | struct msgb *msg = gsm48_msgb_alloc(); |
| 404 | struct gsm48_hdr *gh; |
| 405 | |
| 406 | DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n"); |
| 407 | |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 408 | msgb_tlli(msg) = msgb_tlli(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 409 | msg->trx = old_msg->trx; |
| 410 | |
| 411 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2); |
| 412 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 413 | gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ; |
| 414 | gh->data[0] = cause; |
| 415 | gh->data[1] = 0; /* ? */ |
| 416 | |
| 417 | /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */ |
| 418 | return gsm48_gmm_sendmsg(msg, 0); |
| 419 | } |
| 420 | |
| 421 | /* Chapter 9.4.14: Routing area update request */ |
| 422 | static int gsm48_rx_gmm_ra_upd_req(struct msgb *msg) |
| 423 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 424 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 425 | struct sgsn_mm_ctx *mmctx; |
| 426 | u_int8_t *cur = gh->data; |
| 427 | struct gprs_ra_id old_ra_id; |
| 428 | u_int8_t upd_type; |
| 429 | |
| 430 | /* Update Type 10.5.5.18 */ |
| 431 | upd_type = *cur++ & 0x0f; |
| 432 | |
| 433 | DEBUGP(DMM, "GMM RA UPDATE REQUEST type=\"%s\" ", upd_name(upd_type)); |
| 434 | |
| 435 | /* Old routing area identification 10.5.5.15 */ |
| 436 | gsm48_parse_ra(&old_ra_id, cur); |
| 437 | cur += 6; |
| 438 | |
| 439 | /* MS Radio Access Capability 10.5.5.12a */ |
| 440 | |
| 441 | /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status, |
| 442 | * DRX parameter, MS network capability */ |
| 443 | |
| 444 | switch (upd_type) { |
| 445 | case GPRS_UPD_T_RA_LA: |
| 446 | case GPRS_UPD_T_RA_LA_IMSI_ATT: |
| 447 | DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n"); |
| 448 | return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC); |
| 449 | break; |
| 450 | case GPRS_UPD_T_RA: |
| 451 | case GPRS_UPD_T_PERIODIC: |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | /* Look-up the MM context based on old RA-ID and TLLI */ |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 456 | mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &old_ra_id); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 457 | if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) { |
| 458 | /* The MS has to perform GPRS attach */ |
| 459 | DEBUGPC(DMM, " REJECT\n"); |
| 460 | return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED); |
| 461 | } |
| 462 | |
| 463 | /* FIXME: Update the MM context with the new RA-ID */ |
| 464 | /* FIXME: Update the MM context with the new TLLI */ |
| 465 | /* FIXME: Update the MM context with the MS radio acc capabilities */ |
| 466 | /* FIXME: Update the MM context with the MS network capabilities */ |
| 467 | |
| 468 | DEBUGPC(DMM, " ACCEPT\n"); |
| 469 | return gsm48_tx_gmm_ra_upd_ack(msg); |
| 470 | } |
| 471 | |
| 472 | static int gsm48_rx_gmm_status(struct msgb *msg) |
| 473 | { |
| 474 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 475 | |
| 476 | DEBUGP(DMM, "GPRS MM STATUS (cause: %s)\n", |
| 477 | get_value_string(gmm_cause_names, gh->data[0])); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /* GPRS Mobility Management */ |
| 483 | static int gsm0408_rcv_gmm(struct msgb *msg) |
| 484 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 485 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 486 | int rc; |
| 487 | |
| 488 | switch (gh->msg_type) { |
| 489 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 490 | rc = gsm48_rx_gmm_ra_upd_req(msg); |
| 491 | break; |
| 492 | case GSM48_MT_GMM_ATTACH_REQ: |
| 493 | rc = gsm48_rx_gmm_att_req(msg); |
| 494 | break; |
| 495 | case GSM48_MT_GMM_ID_RESP: |
| 496 | rc = gsm48_rx_gmm_id_resp(msg); |
| 497 | break; |
| 498 | case GSM48_MT_GMM_STATUS: |
| 499 | rc = gsm48_rx_gmm_status(msg); |
| 500 | break; |
| 501 | case GSM48_MT_GMM_RA_UPD_COMPL: |
| 502 | /* only in case SGSN offered new P-TMSI */ |
| 503 | case GSM48_MT_GMM_ATTACH_COMPL: |
| 504 | /* only in case SGSN offered new P-TMSI */ |
| 505 | case GSM48_MT_GMM_DETACH_REQ: |
| 506 | case GSM48_MT_GMM_PTMSI_REALL_COMPL: |
| 507 | case GSM48_MT_GMM_AUTH_CIPH_RESP: |
| 508 | DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n", |
| 509 | gh->msg_type); |
| 510 | break; |
| 511 | default: |
| 512 | DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n", |
| 513 | gh->msg_type); |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | return rc; |
| 518 | } |
| 519 | |
| 520 | /* Section 9.5.2: Ativate PDP Context Accept */ |
| 521 | static int gsm48_tx_gsm_act_pdp_acc(struct msgb *old_msg, struct gsm48_act_pdp_ctx_req *req) |
| 522 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 523 | struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 524 | struct msgb *msg = gsm48_msgb_alloc(); |
| 525 | struct gsm48_act_pdp_ctx_ack *act_ack; |
| 526 | struct gsm48_hdr *gh; |
| 527 | u_int8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */ |
| 528 | |
| 529 | DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n"); |
| 530 | |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 531 | msgb_tlli(msg) = msgb_tlli(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 532 | msg->trx = old_msg->trx; |
| 533 | |
| 534 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 535 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 536 | gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK; |
| 537 | act_ack = (struct gsm48_act_pdp_ctx_ack *) |
| 538 | msgb_put(msg, sizeof(*act_ack)); |
| 539 | act_ack->llc_sapi = req->req_llc_sapi; |
| 540 | memcpy(act_ack->qos_lv, req->req_qos_lv, sizeof(act_ack->qos_lv)); |
| 541 | //act_ack->radio_prio = 4; |
| 542 | |
| 543 | return gsm48_gmm_sendmsg(msg, 0); |
| 544 | } |
| 545 | |
| 546 | /* Section 9.5.9: Deactivate PDP Context Accept */ |
| 547 | static int gsm48_tx_gsm_deact_pdp_acc(struct msgb *old_msg) |
| 548 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 549 | struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 550 | struct msgb *msg = gsm48_msgb_alloc(); |
| 551 | struct gsm48_hdr *gh; |
| 552 | u_int8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */ |
| 553 | |
| 554 | DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n"); |
| 555 | |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 556 | msgb_tlli(msg) = msgb_tlli(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | msg->trx = old_msg->trx; |
| 558 | |
| 559 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 560 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 561 | gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK; |
| 562 | |
| 563 | return gsm48_gmm_sendmsg(msg, 0); |
| 564 | } |
| 565 | |
| 566 | /* Section 9.5.1: Activate PDP Context Request */ |
| 567 | static int gsm48_rx_gsm_act_pdp_req(struct msgb *msg) |
| 568 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 569 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 570 | struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data; |
| 571 | u_int8_t *pdp_addr_lv = act_req->data; |
| 572 | |
| 573 | DEBUGP(DMM, "ACTIVATE PDP CONTEXT REQ\n"); |
| 574 | |
| 575 | /* FIXME: parse access point name + IPCP config options */ |
| 576 | |
| 577 | return gsm48_tx_gsm_act_pdp_acc(msg, act_req); |
| 578 | } |
| 579 | |
| 580 | /* Section 9.5.8: Deactivate PDP Context Request */ |
| 581 | static int gsm48_rx_gsm_deact_pdp_req(struct msgb *msg) |
| 582 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 583 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 584 | |
| 585 | DEBUGP(DMM, "DEACTIVATE PDP CONTEXT REQ (cause: %s)\n", |
| 586 | get_value_string(gsm_cause_names, gh->data[0])); |
| 587 | |
| 588 | return gsm48_tx_gsm_deact_pdp_acc(msg); |
| 589 | } |
| 590 | |
| 591 | static int gsm48_rx_gsm_status(struct msgb *msg) |
| 592 | { |
| 593 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 594 | |
| 595 | DEBUGP(DMM, "GPRS SM STATUS (cause: %s)\n", |
| 596 | get_value_string(gsm_cause_names, gh->data[0])); |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* GPRS Session Management */ |
| 602 | static int gsm0408_rcv_gsm(struct msgb *msg) |
| 603 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 604 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 605 | int rc; |
| 606 | |
| 607 | switch (gh->msg_type) { |
| 608 | case GSM48_MT_GSM_ACT_PDP_REQ: |
| 609 | rc = gsm48_rx_gsm_act_pdp_req(msg); |
| 610 | break; |
| 611 | case GSM48_MT_GSM_DEACT_PDP_REQ: |
| 612 | rc = gsm48_rx_gsm_deact_pdp_req(msg); |
| 613 | case GSM48_MT_GSM_STATUS: |
| 614 | rc = gsm48_rx_gsm_status(msg); |
| 615 | break; |
| 616 | case GSM48_MT_GSM_REQ_PDP_ACT_REJ: |
| 617 | case GSM48_MT_GSM_ACT_AA_PDP_REQ: |
| 618 | case GSM48_MT_GSM_DEACT_AA_PDP_REQ: |
| 619 | DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n", |
| 620 | gh->msg_type); |
| 621 | break; |
| 622 | default: |
| 623 | DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n", |
| 624 | gh->msg_type); |
| 625 | break; |
| 626 | |
| 627 | } |
| 628 | |
| 629 | return rc; |
| 630 | } |
| 631 | |
| 632 | /* Main entry point for incoming 04.08 GPRS messages */ |
| 633 | int gsm0408_gprs_rcvmsg(struct msgb *msg) |
| 634 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 635 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 636 | u_int8_t pdisc = gh->proto_discr & 0x0f; |
| 637 | int rc = -EINVAL; |
| 638 | |
| 639 | switch (pdisc) { |
| 640 | case GSM48_PDISC_MM_GPRS: |
| 641 | rc = gsm0408_rcv_gmm(msg); |
| 642 | break; |
| 643 | case GSM48_PDISC_SM_GPRS: |
| 644 | rc = gsm0408_rcv_gsm(msg); |
| 645 | break; |
| 646 | default: |
| 647 | DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n", |
| 648 | pdisc); |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | return rc; |
| 653 | } |
| 654 | |
| 655 | /* Determine the 'struct gsm_bts' from a RA ID */ |
| 656 | struct gsm_bts *gsm48_bts_by_ra_id(struct gsm_network *net, |
| 657 | const u_int8_t *buf, unsigned int len) |
| 658 | { |
| 659 | struct gprs_ra_id raid; |
| 660 | struct gsm_bts *bts; |
| 661 | |
| 662 | if (len < 6) |
| 663 | return NULL; |
| 664 | |
| 665 | gsm48_parse_ra(&raid, buf); |
| 666 | |
| 667 | if (net->country_code != raid.mcc || |
| 668 | net->network_code != raid.mnc) |
| 669 | return NULL; |
| 670 | |
| 671 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 672 | /* FIXME: we actually also need to check the |
| 673 | * routing area code! */ |
| 674 | if (bts->location_area_code == raid.lac) |
| 675 | return bts; |
| 676 | } |
| 677 | |
| 678 | return NULL; |
| 679 | } |
| 680 | |