Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [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 | |
| 4 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 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 | |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 29 | #include <arpa/inet.h> |
| 30 | |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 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 | |
| 78 | static const char *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 | }; |
| 97 | |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 98 | const char *cc_state_names[32] = { |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 99 | "NULL", |
| 100 | "INITIATED", |
| 101 | "illegal state 2", |
| 102 | "MO_CALL_PROC", |
| 103 | "CALL_DELIVERED", |
| 104 | "illegal state 5", |
| 105 | "CALL_PRESENT", |
| 106 | "CALL_RECEIVED", |
| 107 | "CONNECT_REQUEST", |
| 108 | "MO_TERM_CALL_CONF", |
| 109 | "ACTIVE", |
| 110 | "DISCONNECT_REQ", |
| 111 | "DISCONNECT_IND", |
| 112 | "illegal state 13", |
| 113 | "illegal state 14", |
| 114 | "illegal state 15", |
| 115 | "illegal state 16", |
| 116 | "illegal state 17", |
| 117 | "illegal state 18", |
| 118 | "RELEASE_REQ", |
| 119 | "illegal state 20", |
| 120 | "illegal state 21", |
| 121 | "illegal state 22", |
| 122 | "illegal state 23", |
| 123 | "illegal state 24", |
| 124 | "illegal state 25", |
| 125 | "MO_ORIG_MODIFY", |
| 126 | "MO_TERM_MODIFY", |
| 127 | "CONNECT_IND", |
| 128 | "illegal state 29", |
| 129 | "illegal state 30", |
| 130 | "illegal state 31", |
| 131 | }; |
| 132 | |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 133 | const char *gsm48_cc_msg_names[0x40] = { |
| 134 | "unknown 0x00", |
| 135 | "ALERTING", |
| 136 | "CALL_PROC", |
| 137 | "PROGRESS", |
| 138 | "ESTAB", |
| 139 | "SETUP", |
| 140 | "ESTAB_CONF", |
| 141 | "CONNECT", |
| 142 | "CALL_CONF", |
| 143 | "START_CC", |
| 144 | "unknown 0x0a", |
| 145 | "RECALL", |
| 146 | "unknown 0x0c", |
| 147 | "unknown 0x0d", |
| 148 | "EMERG_SETUP", |
| 149 | "CONNECT_ACK", |
| 150 | "USER_INFO", |
| 151 | "unknown 0x11", |
| 152 | "unknown 0x12", |
| 153 | "MODIFY_REJECT", |
| 154 | "unknown 0x14", |
| 155 | "unknown 0x15", |
| 156 | "unknown 0x16", |
| 157 | "MODIFY", |
| 158 | "HOLD", |
| 159 | "HOLD_ACK", |
| 160 | "HOLD_REJ", |
| 161 | "unknown 0x1b", |
| 162 | "RETR", |
| 163 | "RETR_ACK", |
| 164 | "RETR_REJ", |
| 165 | "MODIFY_COMPL", |
| 166 | "unknown 0x20", |
| 167 | "unknown 0x21", |
| 168 | "unknown 0x22", |
| 169 | "unknown 0x23", |
| 170 | "unknown 0x24", |
| 171 | "DISCONNECT", |
| 172 | "unknown 0x26", |
| 173 | "unknown 0x27", |
| 174 | "unknown 0x28", |
| 175 | "unknown 0x29", |
| 176 | "RELEASE_COMPL", |
| 177 | "unknown 0x2b", |
| 178 | "unknown 0x2c", |
| 179 | "RELEASE", |
| 180 | "unknown 0x2e", |
| 181 | "unknown 0x2f", |
| 182 | "unknown 0x30", |
| 183 | "STOP_DTMF", |
| 184 | "STOP_DTMF_ACK", |
| 185 | "unknown 0x33", |
| 186 | "STATUS_ENQ", |
| 187 | "START_DTMF", |
| 188 | "START_DTMF_ACK", |
| 189 | "START_DTMF_REJ", |
| 190 | "unknown 0x38", |
| 191 | "CONG_CTRL", |
| 192 | "FACILITY", |
| 193 | "unknown 0x3b", |
| 194 | "STATUS", |
| 195 | "unknown 0x3d", |
| 196 | "NOTIFY", |
| 197 | "unknown 0x3f", |
| 198 | }; |
| 199 | |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 200 | static char strbuf[64]; |
| 201 | |
| 202 | const char *rr_cause_name(uint8_t cause) |
| 203 | { |
| 204 | if (cause < ARRAY_SIZE(rr_cause_names) && |
| 205 | rr_cause_names[cause]) |
| 206 | return rr_cause_names[cause]; |
| 207 | |
| 208 | snprintf(strbuf, sizeof(strbuf), "0x%02x", cause); |
| 209 | return strbuf; |
| 210 | } |
| 211 | |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 212 | static void to_bcd(uint8_t *bcd, uint16_t val) |
| 213 | { |
| 214 | bcd[2] = val % 10; |
| 215 | val = val / 10; |
| 216 | bcd[1] = val % 10; |
| 217 | val = val / 10; |
| 218 | bcd[0] = val % 10; |
| 219 | val = val / 10; |
| 220 | } |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 221 | |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 222 | void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc, |
| 223 | uint16_t mnc, uint16_t lac) |
| 224 | { |
| 225 | uint8_t bcd[3]; |
| 226 | |
| 227 | to_bcd(bcd, mcc); |
| 228 | lai48->digits[0] = bcd[0] | (bcd[1] << 4); |
| 229 | lai48->digits[1] = bcd[2]; |
| 230 | |
| 231 | to_bcd(bcd, mnc); |
| 232 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
| 233 | #if 0 |
| 234 | lai48->digits[1] |= bcd[2] << 4; |
| 235 | lai48->digits[2] = bcd[0] | (bcd[1] << 4); |
| 236 | #else |
| 237 | lai48->digits[1] |= 0xf << 4; |
| 238 | lai48->digits[2] = bcd[1] | (bcd[2] << 4); |
| 239 | #endif |
| 240 | |
| 241 | lai48->lac = htons(lac); |
| 242 | } |
| 243 | |
| 244 | int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi) |
| 245 | { |
| 246 | uint32_t *tptr = (uint32_t *) &buf[3]; |
| 247 | |
| 248 | buf[0] = GSM48_IE_MOBILE_ID; |
| 249 | buf[1] = GSM48_TMSI_LEN; |
| 250 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
| 251 | *tptr = htonl(tmsi); |
| 252 | |
| 253 | return 7; |
| 254 | } |
| 255 | |
| 256 | int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi) |
| 257 | { |
| 258 | unsigned int length = strlen(imsi), i, off = 0; |
| 259 | uint8_t odd = (length & 0x1) == 1; |
| 260 | |
| 261 | buf[0] = GSM48_IE_MOBILE_ID; |
| 262 | buf[2] = char2bcd(imsi[0]) << 4 | GSM_MI_TYPE_IMSI | (odd << 3); |
| 263 | |
| 264 | /* if the length is even we will fill half of the last octet */ |
| 265 | if (odd) |
| 266 | buf[1] = (length + 1) >> 1; |
| 267 | else |
| 268 | buf[1] = (length + 2) >> 1; |
| 269 | |
| 270 | for (i = 1; i < buf[1]; ++i) { |
| 271 | uint8_t lower, upper; |
| 272 | |
| 273 | lower = char2bcd(imsi[++off]); |
| 274 | if (!odd && off + 1 == length) |
| 275 | upper = 0x0f; |
| 276 | else |
| 277 | upper = char2bcd(imsi[++off]) & 0x0f; |
| 278 | |
| 279 | buf[2 + i] = (upper << 4) | lower; |
| 280 | } |
| 281 | |
| 282 | return 2 + buf[1]; |
| 283 | } |