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 | |
Harald Welte | de34afa | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 4 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 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> |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | |
| 30 | #include <netinet/in.h> |
Harald Welte | 2aea749 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 31 | #include <arpa/inet.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 32 | |
| 33 | #include <openbsc/db.h> |
| 34 | #include <osmocore/msgb.h> |
| 35 | #include <osmocore/tlv.h> |
Harald Welte | e4cc19a | 2010-05-02 12:57:45 +0200 | [diff] [blame] | 36 | #include <osmocore/gsm_utils.h> |
| 37 | #include <osmocore/signal.h> |
| 38 | #include <osmocore/talloc.h> |
| 39 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 40 | #include <openbsc/debug.h> |
| 41 | #include <openbsc/gsm_data.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 42 | #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 Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 46 | #include <openbsc/transaction.h> |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 47 | #include <openbsc/gprs_bssgp.h> |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 48 | #include <openbsc/gprs_llc.h> |
| 49 | #include <openbsc/gprs_sgsn.h> |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 50 | #include <openbsc/sgsn.h> |
| 51 | |
| 52 | extern struct sgsn_instance *sgsn; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 53 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 54 | /* Protocol related stuff, should go into libosmocore */ |
| 55 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 56 | /* 10.5.5.14 GPRS MM Cause / Table 10.5.147 */ |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 57 | const struct value_string gmm_cause_names[] = { |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 58 | /* FIXME */ |
| 59 | { GMM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" }, |
| 60 | { GMM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" }, |
| 61 | { GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL, |
| 62 | "Message type non-existant or not implemented" }, |
| 63 | { GMM_CAUSE_MSGT_INCOMP_P_STATE, |
| 64 | "Message type not compatible with protocol state" }, |
| 65 | { GMM_CAUSE_IE_NOTEXIST_NOTIMPL, |
| 66 | "Information element non-existent or not implemented" }, |
| 67 | { GMM_CAUSE_COND_IE_ERR, "Conditional IE error" }, |
| 68 | { GMM_CAUSE_MSG_INCOMP_P_STATE, |
| 69 | "Message not compatible with protocol state " }, |
| 70 | { GMM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
| 71 | { 0, NULL } |
| 72 | }; |
| 73 | |
| 74 | /* 10.5.6.6 SM Cause / Table 10.5.157 */ |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 75 | const struct value_string gsm_cause_names[] = { |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 76 | { GSM_CAUSE_INSUFF_RSRC, "Insufficient resources" }, |
| 77 | { GSM_CAUSE_MISSING_APN, "Missing or unknown APN" }, |
| 78 | { GSM_CAUSE_UNKNOWN_PDP, "Unknown PDP address or PDP type" }, |
| 79 | { GSM_CAUSE_AUTH_FAILED, "User Authentication failed" }, |
| 80 | { GSM_CAUSE_ACT_REJ_GGSN, "Activation rejected by GGSN" }, |
| 81 | { GSM_CAUSE_ACT_REJ_UNSPEC, "Activation rejected, unspecified" }, |
| 82 | { GSM_CAUSE_SERV_OPT_NOTSUPP, "Service option not supported" }, |
| 83 | { GSM_CAUSE_REQ_SERV_OPT_NOTSUB, |
| 84 | "Requested service option not subscribed" }, |
| 85 | { GSM_CAUSE_SERV_OPT_TEMP_OOO, |
| 86 | "Service option temporarily out of order" }, |
| 87 | { GSM_CAUSE_NSAPI_IN_USE, "NSAPI already used" }, |
| 88 | { GSM_CAUSE_DEACT_REGULAR, "Regular deactivation" }, |
| 89 | { GSM_CAUSE_QOS_NOT_ACCEPTED, "QoS not accepted" }, |
| 90 | { GSM_CAUSE_NET_FAIL, "Network Failure" }, |
| 91 | { GSM_CAUSE_REACT_RQD, "Reactivation required" }, |
| 92 | { GSM_CAUSE_FEATURE_NOTSUPP, "Feature not supported " }, |
| 93 | { GSM_CAUSE_INVALID_TRANS_ID, "Invalid transaction identifier" }, |
| 94 | { GSM_CAUSE_SEM_INCORR_MSG, "Semantically incorrect message" }, |
| 95 | { GSM_CAUSE_INV_MAND_INFO, "Invalid mandatory information" }, |
| 96 | { GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL, |
| 97 | "Message type non-existant or not implemented" }, |
| 98 | { GSM_CAUSE_MSGT_INCOMP_P_STATE, |
| 99 | "Message type not compatible with protocol state" }, |
| 100 | { GSM_CAUSE_IE_NOTEXIST_NOTIMPL, |
| 101 | "Information element non-existent or not implemented" }, |
| 102 | { GSM_CAUSE_COND_IE_ERR, "Conditional IE error" }, |
| 103 | { GSM_CAUSE_MSG_INCOMP_P_STATE, |
| 104 | "Message not compatible with protocol state " }, |
| 105 | { GSM_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
| 106 | { 0, NULL } |
| 107 | }; |
| 108 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 109 | /* 10.5.5.2 */ |
| 110 | const struct value_string gprs_att_t_strs[] = { |
| 111 | { GPRS_ATT_T_ATTACH, "GPRS attach" }, |
| 112 | { GPRS_ATT_T_ATT_WHILE_IMSI, "GPRS attach while IMSI attached" }, |
| 113 | { GPRS_ATT_T_COMBINED, "Combined GPRS/IMSI attach" }, |
| 114 | { 0, NULL } |
| 115 | }; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 116 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 117 | const struct value_string gprs_upd_t_strs[] = { |
| 118 | { GPRS_UPD_T_RA, "RA updating" }, |
| 119 | { GPRS_UPD_T_RA_LA, "combined RA/LA updating" }, |
| 120 | { GPRS_UPD_T_RA_LA_IMSI_ATT, "combined RA/LA updating + IMSI attach" }, |
| 121 | { GPRS_UPD_T_PERIODIC, "periodic updating" }, |
| 122 | { 0, NULL } |
| 123 | }; |
| 124 | |
| 125 | /* 10.5.5.5 */ |
| 126 | const struct value_string gprs_det_t_mo_strs[] = { |
| 127 | { GPRS_DET_T_MO_GPRS, "GPRS detach" }, |
| 128 | { GPRS_DET_T_MO_IMSI, "IMSI detach" }, |
| 129 | { GPRS_DET_T_MO_COMBINED, "Combined GPRS/IMSI detach" }, |
| 130 | { 0, NULL } |
| 131 | }; |
| 132 | |
| 133 | /* Our implementation, should be kept in SGSN */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 134 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 135 | /* Send a message through the underlying layer */ |
| 136 | static int gsm48_gmm_sendmsg(struct msgb *msg, int command) |
| 137 | { |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 138 | /* caller needs to provide TLLI, BVCI and NSEI */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 139 | return gprs_llc_tx_ui(msg, GPRS_SAPI_GMM, command); |
| 140 | } |
| 141 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 142 | /* copy identifiers from old message to new message, this |
| 143 | * is required so lower layers can route it correctly */ |
| 144 | static void gmm_copy_id(struct msgb *msg, const struct msgb *old) |
| 145 | { |
| 146 | msgb_tlli(msg) = msgb_tlli(old); |
| 147 | msgb_bvci(msg) = msgb_bvci(old); |
| 148 | msgb_nsei(msg) = msgb_nsei(old); |
| 149 | } |
| 150 | |
Harald Welte | 242d172 | 2010-05-03 10:54:35 +0200 | [diff] [blame] | 151 | static struct gsm48_qos default_qos = { |
| 152 | .delay_class = 4, /* best effort */ |
| 153 | .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT, |
| 154 | .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps, |
| 155 | .preced_class = GSM48_QOS_PC_NORMAL, |
| 156 | .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT, |
| 157 | .traf_class = GSM48_QOS_TC_INTERACTIVE, |
| 158 | .deliv_order = GSM48_QOS_DO_UNORDERED, |
| 159 | .deliv_err_sdu = GSM48_QOS_ERRSDU_YES, |
| 160 | .max_sdu_size = GSM48_QOS_MAXSDU_1520, |
| 161 | .max_bitrate_up = GSM48_QOS_MBRATE_63k, |
| 162 | .max_bitrate_down = GSM48_QOS_MBRATE_63k, |
| 163 | .resid_ber = GSM48_QOS_RBER_5e_2, |
| 164 | .sdu_err_ratio = GSM48_QOS_SERR_1e_2, |
| 165 | .handling_prio = 3, |
| 166 | .xfer_delay = 0x10, /* 200ms */ |
| 167 | .guar_bitrate_up = GSM48_QOS_MBRATE_0k, |
| 168 | .guar_bitrate_down = GSM48_QOS_MBRATE_0k, |
| 169 | .sig_ind = 0, /* not optimised for signalling */ |
| 170 | .max_bitrate_down_ext = 0, /* use octet 9 */ |
| 171 | .guar_bitrate_down_ext = 0, /* use octet 13 */ |
| 172 | }; |
| 173 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 174 | /* Chapter 9.4.2: Attach accept */ |
| 175 | static int gsm48_tx_gmm_att_ack(struct msgb *old_msg) |
| 176 | { |
| 177 | struct msgb *msg = gsm48_msgb_alloc(); |
| 178 | struct gsm48_hdr *gh; |
| 179 | struct gsm48_attach_ack *aa; |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 180 | struct gprs_ra_id ra_id; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 181 | |
| 182 | DEBUGP(DMM, "<- GPRS ATTACH ACCEPT\n"); |
| 183 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 184 | gmm_copy_id(msg, old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 185 | |
| 186 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 187 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 188 | gh->msg_type = GSM48_MT_GMM_ATTACH_ACK; |
| 189 | |
| 190 | aa = (struct gsm48_attach_ack *) msgb_put(msg, sizeof(*aa)); |
| 191 | aa->force_stby = 0; /* not indicated */ |
| 192 | aa->att_result = 1; /* GPRS only */ |
| 193 | aa->ra_upd_timer = GPRS_TMR_MINUTE | 10; |
| 194 | aa->radio_prio = 4; /* lowest */ |
Harald Welte | 18a7b9a | 2010-05-02 21:36:36 +0200 | [diff] [blame] | 195 | bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg)); |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 196 | gsm48_construct_ra(aa->ra_id.digits, &ra_id); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 197 | |
| 198 | /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */ |
| 199 | return gsm48_gmm_sendmsg(msg, 0); |
| 200 | } |
| 201 | |
| 202 | /* Chapter 9.4.5: Attach reject */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 203 | static int gsm48_tx_gmm_att_rej(struct msgb *old_msg, uint8_t gmm_cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 204 | { |
| 205 | struct msgb *msg = gsm48_msgb_alloc(); |
| 206 | struct gsm48_hdr *gh; |
| 207 | |
| 208 | DEBUGP(DMM, "<- GPRS ATTACH REJECT\n"); |
| 209 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 210 | gmm_copy_id(msg, old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 211 | |
| 212 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 213 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 214 | gh->msg_type = GSM48_MT_GMM_ATTACH_REJ; |
| 215 | gh->data[0] = gmm_cause; |
| 216 | |
| 217 | return gsm48_gmm_sendmsg(msg, 0); |
| 218 | } |
| 219 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 220 | /* Chapter 9.4.6.2 Detach accept */ |
| 221 | static gsm48_tx_gmm_det_ack(struct msgb *old_msg, uint8_t force_stby) |
| 222 | { |
| 223 | struct msgb *msg = gsm48_msgb_alloc(); |
| 224 | struct gsm48_hdr *gh; |
| 225 | |
| 226 | DEBUGP(DMM, "<- GPRS DETACH ACCEPT\n"); |
| 227 | |
| 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_DETACH_ACK; |
| 231 | gh->data[0] = force_stby; |
| 232 | |
| 233 | return gsm48_gmm_sendmsg(msg, 0); |
| 234 | } |
| 235 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 236 | /* Transmit Chapter 9.4.12 Identity Request */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 237 | static int gsm48_tx_gmm_id_req(struct msgb *old_msg, uint8_t id_type) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 238 | { |
| 239 | struct msgb *msg = gsm48_msgb_alloc(); |
| 240 | struct gsm48_hdr *gh; |
| 241 | |
| 242 | DEBUGP(DMM, "-> GPRS IDENTITY REQUEST: mi_type=%02x\n", id_type); |
| 243 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 244 | gmm_copy_id(msg, old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 245 | |
| 246 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 247 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 248 | gh->msg_type = GSM48_MT_GMM_ID_REQ; |
| 249 | /* 10.5.5.9 ID type 2 + identity type and 10.5.5.7 'force to standby' IE */ |
| 250 | gh->data[0] = id_type & 0xf; |
| 251 | |
| 252 | return gsm48_gmm_sendmsg(msg, 0); |
| 253 | } |
| 254 | |
| 255 | /* Check if we can already authorize a subscriber */ |
| 256 | static int gsm48_gmm_authorize(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
| 257 | { |
| 258 | if (strlen(ctx->imei) && strlen(ctx->imsi)) { |
| 259 | ctx->mm_state = GMM_REGISTERED_NORMAL; |
| 260 | return gsm48_tx_gmm_att_ack(msg); |
| 261 | } |
| 262 | if (!strlen(ctx->imei)) |
| 263 | return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMEI); |
| 264 | |
| 265 | if (!strlen(ctx->imsi)) |
| 266 | return gsm48_tx_gmm_id_req(msg, GSM_MI_TYPE_IMSI); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /* Parse Chapter 9.4.13 Identity Response */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 272 | static int gsm48_rx_gmm_id_resp(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 273 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 274 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 275 | uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 276 | char mi_string[GSM48_MI_SIZE]; |
| 277 | struct gprs_ra_id ra_id; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 278 | |
| 279 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]); |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 280 | DEBUGP(DMM, "-> GMM IDENTITY RESPONSE: mi_type=0x%02x MI(%s) ", |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 281 | mi_type, mi_string); |
| 282 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 283 | if (!ctx) { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 284 | DEBUGP(DMM, "from unknown TLLI 0x%08x?!?\n", msgb_tlli(msg)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | switch (mi_type) { |
| 289 | case GSM_MI_TYPE_IMSI: |
| 290 | /* we already have a mm context with current TLLI, but no |
| 291 | * P-TMSI / IMSI yet. What we now need to do is to fill |
| 292 | * this initial context with data from the HLR */ |
| 293 | strncpy(ctx->imsi, mi_string, sizeof(ctx->imei)); |
| 294 | break; |
| 295 | case GSM_MI_TYPE_IMEI: |
| 296 | strncpy(ctx->imei, mi_string, sizeof(ctx->imei)); |
| 297 | break; |
| 298 | case GSM_MI_TYPE_IMEISV: |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | DEBUGPC(DMM, "\n"); |
| 303 | /* Check if we can let the mobile station enter */ |
| 304 | return gsm48_gmm_authorize(ctx, msg); |
| 305 | } |
| 306 | |
| 307 | static void attach_rej_cb(void *data) |
| 308 | { |
| 309 | struct sgsn_mm_ctx *ctx = data; |
| 310 | |
| 311 | /* FIXME: determine through which BTS/TRX to send this */ |
| 312 | //gsm48_tx_gmm_att_rej(ctx->tlli, GMM_CAUSE_MS_ID_NOT_DERIVED); |
| 313 | ctx->mm_state = GMM_DEREGISTERED; |
| 314 | /* FIXME: release the context */ |
| 315 | } |
| 316 | |
| 317 | static void schedule_reject(struct sgsn_mm_ctx *ctx) |
| 318 | { |
| 319 | ctx->T = 3370; |
| 320 | ctx->timer.cb = attach_rej_cb; |
| 321 | ctx->timer.data = ctx; |
| 322 | bsc_schedule_timer(&ctx->timer, 6, 0); |
| 323 | } |
| 324 | |
| 325 | /* Section 9.4.1 Attach request */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 326 | static int gsm48_rx_gmm_att_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 327 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 328 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 329 | uint8_t *cur = gh->data, *msnc, *mi, *old_ra_info; |
| 330 | uint8_t msnc_len, att_type, mi_len, mi_type; |
| 331 | uint16_t drx_par; |
| 332 | uint32_t tmsi; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 333 | char mi_string[GSM48_MI_SIZE]; |
| 334 | struct gprs_ra_id ra_id; |
Harald Welte | de34afa | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 335 | uint16_t cid; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 336 | struct sgsn_mm_ctx *ctx; |
| 337 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 338 | DEBUGP(DMM, "-> GMM ATTACH REQUEST "); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 339 | |
| 340 | /* As per TS 04.08 Chapter 4.7.1.4, the attach request arrives either |
| 341 | * with a foreign TLLI (P-TMSI that was allocated to the MS before), |
| 342 | * or with random TLLI. */ |
| 343 | |
Harald Welte | de34afa | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 344 | cid = bssgp_parse_cell_id(&ra_id, msgb_bcid(msg)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 345 | |
| 346 | /* MS network capability 10.5.5.12 */ |
| 347 | msnc_len = *cur++; |
| 348 | msnc = cur; |
| 349 | if (msnc_len > 2) |
| 350 | goto err_inval; |
| 351 | cur += msnc_len; |
| 352 | |
| 353 | /* aTTACH Type 10.5.5.2 */ |
| 354 | att_type = *cur++ & 0x0f; |
| 355 | |
| 356 | /* DRX parameter 10.5.5.6 */ |
| 357 | drx_par = *cur++; |
| 358 | drx_par |= *cur++ << 8; |
| 359 | |
| 360 | /* Mobile Identity (P-TMSI or IMSI) 10.5.1.4 */ |
| 361 | mi_len = *cur++; |
| 362 | mi = cur; |
| 363 | if (mi_len > 8) |
| 364 | goto err_inval; |
| 365 | mi_type = *mi & GSM_MI_TYPE_MASK; |
| 366 | cur += mi_len; |
| 367 | |
| 368 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len); |
| 369 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 370 | DEBUGPC(DMM, "MI(%s) type=\"%s\" ", mi_string, |
| 371 | get_value_string(gprs_att_t_strs, att_type)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 372 | |
| 373 | /* Old routing area identification 10.5.5.15 */ |
| 374 | old_ra_info = cur; |
| 375 | cur += 6; |
| 376 | |
| 377 | /* MS Radio Access Capability 10.5.5.12a */ |
| 378 | |
| 379 | /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status */ |
| 380 | |
| 381 | switch (mi_type) { |
| 382 | case GSM_MI_TYPE_IMSI: |
| 383 | /* Try to find MM context based on IMSI */ |
| 384 | ctx = sgsn_mm_ctx_by_imsi(mi_string); |
| 385 | if (!ctx) { |
| 386 | #if 0 |
| 387 | return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_IMSI_UNKNOWN); |
| 388 | #else |
| 389 | /* As a temorary hack, we simply assume that the IMSI exists */ |
| 390 | ctx = sgsn_mm_ctx_alloc(0, &ra_id); |
| 391 | if (!ctx) |
| 392 | return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_NET_FAIL); |
| 393 | strncpy(ctx->imsi, mi_string, sizeof(ctx->imsi)); |
| 394 | #endif |
| 395 | } |
| 396 | /* FIXME: Start some timer */ |
| 397 | ctx->mm_state = GMM_COMMON_PROC_INIT; |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 398 | ctx->tlli = msgb_tlli(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 399 | break; |
| 400 | case GSM_MI_TYPE_TMSI: |
| 401 | tmsi = strtoul(mi_string, NULL, 10); |
| 402 | /* Try to find MM context based on P-TMSI */ |
| 403 | ctx = sgsn_mm_ctx_by_ptmsi(tmsi); |
| 404 | if (!ctx) { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 405 | ctx = sgsn_mm_ctx_alloc(msgb_tlli(msg), &ra_id); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 406 | /* FIXME: Start some timer */ |
| 407 | ctx->mm_state = GMM_COMMON_PROC_INIT; |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 408 | ctx->tlli = msgb_tlli(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 409 | } |
| 410 | break; |
| 411 | default: |
Harald Welte | de34afa | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 412 | return 0; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 413 | } |
Harald Welte | de34afa | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 414 | /* Update MM Context with currient RA and Cell ID */ |
| 415 | ctx->ra = ra_id; |
| 416 | ctx->cell_id = cid; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 417 | |
| 418 | /* FIXME: allocate a new P-TMSI (+ P-TMSI signature) */ |
| 419 | /* FIXME: update the TLLI with the new local TLLI based on the P-TMSI */ |
| 420 | |
| 421 | DEBUGPC(DMM, "\n"); |
| 422 | |
| 423 | return ctx ? gsm48_gmm_authorize(ctx, msg) : 0; |
| 424 | |
| 425 | err_inval: |
| 426 | DEBUGPC(DMM, "\n"); |
| 427 | return gsm48_tx_gmm_att_rej(msg, GMM_CAUSE_SEM_INCORR_MSG); |
| 428 | } |
| 429 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 430 | /* Section 4.7.4.1 / 9.4.5.2 MO Detach request */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 431 | static int gsm48_rx_gmm_det_req(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 432 | { |
| 433 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 434 | uint8_t detach_type, power_off; |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 435 | |
| 436 | detach_type = gh->data[0] & 0x7; |
| 437 | power_off = gh->data[0] & 0x8; |
| 438 | |
| 439 | /* FIXME: In 24.008 there is an optional P-TMSI and P-TMSI signature IE */ |
| 440 | |
| 441 | DEBUGP(DMM, "-> GMM DETACH REQUEST TLLI=0x%08x type=%s %s\n", |
| 442 | msgb_tlli(msg), get_value_string(gprs_det_t_mo_strs, detach_type), |
| 443 | power_off ? "Power-off" : ""); |
| 444 | |
| 445 | /* Mark MM state as deregistered */ |
| 446 | ctx->mm_state = GMM_DEREGISTERED; |
| 447 | |
| 448 | /* force_stby = 0 */ |
| 449 | return gsm48_tx_gmm_det_ack(msg, 0); |
| 450 | } |
| 451 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 452 | /* Chapter 9.4.15: Routing area update accept */ |
| 453 | static int gsm48_tx_gmm_ra_upd_ack(struct msgb *old_msg) |
| 454 | { |
| 455 | struct msgb *msg = gsm48_msgb_alloc(); |
| 456 | struct gsm48_hdr *gh; |
| 457 | struct gsm48_ra_upd_ack *rua; |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 458 | struct gprs_ra_id ra_id; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 459 | |
| 460 | DEBUGP(DMM, "<- ROUTING AREA UPDATE ACCEPT\n"); |
| 461 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 462 | gmm_copy_id(msg, old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 463 | |
| 464 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 465 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 466 | gh->msg_type = GSM48_MT_GMM_RA_UPD_ACK; |
| 467 | |
| 468 | rua = (struct gsm48_ra_upd_ack *) msgb_put(msg, sizeof(*rua)); |
| 469 | rua->force_stby = 0; /* not indicated */ |
| 470 | rua->upd_result = 0; /* RA updated */ |
| 471 | rua->ra_upd_timer = GPRS_TMR_MINUTE | 10; |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 472 | |
Harald Welte | 18a7b9a | 2010-05-02 21:36:36 +0200 | [diff] [blame] | 473 | bssgp_parse_cell_id(&ra_id, msgb_bcid(old_msg)); |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 474 | gsm48_construct_ra(rua->ra_id.digits, &ra_id); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 475 | |
| 476 | /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */ |
| 477 | return gsm48_gmm_sendmsg(msg, 0); |
| 478 | } |
| 479 | |
| 480 | /* Chapter 9.4.17: Routing area update reject */ |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 481 | static int gsm48_tx_gmm_ra_upd_rej(struct msgb *old_msg, uint8_t cause) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 482 | { |
| 483 | struct msgb *msg = gsm48_msgb_alloc(); |
| 484 | struct gsm48_hdr *gh; |
| 485 | |
| 486 | DEBUGP(DMM, "<- ROUTING AREA UPDATE REJECT\n"); |
| 487 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 488 | gmm_copy_id(msg, old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 489 | |
| 490 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2); |
| 491 | gh->proto_discr = GSM48_PDISC_MM_GPRS; |
| 492 | gh->msg_type = GSM48_MT_GMM_RA_UPD_REJ; |
| 493 | gh->data[0] = cause; |
| 494 | gh->data[1] = 0; /* ? */ |
| 495 | |
| 496 | /* Option: P-TMSI signature, allocated P-TMSI, MS ID, ... */ |
| 497 | return gsm48_gmm_sendmsg(msg, 0); |
| 498 | } |
| 499 | |
| 500 | /* Chapter 9.4.14: Routing area update request */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 501 | static int gsm48_rx_gmm_ra_upd_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 502 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 503 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 504 | uint8_t *cur = gh->data; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 505 | struct gprs_ra_id old_ra_id; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 506 | uint8_t upd_type; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 507 | |
| 508 | /* Update Type 10.5.5.18 */ |
| 509 | upd_type = *cur++ & 0x0f; |
| 510 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 511 | DEBUGP(DMM, "-> GMM RA UPDATE REQUEST type=\"%s\" ", |
| 512 | get_value_string(gprs_upd_t_strs, upd_type)); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 513 | |
| 514 | /* Old routing area identification 10.5.5.15 */ |
| 515 | gsm48_parse_ra(&old_ra_id, cur); |
| 516 | cur += 6; |
| 517 | |
| 518 | /* MS Radio Access Capability 10.5.5.12a */ |
| 519 | |
| 520 | /* Optional: Old P-TMSI Signature, Requested READY timer, TMSI Status, |
| 521 | * DRX parameter, MS network capability */ |
| 522 | |
| 523 | switch (upd_type) { |
| 524 | case GPRS_UPD_T_RA_LA: |
| 525 | case GPRS_UPD_T_RA_LA_IMSI_ATT: |
| 526 | DEBUGPC(DMM, " unsupported in Mode III, is your SI13 corrupt?\n"); |
| 527 | return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_PROTO_ERR_UNSPEC); |
| 528 | break; |
| 529 | case GPRS_UPD_T_RA: |
| 530 | case GPRS_UPD_T_PERIODIC: |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | /* Look-up the MM context based on old RA-ID and TLLI */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 535 | if (!mmctx || mmctx->mm_state == GMM_DEREGISTERED) { |
| 536 | /* The MS has to perform GPRS attach */ |
| 537 | DEBUGPC(DMM, " REJECT\n"); |
| 538 | return gsm48_tx_gmm_ra_upd_rej(msg, GMM_CAUSE_IMPL_DETACHED); |
| 539 | } |
| 540 | |
Harald Welte | de34afa | 2010-05-02 12:56:57 +0200 | [diff] [blame] | 541 | /* Update the MM context with the new RA-ID */ |
| 542 | bssgp_parse_cell_id(&mmctx->ra, msgb_bcid(msg)); |
| 543 | /* Update the MM context with the new TLLI */ |
| 544 | mmctx->tlli = msgb_tlli(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 545 | /* FIXME: Update the MM context with the MS radio acc capabilities */ |
| 546 | /* FIXME: Update the MM context with the MS network capabilities */ |
| 547 | |
| 548 | DEBUGPC(DMM, " ACCEPT\n"); |
| 549 | return gsm48_tx_gmm_ra_upd_ack(msg); |
| 550 | } |
| 551 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 552 | static int gsm48_rx_gmm_status(struct sgsn_mm_ctx *mmctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 553 | { |
| 554 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 555 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 556 | DEBUGP(DMM, "-> GPRS MM STATUS (cause: %s)\n", |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 557 | get_value_string(gmm_cause_names, gh->data[0])); |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /* GPRS Mobility Management */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 563 | static int gsm0408_rcv_gmm(struct sgsn_mm_ctx *mmctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 564 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 565 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 566 | int rc; |
| 567 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 568 | if (!mmctx && |
| 569 | gh->msg_type != GSM48_MT_GMM_ATTACH_REQ && |
| 570 | gh->msg_type != GSM48_MT_GMM_RA_UPD_REQ) { |
| 571 | LOGP(DMM, LOGL_NOTICE, "Cannot handle GMM for unknown MM CTX\n"); |
| 572 | return -EINVAL; |
| 573 | } |
| 574 | |
| 575 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 576 | switch (gh->msg_type) { |
| 577 | case GSM48_MT_GMM_RA_UPD_REQ: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 578 | rc = gsm48_rx_gmm_ra_upd_req(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 579 | break; |
| 580 | case GSM48_MT_GMM_ATTACH_REQ: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 581 | rc = gsm48_rx_gmm_att_req(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 582 | break; |
| 583 | case GSM48_MT_GMM_ID_RESP: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 584 | rc = gsm48_rx_gmm_id_resp(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 585 | break; |
| 586 | case GSM48_MT_GMM_STATUS: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 587 | rc = gsm48_rx_gmm_status(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 588 | break; |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 589 | case GSM48_MT_GMM_DETACH_REQ: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 590 | rc = gsm48_rx_gmm_det_req(mmctx, msg); |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 591 | break; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 592 | case GSM48_MT_GMM_RA_UPD_COMPL: |
| 593 | /* only in case SGSN offered new P-TMSI */ |
| 594 | case GSM48_MT_GMM_ATTACH_COMPL: |
| 595 | /* only in case SGSN offered new P-TMSI */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 596 | case GSM48_MT_GMM_PTMSI_REALL_COMPL: |
| 597 | case GSM48_MT_GMM_AUTH_CIPH_RESP: |
| 598 | DEBUGP(DMM, "Unimplemented GSM 04.08 GMM msg type 0x%02x\n", |
| 599 | gh->msg_type); |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 600 | /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 601 | break; |
| 602 | default: |
| 603 | DEBUGP(DMM, "Unknown GSM 04.08 GMM msg type 0x%02x\n", |
| 604 | gh->msg_type); |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 605 | /* FIXME: Send GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 606 | break; |
| 607 | } |
| 608 | |
| 609 | return rc; |
| 610 | } |
| 611 | |
Harald Welte | 2aea749 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 612 | static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr) |
| 613 | { |
| 614 | uint8_t v[6]; |
| 615 | |
| 616 | v[0] = PDP_TYPE_ORG_IETF; |
| 617 | v[1] = PDP_TYPE_N_IETF_IPv4; |
| 618 | *(uint32_t *)(v+2) = htonl(ipaddr); |
| 619 | |
| 620 | msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v); |
| 621 | } |
| 622 | |
| 623 | static void msgb_put_pdp_addr_ppp(struct msgb *msg) |
| 624 | { |
| 625 | uint8_t v[2]; |
| 626 | |
| 627 | v[0] = PDP_TYPE_ORG_ETSI; |
| 628 | v[1] = PDP_TYPE_N_ETSI_PPP; |
| 629 | |
| 630 | msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v); |
| 631 | } |
| 632 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 633 | /* Section 9.5.2: Ativate PDP Context Accept */ |
| 634 | static int gsm48_tx_gsm_act_pdp_acc(struct msgb *old_msg, struct gsm48_act_pdp_ctx_req *req) |
| 635 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 636 | struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 637 | struct msgb *msg = gsm48_msgb_alloc(); |
| 638 | struct gsm48_act_pdp_ctx_ack *act_ack; |
| 639 | struct gsm48_hdr *gh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 640 | uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 641 | |
| 642 | DEBUGP(DMM, "<- ACTIVATE PDP CONTEXT ACK\n"); |
| 643 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 644 | gmm_copy_id(msg, old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 645 | |
| 646 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 647 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 648 | gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK; |
Harald Welte | 2aea749 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 649 | |
| 650 | /* Negotiated LLC SAPI */ |
| 651 | msgb_v_put(msg, req->req_llc_sapi); |
| 652 | /* copy QoS parameters from original request */ |
Harald Welte | 242d172 | 2010-05-03 10:54:35 +0200 | [diff] [blame] | 653 | msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos); |
Harald Welte | 2aea749 | 2010-05-02 23:11:50 +0200 | [diff] [blame] | 654 | /* Radio priority 10.5.7.2 */ |
| 655 | msgb_v_put(msg, 4); |
| 656 | /* PDP address */ |
| 657 | msgb_put_pdp_addr_ipv4(msg, 0x01020304); |
| 658 | /* Optional: Protocol configuration options */ |
| 659 | /* Optional: Packet Flow Identifier */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 660 | |
| 661 | return gsm48_gmm_sendmsg(msg, 0); |
| 662 | } |
| 663 | |
| 664 | /* Section 9.5.9: Deactivate PDP Context Accept */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 665 | static int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mmctx, |
| 666 | struct msgb *old_msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 667 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 668 | struct gsm48_hdr *old_gh = (struct gsm48_hdr *) msgb_gmmh(old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 669 | struct msgb *msg = gsm48_msgb_alloc(); |
| 670 | struct gsm48_hdr *gh; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 671 | uint8_t transaction_id = ((old_gh->proto_discr >> 4) ^ 0x8); /* flip */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 672 | |
| 673 | DEBUGP(DMM, "<- DEACTIVATE PDP CONTEXT ACK\n"); |
| 674 | |
Harald Welte | 2351f1f | 2010-05-02 11:54:55 +0200 | [diff] [blame] | 675 | gmm_copy_id(msg, old_msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 676 | |
| 677 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh)); |
| 678 | gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4); |
| 679 | gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK; |
| 680 | |
| 681 | return gsm48_gmm_sendmsg(msg, 0); |
| 682 | } |
| 683 | |
| 684 | /* Section 9.5.1: Activate PDP Context Request */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 685 | static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx, |
| 686 | struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 687 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 688 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 689 | struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data; |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 690 | uint8_t *pdp_addr_lv = act_req->data; |
Harald Welte | b1fd23a | 2010-05-02 21:07:14 +0200 | [diff] [blame] | 691 | uint8_t req_qos_len, req_pdpa_len; |
| 692 | uint8_t *req_qos, *req_pdpa; |
| 693 | struct tlv_parsed tp; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 694 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 695 | DEBUGP(DMM, "-> ACTIVATE PDP CONTEXT REQ: "); |
Harald Welte | b1fd23a | 2010-05-02 21:07:14 +0200 | [diff] [blame] | 696 | req_qos_len = act_req->data[0]; |
| 697 | req_qos = act_req->data + 1; /* 10.5.6.5 */ |
| 698 | req_pdpa_len = act_req->data[1 + req_qos_len]; |
| 699 | req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */ |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 700 | |
Harald Welte | b1fd23a | 2010-05-02 21:07:14 +0200 | [diff] [blame] | 701 | switch (req_pdpa[0] & 0xf) { |
| 702 | case 0x0: |
| 703 | DEBUGPC(DMM, "ETSI "); |
| 704 | break; |
| 705 | case 0x1: |
| 706 | DEBUGPC(DMM, "IETF "); |
| 707 | break; |
| 708 | case 0xf: |
| 709 | DEBUGPC(DMM, "Empty "); |
| 710 | break; |
| 711 | } |
| 712 | |
| 713 | switch (req_pdpa[1]) { |
| 714 | case 0x21: |
| 715 | DEBUGPC(DMM, "IPv4 "); |
| 716 | if (req_pdpa_len >= 6) { |
| 717 | struct in_addr ia; |
| 718 | ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2))); |
| 719 | DEBUGPC(DMM, "%s ", inet_ntoa(ia)); |
| 720 | } |
| 721 | break; |
| 722 | case 0x57: |
| 723 | DEBUGPC(DMM, "IPv6 "); |
| 724 | if (req_pdpa_len >= 18) { |
| 725 | /* FIXME: print IPv6 address */ |
| 726 | } |
| 727 | break; |
| 728 | default: |
| 729 | DEBUGPC(DMM, "0x%02x ", req_pdpa[1]); |
| 730 | break; |
| 731 | } |
| 732 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 733 | /* put the non-TLV elements in the TLV parser structure to |
| 734 | * pass them on to the SGSN / GTP code */ |
| 735 | tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len; |
| 736 | tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos; |
| 737 | tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len; |
| 738 | tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa; |
| 739 | |
Harald Welte | b1fd23a | 2010-05-02 21:07:14 +0200 | [diff] [blame] | 740 | /* FIXME: parse TLV for AP name and protocol config options */ |
| 741 | if (TLVP_PRESENT(&tp, GSM48_IE_GSM_APN)) {} |
| 742 | if (TLVP_PRESENT(&tp, GSM48_IE_GSM_PROTO_CONF_OPT)) {} |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 743 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 744 | #if 1 |
| 745 | { |
| 746 | struct ggsn_ctx ggsn; |
| 747 | ggsn.gtp_version = 1; |
| 748 | inet_aton("192.168.100.239", &ggsn.remote_addr); |
| 749 | ggsn.gsn = sgsn->gsn; |
| 750 | return sgsn_create_pdp_ctx(ggsn, mmctx, 5, &tp); |
| 751 | } |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 752 | #else |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 753 | return gsm48_tx_gsm_act_pdp_acc(msg, act_req); |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 754 | #endif |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* Section 9.5.8: Deactivate PDP Context Request */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 758 | static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 759 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 760 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 761 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 762 | DEBUGP(DMM, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n", |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 763 | get_value_string(gsm_cause_names, gh->data[0])); |
| 764 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 765 | return gsm48_tx_gsm_deact_pdp_acc(ctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 766 | } |
| 767 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 768 | static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 769 | { |
| 770 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 771 | |
Harald Welte | 8f77f19 | 2010-05-17 00:44:57 +0200 | [diff] [blame] | 772 | DEBUGP(DMM, "-> GPRS SM STATUS (cause: %s)\n", |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 773 | get_value_string(gsm_cause_names, gh->data[0])); |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | /* GPRS Session Management */ |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 779 | static int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg) |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 780 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 781 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 782 | int rc; |
| 783 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 784 | if (!mmctx) { |
| 785 | LOGP(DMM, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n"); |
| 786 | return -EINVAL; |
| 787 | } |
| 788 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 789 | switch (gh->msg_type) { |
| 790 | case GSM48_MT_GSM_ACT_PDP_REQ: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 791 | rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 792 | break; |
| 793 | case GSM48_MT_GSM_DEACT_PDP_REQ: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 794 | rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 795 | case GSM48_MT_GSM_STATUS: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 796 | rc = gsm48_rx_gsm_status(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 797 | break; |
| 798 | case GSM48_MT_GSM_REQ_PDP_ACT_REJ: |
| 799 | case GSM48_MT_GSM_ACT_AA_PDP_REQ: |
| 800 | case GSM48_MT_GSM_DEACT_AA_PDP_REQ: |
| 801 | DEBUGP(DMM, "Unimplemented GSM 04.08 GSM msg type 0x%02x\n", |
| 802 | gh->msg_type); |
| 803 | break; |
| 804 | default: |
| 805 | DEBUGP(DMM, "Unknown GSM 04.08 GSM msg type 0x%02x\n", |
| 806 | gh->msg_type); |
| 807 | break; |
| 808 | |
| 809 | } |
| 810 | |
| 811 | return rc; |
| 812 | } |
| 813 | |
| 814 | /* Main entry point for incoming 04.08 GPRS messages */ |
| 815 | int gsm0408_gprs_rcvmsg(struct msgb *msg) |
| 816 | { |
Harald Welte | cf3c414 | 2010-04-30 16:33:12 +0200 | [diff] [blame] | 817 | struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg); |
Harald Welte | d85d9a9 | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 818 | uint8_t pdisc = gh->proto_discr & 0x0f; |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 819 | struct sgsn_mm_ctx *mmctx; |
| 820 | struct gprs_ra_id ra_id; |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 821 | int rc = -EINVAL; |
| 822 | |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 823 | bssgp_parse_cell_id(&ra_id, msgb_bcid(msg)); |
| 824 | mmctx = sgsn_mm_ctx_by_tlli(msgb_tlli(msg), &ra_id); |
| 825 | |
| 826 | /* MMCTX can be NULL */ |
| 827 | |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 828 | switch (pdisc) { |
| 829 | case GSM48_PDISC_MM_GPRS: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 830 | rc = gsm0408_rcv_gmm(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 831 | break; |
| 832 | case GSM48_PDISC_SM_GPRS: |
Harald Welte | c1f6bfe | 2010-05-17 22:58:03 +0200 | [diff] [blame] | 833 | rc = gsm0408_rcv_gsm(mmctx, msg); |
Harald Welte | 75bb820 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 834 | break; |
| 835 | default: |
| 836 | DEBUGP(DMM, "Unknown GSM 04.08 discriminator 0x%02x\n", |
| 837 | pdisc); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | return rc; |
| 842 | } |