Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 1 | /* GSM Mobile Radio Interface Layer 3 messages |
| 2 | * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */ |
| 3 | |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame] | 4 | /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 5 | * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | #include <osmocore/utils.h> |
| 32 | #include <osmocore/tlv.h> |
| 33 | #include <osmocore/gsm48.h> |
| 34 | |
| 35 | #include <osmocore/protocol/gsm_04_08.h> |
| 36 | |
| 37 | const struct tlv_definition gsm48_att_tlvdef = { |
| 38 | .def = { |
| 39 | [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV }, |
| 40 | [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV }, |
| 41 | [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV }, |
| 42 | [GSM48_IE_UTC] = { TLV_TYPE_TV }, |
| 43 | [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 }, |
| 44 | [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV }, |
| 45 | |
| 46 | [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV }, |
| 47 | [GSM48_IE_CAUSE] = { TLV_TYPE_TLV }, |
| 48 | [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV }, |
| 49 | [GSM48_IE_ALERT] = { TLV_TYPE_TLV }, |
| 50 | [GSM48_IE_FACILITY] = { TLV_TYPE_TLV }, |
| 51 | [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV }, |
| 52 | [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV }, |
| 53 | [GSM48_IE_NOTIFY] = { TLV_TYPE_TV }, |
| 54 | [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV }, |
| 55 | [GSM48_IE_SIGNAL] = { TLV_TYPE_TV }, |
| 56 | [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV }, |
| 57 | [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV }, |
| 58 | [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV }, |
| 59 | [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV }, |
| 60 | [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV }, |
| 61 | [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV }, |
| 62 | [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV }, |
| 63 | [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV }, |
| 64 | [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV }, |
| 65 | [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV }, |
| 66 | [GSM48_IE_USER_USER] = { TLV_TYPE_TLV }, |
| 67 | [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV }, |
| 68 | [GSM48_IE_MORE_DATA] = { TLV_TYPE_T }, |
| 69 | [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T }, |
| 70 | [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T }, |
| 71 | [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T }, |
| 72 | [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T }, |
| 73 | [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T }, |
| 74 | /* FIXME: more elements */ |
| 75 | }, |
| 76 | }; |
| 77 | |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame] | 78 | static const struct value_string rr_cause_names[] = { |
| 79 | { GSM48_RR_CAUSE_NORMAL, "Normal event" }, |
| 80 | { GSM48_RR_CAUSE_ABNORMAL_UNSPEC, "Abnormal release, unspecified" }, |
| 81 | { GSM48_RR_CAUSE_ABNORMAL_UNACCT, "Abnormal release, channel unacceptable" }, |
| 82 | { GSM48_RR_CAUSE_ABNORMAL_TIMER, "Abnormal release, timer expired" }, |
| 83 | { GSM48_RR_CAUSE_ABNORMAL_NOACT, "Abnormal release, no activity on radio path" }, |
| 84 | { GSM48_RR_CAUSE_PREMPTIVE_REL, "Preemptive release" }, |
| 85 | { GSM48_RR_CAUSE_HNDOVER_IMP, "Handover impossible, timing advance out of range" }, |
| 86 | { GSM48_RR_CAUSE_CHAN_MODE_UNACCT, "Channel mode unacceptable" }, |
| 87 | { GSM48_RR_CAUSE_FREQ_NOT_IMPL, "Frequency not implemented" }, |
| 88 | { GSM48_RR_CAUSE_CALL_CLEARED, "Call already cleared" }, |
| 89 | { GSM48_RR_CAUSE_SEMANT_INCORR, "Semantically incorrect message" }, |
| 90 | { GSM48_RR_CAUSE_INVALID_MAND_INF, "Invalid mandatory information" }, |
| 91 | { GSM48_RR_CAUSE_MSG_TYPE_N, "Message type non-existant or not implemented" }, |
| 92 | { GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT, "Message type not compatible with protocol state" }, |
| 93 | { GSM48_RR_CAUSE_COND_IE_ERROR, "Conditional IE error" }, |
| 94 | { GSM48_RR_CAUSE_NO_CELL_ALLOC_A, "No cell allocation available" }, |
| 95 | { GSM48_RR_CAUSE_PROT_ERROR_UNSPC, "Protocol error unspecified" }, |
| 96 | { 0, NULL }, |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 97 | }; |
| 98 | |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame] | 99 | /* FIXME: convert to value_string */ |
Harald Welte | f3f9002 | 2010-04-30 14:29:56 +0200 | [diff] [blame] | 100 | static const char *cc_state_names[33] = { |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 101 | "NULL", |
| 102 | "INITIATED", |
Harald Welte | f3f9002 | 2010-04-30 14:29:56 +0200 | [diff] [blame] | 103 | "MM_CONNECTION_PEND", |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 104 | "MO_CALL_PROC", |
| 105 | "CALL_DELIVERED", |
| 106 | "illegal state 5", |
| 107 | "CALL_PRESENT", |
| 108 | "CALL_RECEIVED", |
| 109 | "CONNECT_REQUEST", |
| 110 | "MO_TERM_CALL_CONF", |
| 111 | "ACTIVE", |
| 112 | "DISCONNECT_REQ", |
| 113 | "DISCONNECT_IND", |
| 114 | "illegal state 13", |
| 115 | "illegal state 14", |
| 116 | "illegal state 15", |
| 117 | "illegal state 16", |
| 118 | "illegal state 17", |
| 119 | "illegal state 18", |
| 120 | "RELEASE_REQ", |
| 121 | "illegal state 20", |
| 122 | "illegal state 21", |
| 123 | "illegal state 22", |
| 124 | "illegal state 23", |
| 125 | "illegal state 24", |
| 126 | "illegal state 25", |
| 127 | "MO_ORIG_MODIFY", |
| 128 | "MO_TERM_MODIFY", |
| 129 | "CONNECT_IND", |
| 130 | "illegal state 29", |
| 131 | "illegal state 30", |
| 132 | "illegal state 31", |
| 133 | }; |
| 134 | |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame] | 135 | const char *gsm48_cc_state_name(uint8_t state) |
| 136 | { |
| 137 | if (state < ARRAY_SIZE(cc_state_names)) |
| 138 | return cc_state_names[state]; |
| 139 | |
| 140 | return "invalid"; |
| 141 | } |
| 142 | |
| 143 | static const struct value_string cc_msg_names[] = { |
| 144 | { GSM48_MT_CC_ALERTING, "ALERTING" }, |
| 145 | { GSM48_MT_CC_CALL_PROC, "CALL_PROC" }, |
| 146 | { GSM48_MT_CC_PROGRESS, "PROGRESS" }, |
| 147 | { GSM48_MT_CC_ESTAB, "ESTAB" }, |
| 148 | { GSM48_MT_CC_SETUP, "SETUP" }, |
| 149 | { GSM48_MT_CC_ESTAB_CONF, "ESTAB_CONF" }, |
| 150 | { GSM48_MT_CC_CONNECT, "CONNECT" }, |
| 151 | { GSM48_MT_CC_CALL_CONF, "CALL_CONF" }, |
| 152 | { GSM48_MT_CC_START_CC, "START_CC" }, |
| 153 | { GSM48_MT_CC_RECALL, "RECALL" }, |
| 154 | { GSM48_MT_CC_EMERG_SETUP, "EMERG_SETUP" }, |
| 155 | { GSM48_MT_CC_CONNECT_ACK, "CONNECT_ACK" }, |
| 156 | { GSM48_MT_CC_USER_INFO, "USER_INFO" }, |
| 157 | { GSM48_MT_CC_MODIFY_REJECT, "MODIFY_REJECT" }, |
| 158 | { GSM48_MT_CC_MODIFY, "MODIFY" }, |
| 159 | { GSM48_MT_CC_HOLD, "HOLD" }, |
| 160 | { GSM48_MT_CC_HOLD_ACK, "HOLD_ACK" }, |
| 161 | { GSM48_MT_CC_HOLD_REJ, "HOLD_REJ" }, |
| 162 | { GSM48_MT_CC_RETR, "RETR" }, |
| 163 | { GSM48_MT_CC_RETR_ACK, "RETR_ACK" }, |
| 164 | { GSM48_MT_CC_RETR_REJ, "RETR_REJ" }, |
| 165 | { GSM48_MT_CC_MODIFY_COMPL, "MODIFY_COMPL" }, |
| 166 | { GSM48_MT_CC_DISCONNECT, "DISCONNECT" }, |
| 167 | { GSM48_MT_CC_RELEASE_COMPL, "RELEASE_COMPL" }, |
| 168 | { GSM48_MT_CC_RELEASE, "RELEASE" }, |
| 169 | { GSM48_MT_CC_STOP_DTMF, "STOP_DTMF" }, |
| 170 | { GSM48_MT_CC_STOP_DTMF_ACK, "STOP_DTMF_ACK" }, |
| 171 | { GSM48_MT_CC_STATUS_ENQ, "STATUS_ENQ" }, |
| 172 | { GSM48_MT_CC_START_DTMF, "START_DTMF" }, |
| 173 | { GSM48_MT_CC_START_DTMF_ACK, "START_DTMF_ACK" }, |
| 174 | { GSM48_MT_CC_START_DTMF_REJ, "START_DTMF_REJ" }, |
| 175 | { GSM48_MT_CC_CONG_CTRL, "CONG_CTRL" }, |
| 176 | { GSM48_MT_CC_FACILITY, "FACILITY" }, |
| 177 | { GSM48_MT_CC_STATUS, "STATUS" }, |
| 178 | { GSM48_MT_CC_NOTIFY, "NOTFIY" }, |
| 179 | { 0, NULL } |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 180 | }; |
| 181 | |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame] | 182 | const char *gsm48_cc_msg_name(uint8_t msgtype) |
| 183 | { |
| 184 | return get_value_string(cc_msg_names, msgtype); |
| 185 | } |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 186 | |
| 187 | const char *rr_cause_name(uint8_t cause) |
| 188 | { |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame] | 189 | return get_value_string(rr_cause_names, cause); |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void to_bcd(uint8_t *bcd, uint16_t val) |
| 193 | { |
| 194 | bcd[2] = val % 10; |
| 195 | val = val / 10; |
| 196 | bcd[1] = val % 10; |
| 197 | val = val / 10; |
| 198 | bcd[0] = val % 10; |
| 199 | val = val / 10; |
| 200 | } |
| 201 | |
| 202 | void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc, |
| 203 | uint16_t mnc, uint16_t lac) |
| 204 | { |
| 205 | uint8_t bcd[3]; |
| 206 | |
| 207 | to_bcd(bcd, mcc); |
| 208 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 209 | lai48->digits[1] = bcd[2]; |
| 210 | |
| 211 | to_bcd(bcd, mnc); |
| 212 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 213 | #if 0 |
| 214 | lai48->digits[1] |= bcd[2] << 4; |
| 215 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
| 216 | #else |
| 217 | lai48->digits[1] |= 0xf << 4; |
| 218 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 219 | #endif |
| 220 | |
| 221 | lai48->lac = htons(lac); |
| 222 | } |
| 223 | |
| 224 | int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi) |
| 225 | { |
| 226 | uint32_t *tptr = (uint32_t *) &buf[3]; |
| 227 | |
| 228 | buf[0] = GSM48_IE_MOBILE_ID; |
| 229 | buf[1] = GSM48_TMSI_LEN; |
| 230 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 231 | *tptr = htonl(tmsi); |
| 232 | |
| 233 | return 7; |
| 234 | } |
| 235 | |
| 236 | int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi) |
| 237 | { |
| 238 | unsigned int length = strlen(imsi), i, off = 0; |
| 239 | uint8_t odd = (length & 0x1) == 1; |
| 240 | |
| 241 | buf[0] = GSM48_IE_MOBILE_ID; |
| 242 | buf[2] = char2bcd(imsi[0]) << 4 | GSM_MI_TYPE_IMSI | (odd << 3); |
| 243 | |
| 244 | /* if the length is even we will fill half of the last octet */ |
| 245 | if (odd) |
| 246 | buf[1] = (length + 1) >> 1; |
| 247 | else |
| 248 | buf[1] = (length + 2) >> 1; |
| 249 | |
| 250 | for (i = 1; i < buf[1]; ++i) { |
| 251 | uint8_t lower, upper; |
| 252 | |
| 253 | lower = char2bcd(imsi[++off]); |
| 254 | if (!odd && off + 1 == length) |
| 255 | upper = 0x0f; |
| 256 | else |
| 257 | upper = char2bcd(imsi[++off]) & 0x0f; |
| 258 | |
| 259 | buf[2 + i] = (upper << 4) | lower; |
| 260 | } |
| 261 | |
| 262 | return 2 + buf[1]; |
| 263 | } |
Harald Welte | f3f9002 | 2010-04-30 14:29:56 +0200 | [diff] [blame] | 264 | |
| 265 | /* Convert Mobile Identity (10.5.1.4) to string */ |
| 266 | int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi, |
| 267 | const int mi_len) |
| 268 | { |
| 269 | int i; |
| 270 | uint8_t mi_type; |
| 271 | char *str_cur = string; |
| 272 | uint32_t tmsi; |
| 273 | |
| 274 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 275 | |
| 276 | switch (mi_type) { |
| 277 | case GSM_MI_TYPE_NONE: |
| 278 | break; |
| 279 | case GSM_MI_TYPE_TMSI: |
| 280 | /* Table 10.5.4.3, reverse generate_mid_from_tmsi */ |
| 281 | if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) { |
| 282 | memcpy(&tmsi, &mi[1], 4); |
| 283 | tmsi = ntohl(tmsi); |
| 284 | return snprintf(string, str_len, "%u", tmsi); |
| 285 | } |
| 286 | break; |
| 287 | case GSM_MI_TYPE_IMSI: |
| 288 | case GSM_MI_TYPE_IMEI: |
| 289 | case GSM_MI_TYPE_IMEISV: |
| 290 | *str_cur++ = bcd2char(mi[0] >> 4); |
| 291 | |
| 292 | for (i = 1; i < mi_len; i++) { |
| 293 | if (str_cur + 2 >= string + str_len) |
| 294 | return str_cur - string; |
| 295 | *str_cur++ = bcd2char(mi[i] & 0xf); |
| 296 | /* skip last nibble in last input byte when GSM_EVEN */ |
| 297 | if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD)) |
| 298 | *str_cur++ = bcd2char(mi[i] >> 4); |
| 299 | } |
| 300 | break; |
| 301 | default: |
| 302 | break; |
| 303 | } |
| 304 | *str_cur++ = '\0'; |
| 305 | |
| 306 | return str_cur - string; |
| 307 | } |
Harald Welte | c795235 | 2010-05-01 12:07:56 +0200 | [diff] [blame] | 308 | |
| 309 | void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf) |
| 310 | { |
| 311 | raid->mcc = (buf[0] & 0xf) * 100; |
| 312 | raid->mcc += (buf[0] >> 4) * 10; |
| 313 | raid->mcc += (buf[1] & 0xf) * 1; |
| 314 | |
| 315 | /* I wonder who came up with the stupidity of encoding the MNC |
| 316 | * differently depending on how many digits its decimal number has! */ |
| 317 | if ((buf[1] >> 4) == 0xf) { |
| 318 | raid->mnc = (buf[2] & 0xf) * 10; |
| 319 | raid->mnc += (buf[2] >> 4) * 1; |
| 320 | } else { |
| 321 | raid->mnc = (buf[2] & 0xf) * 100; |
| 322 | raid->mnc += (buf[2] >> 4) * 10; |
| 323 | raid->mnc += (buf[1] >> 4) * 1; |
| 324 | } |
| 325 | |
| 326 | raid->lac = ntohs(*(uint16_t *)(buf + 3)); |
| 327 | raid->rac = buf[5]; |
| 328 | } |
Harald Welte | d4ea09f | 2010-05-01 14:27:23 +0200 | [diff] [blame^] | 329 | |
| 330 | int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid) |
| 331 | { |
| 332 | uint16_t mcc = raid->mcc; |
| 333 | uint16_t mnc = raid->mnc; |
| 334 | |
| 335 | buf[0] = ((mcc / 100) % 10) | (((mcc / 10) % 10) << 4); |
| 336 | buf[1] = (mcc % 10); |
| 337 | |
| 338 | /* I wonder who came up with the stupidity of encoding the MNC |
| 339 | * differently depending on how many digits its decimal number has! */ |
| 340 | if (mnc < 100) { |
| 341 | buf[1] |= 0xf0; |
| 342 | buf[2] = ((mnc / 10) % 10) | ((mnc % 10) << 4); |
| 343 | } else { |
| 344 | buf[1] |= (mnc % 10) << 4; |
| 345 | buf[2] = ((mnc / 100) % 10) | (((mcc / 10) % 10) << 4); |
| 346 | } |
| 347 | |
| 348 | *(uint16_t *)(buf+3) = htons(raid->lac); |
| 349 | |
| 350 | buf[5] = raid->rac; |
| 351 | |
| 352 | return 6; |
| 353 | } |