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 | |
Harald Welte | 9eb6d88 | 2010-03-25 12:00:54 +0800 | [diff] [blame] | 4 | /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [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> |
Max | fe65fa7 | 2016-05-10 17:17:05 +0200 | [diff] [blame] | 28 | #include <stdbool.h> |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 29 | #include <arpa/inet.h> |
| 30 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 31 | #include <osmocom/core/utils.h> |
| 32 | #include <osmocom/gsm/tlv.h> |
| 33 | #include <osmocom/gsm/gsm48.h> |
Harald Welte | 94df39e | 2011-06-26 14:33:57 +0200 | [diff] [blame] | 34 | #include <osmocom/gsm/gsm0502.h> |
Max | fb348ee | 2016-03-30 21:14:53 +0200 | [diff] [blame] | 35 | #include <osmocom/gsm/gsm_utils.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Harald Welte | 2aee7b1 | 2011-06-26 14:20:04 +0200 | [diff] [blame] | 37 | #include <osmocom/gsm/protocol/gsm_08_58.h> |
Max | fdca25d | 2016-07-05 16:06:28 +0200 | [diff] [blame] | 38 | #include <osmocom/gsm/protocol/gsm_04_08_gprs.h> |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 39 | |
| 40 | const struct tlv_definition gsm48_att_tlvdef = { |
| 41 | .def = { |
| 42 | [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV }, |
| 43 | [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV }, |
| 44 | [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV }, |
| 45 | [GSM48_IE_UTC] = { TLV_TYPE_TV }, |
| 46 | [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 }, |
| 47 | [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV }, |
| 48 | |
| 49 | [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV }, |
| 50 | [GSM48_IE_CAUSE] = { TLV_TYPE_TLV }, |
| 51 | [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV }, |
| 52 | [GSM48_IE_ALERT] = { TLV_TYPE_TLV }, |
| 53 | [GSM48_IE_FACILITY] = { TLV_TYPE_TLV }, |
| 54 | [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV }, |
| 55 | [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV }, |
| 56 | [GSM48_IE_NOTIFY] = { TLV_TYPE_TV }, |
| 57 | [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV }, |
| 58 | [GSM48_IE_SIGNAL] = { TLV_TYPE_TV }, |
| 59 | [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV }, |
| 60 | [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV }, |
| 61 | [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV }, |
| 62 | [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV }, |
| 63 | [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV }, |
| 64 | [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV }, |
| 65 | [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV }, |
| 66 | [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV }, |
| 67 | [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV }, |
| 68 | [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV }, |
| 69 | [GSM48_IE_USER_USER] = { TLV_TYPE_TLV }, |
| 70 | [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV }, |
| 71 | [GSM48_IE_MORE_DATA] = { TLV_TYPE_T }, |
| 72 | [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T }, |
| 73 | [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T }, |
| 74 | [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T }, |
| 75 | [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T }, |
| 76 | [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T }, |
| 77 | /* FIXME: more elements */ |
| 78 | }, |
| 79 | }; |
| 80 | |
Andreas Eversberg | 014cb87 | 2010-07-12 09:11:00 +0200 | [diff] [blame] | 81 | /* RR elements */ |
| 82 | const struct tlv_definition gsm48_rr_att_tlvdef = { |
| 83 | .def = { |
| 84 | /* NOTE: Don't add IE 17 = MOBILE_ID here, it already used. */ |
| 85 | [GSM48_IE_VGCS_TARGET] = { TLV_TYPE_TLV }, |
| 86 | [GSM48_IE_FRQSHORT_AFTER] = { TLV_TYPE_FIXED, 9 }, |
| 87 | [GSM48_IE_MUL_RATE_CFG] = { TLV_TYPE_TLV }, |
| 88 | [GSM48_IE_FREQ_L_AFTER] = { TLV_TYPE_TLV }, |
| 89 | [GSM48_IE_MSLOT_DESC] = { TLV_TYPE_TLV }, |
| 90 | [GSM48_IE_CHANMODE_2] = { TLV_TYPE_TV }, |
| 91 | [GSM48_IE_FRQSHORT_BEFORE] = { TLV_TYPE_FIXED, 9 }, |
| 92 | [GSM48_IE_CHANMODE_3] = { TLV_TYPE_TV }, |
| 93 | [GSM48_IE_CHANMODE_4] = { TLV_TYPE_TV }, |
| 94 | [GSM48_IE_CHANMODE_5] = { TLV_TYPE_TV }, |
| 95 | [GSM48_IE_CHANMODE_6] = { TLV_TYPE_TV }, |
| 96 | [GSM48_IE_CHANMODE_7] = { TLV_TYPE_TV }, |
| 97 | [GSM48_IE_CHANMODE_8] = { TLV_TYPE_TV }, |
| 98 | [GSM48_IE_FREQ_L_BEFORE] = { TLV_TYPE_TLV }, |
| 99 | [GSM48_IE_CH_DESC_1_BEFORE] = { TLV_TYPE_FIXED, 3 }, |
| 100 | [GSM48_IE_CH_DESC_2_BEFORE] = { TLV_TYPE_FIXED, 3 }, |
| 101 | [GSM48_IE_F_CH_SEQ_BEFORE] = { TLV_TYPE_FIXED, 9 }, |
| 102 | [GSM48_IE_CLASSMARK3] = { TLV_TYPE_TLV }, |
| 103 | [GSM48_IE_MA_BEFORE] = { TLV_TYPE_TLV }, |
| 104 | [GSM48_IE_RR_PACKET_UL] = { TLV_TYPE_TLV }, |
| 105 | [GSM48_IE_RR_PACKET_DL] = { TLV_TYPE_TLV }, |
| 106 | [GSM48_IE_CELL_CH_DESC] = { TLV_TYPE_FIXED, 16 }, |
| 107 | [GSM48_IE_CHANMODE_1] = { TLV_TYPE_TV }, |
| 108 | [GSM48_IE_CHDES_2_AFTER] = { TLV_TYPE_FIXED, 3 }, |
| 109 | [GSM48_IE_MODE_SEC_CH] = { TLV_TYPE_TV }, |
| 110 | [GSM48_IE_F_CH_SEQ_AFTER] = { TLV_TYPE_FIXED, 9 }, |
| 111 | [GSM48_IE_MA_AFTER] = { TLV_TYPE_TLV }, |
| 112 | [GSM48_IE_BA_RANGE] = { TLV_TYPE_TLV }, |
| 113 | [GSM48_IE_GROUP_CHDES] = { TLV_TYPE_TLV }, |
| 114 | [GSM48_IE_BA_LIST_PREF] = { TLV_TYPE_TLV }, |
| 115 | [GSM48_IE_MOB_OVSERV_DIF] = { TLV_TYPE_TLV }, |
| 116 | [GSM48_IE_REALTIME_DIFF] = { TLV_TYPE_TLV }, |
| 117 | [GSM48_IE_START_TIME] = { TLV_TYPE_FIXED, 2 }, |
| 118 | [GSM48_IE_TIMING_ADVANCE] = { TLV_TYPE_TV }, |
| 119 | [GSM48_IE_GROUP_CIP_SEQ] = { TLV_TYPE_SINGLE_TV }, |
| 120 | [GSM48_IE_CIP_MODE_SET] = { TLV_TYPE_SINGLE_TV }, |
| 121 | [GSM48_IE_GPRS_RESUMPT] = { TLV_TYPE_SINGLE_TV }, |
| 122 | [GSM48_IE_SYNC_IND] = { TLV_TYPE_SINGLE_TV }, |
| 123 | }, |
| 124 | }; |
| 125 | |
| 126 | /* MM elements */ |
| 127 | const struct tlv_definition gsm48_mm_att_tlvdef = { |
| 128 | .def = { |
| 129 | [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV }, |
| 130 | [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV }, |
| 131 | [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV }, |
| 132 | [GSM48_IE_UTC] = { TLV_TYPE_TV }, |
| 133 | [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 }, |
| 134 | [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV }, |
Jacob Erlbeck | 1c3f088 | 2013-09-16 10:29:57 +0200 | [diff] [blame] | 135 | [GSM48_IE_NET_DST] = { TLV_TYPE_TLV }, |
Andreas Eversberg | 014cb87 | 2010-07-12 09:11:00 +0200 | [diff] [blame] | 136 | |
| 137 | [GSM48_IE_LOCATION_AREA] = { TLV_TYPE_FIXED, 5 }, |
| 138 | [GSM48_IE_PRIORITY_LEV] = { TLV_TYPE_SINGLE_TV }, |
| 139 | [GSM48_IE_FOLLOW_ON_PROC] = { TLV_TYPE_T }, |
| 140 | [GSM48_IE_CTS_PERMISSION] = { TLV_TYPE_T }, |
| 141 | }, |
| 142 | }; |
| 143 | |
Harald Welte | e9e190a | 2010-03-25 11:44:57 +0800 | [diff] [blame] | 144 | static const struct value_string rr_cause_names[] = { |
| 145 | { GSM48_RR_CAUSE_NORMAL, "Normal event" }, |
| 146 | { GSM48_RR_CAUSE_ABNORMAL_UNSPEC, "Abnormal release, unspecified" }, |
| 147 | { GSM48_RR_CAUSE_ABNORMAL_UNACCT, "Abnormal release, channel unacceptable" }, |
| 148 | { GSM48_RR_CAUSE_ABNORMAL_TIMER, "Abnormal release, timer expired" }, |
| 149 | { GSM48_RR_CAUSE_ABNORMAL_NOACT, "Abnormal release, no activity on radio path" }, |
| 150 | { GSM48_RR_CAUSE_PREMPTIVE_REL, "Preemptive release" }, |
| 151 | { GSM48_RR_CAUSE_HNDOVER_IMP, "Handover impossible, timing advance out of range" }, |
| 152 | { GSM48_RR_CAUSE_CHAN_MODE_UNACCT, "Channel mode unacceptable" }, |
| 153 | { GSM48_RR_CAUSE_FREQ_NOT_IMPL, "Frequency not implemented" }, |
| 154 | { GSM48_RR_CAUSE_CALL_CLEARED, "Call already cleared" }, |
| 155 | { GSM48_RR_CAUSE_SEMANT_INCORR, "Semantically incorrect message" }, |
| 156 | { GSM48_RR_CAUSE_INVALID_MAND_INF, "Invalid mandatory information" }, |
| 157 | { GSM48_RR_CAUSE_MSG_TYPE_N, "Message type non-existant or not implemented" }, |
| 158 | { GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT, "Message type not compatible with protocol state" }, |
| 159 | { GSM48_RR_CAUSE_COND_IE_ERROR, "Conditional IE error" }, |
| 160 | { GSM48_RR_CAUSE_NO_CELL_ALLOC_A, "No cell allocation available" }, |
| 161 | { GSM48_RR_CAUSE_PROT_ERROR_UNSPC, "Protocol error unspecified" }, |
| 162 | { 0, NULL }, |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 163 | }; |
| 164 | |
Harald Welte | 9eb6d88 | 2010-03-25 12:00:54 +0800 | [diff] [blame] | 165 | /* FIXME: convert to value_string */ |
Andreas Eversberg | 014cb87 | 2010-07-12 09:11:00 +0200 | [diff] [blame] | 166 | static const char *cc_state_names[32] = { |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 167 | "NULL", |
| 168 | "INITIATED", |
Andreas Eversberg | 1ef041f | 2010-04-09 07:52:12 +0200 | [diff] [blame] | 169 | "MM_CONNECTION_PEND", |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 170 | "MO_CALL_PROC", |
| 171 | "CALL_DELIVERED", |
| 172 | "illegal state 5", |
| 173 | "CALL_PRESENT", |
| 174 | "CALL_RECEIVED", |
| 175 | "CONNECT_REQUEST", |
| 176 | "MO_TERM_CALL_CONF", |
| 177 | "ACTIVE", |
| 178 | "DISCONNECT_REQ", |
| 179 | "DISCONNECT_IND", |
| 180 | "illegal state 13", |
| 181 | "illegal state 14", |
| 182 | "illegal state 15", |
| 183 | "illegal state 16", |
| 184 | "illegal state 17", |
| 185 | "illegal state 18", |
| 186 | "RELEASE_REQ", |
| 187 | "illegal state 20", |
| 188 | "illegal state 21", |
| 189 | "illegal state 22", |
| 190 | "illegal state 23", |
| 191 | "illegal state 24", |
| 192 | "illegal state 25", |
| 193 | "MO_ORIG_MODIFY", |
| 194 | "MO_TERM_MODIFY", |
| 195 | "CONNECT_IND", |
| 196 | "illegal state 29", |
| 197 | "illegal state 30", |
| 198 | "illegal state 31", |
| 199 | }; |
| 200 | |
Harald Welte | 9eb6d88 | 2010-03-25 12:00:54 +0800 | [diff] [blame] | 201 | const char *gsm48_cc_state_name(uint8_t state) |
| 202 | { |
| 203 | if (state < ARRAY_SIZE(cc_state_names)) |
| 204 | return cc_state_names[state]; |
| 205 | |
| 206 | return "invalid"; |
| 207 | } |
| 208 | |
| 209 | static const struct value_string cc_msg_names[] = { |
| 210 | { GSM48_MT_CC_ALERTING, "ALERTING" }, |
| 211 | { GSM48_MT_CC_CALL_PROC, "CALL_PROC" }, |
| 212 | { GSM48_MT_CC_PROGRESS, "PROGRESS" }, |
| 213 | { GSM48_MT_CC_ESTAB, "ESTAB" }, |
| 214 | { GSM48_MT_CC_SETUP, "SETUP" }, |
| 215 | { GSM48_MT_CC_ESTAB_CONF, "ESTAB_CONF" }, |
| 216 | { GSM48_MT_CC_CONNECT, "CONNECT" }, |
| 217 | { GSM48_MT_CC_CALL_CONF, "CALL_CONF" }, |
| 218 | { GSM48_MT_CC_START_CC, "START_CC" }, |
| 219 | { GSM48_MT_CC_RECALL, "RECALL" }, |
| 220 | { GSM48_MT_CC_EMERG_SETUP, "EMERG_SETUP" }, |
| 221 | { GSM48_MT_CC_CONNECT_ACK, "CONNECT_ACK" }, |
| 222 | { GSM48_MT_CC_USER_INFO, "USER_INFO" }, |
| 223 | { GSM48_MT_CC_MODIFY_REJECT, "MODIFY_REJECT" }, |
| 224 | { GSM48_MT_CC_MODIFY, "MODIFY" }, |
| 225 | { GSM48_MT_CC_HOLD, "HOLD" }, |
| 226 | { GSM48_MT_CC_HOLD_ACK, "HOLD_ACK" }, |
| 227 | { GSM48_MT_CC_HOLD_REJ, "HOLD_REJ" }, |
| 228 | { GSM48_MT_CC_RETR, "RETR" }, |
| 229 | { GSM48_MT_CC_RETR_ACK, "RETR_ACK" }, |
| 230 | { GSM48_MT_CC_RETR_REJ, "RETR_REJ" }, |
| 231 | { GSM48_MT_CC_MODIFY_COMPL, "MODIFY_COMPL" }, |
| 232 | { GSM48_MT_CC_DISCONNECT, "DISCONNECT" }, |
| 233 | { GSM48_MT_CC_RELEASE_COMPL, "RELEASE_COMPL" }, |
| 234 | { GSM48_MT_CC_RELEASE, "RELEASE" }, |
| 235 | { GSM48_MT_CC_STOP_DTMF, "STOP_DTMF" }, |
| 236 | { GSM48_MT_CC_STOP_DTMF_ACK, "STOP_DTMF_ACK" }, |
| 237 | { GSM48_MT_CC_STATUS_ENQ, "STATUS_ENQ" }, |
| 238 | { GSM48_MT_CC_START_DTMF, "START_DTMF" }, |
| 239 | { GSM48_MT_CC_START_DTMF_ACK, "START_DTMF_ACK" }, |
| 240 | { GSM48_MT_CC_START_DTMF_REJ, "START_DTMF_REJ" }, |
| 241 | { GSM48_MT_CC_CONG_CTRL, "CONG_CTRL" }, |
| 242 | { GSM48_MT_CC_FACILITY, "FACILITY" }, |
| 243 | { GSM48_MT_CC_STATUS, "STATUS" }, |
| 244 | { GSM48_MT_CC_NOTIFY, "NOTFIY" }, |
| 245 | { 0, NULL } |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 246 | }; |
| 247 | |
Harald Welte | 9eb6d88 | 2010-03-25 12:00:54 +0800 | [diff] [blame] | 248 | const char *gsm48_cc_msg_name(uint8_t msgtype) |
| 249 | { |
| 250 | return get_value_string(cc_msg_names, msgtype); |
| 251 | } |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 252 | |
| 253 | const char *rr_cause_name(uint8_t cause) |
| 254 | { |
Harald Welte | e9e190a | 2010-03-25 11:44:57 +0800 | [diff] [blame] | 255 | return get_value_string(rr_cause_names, cause); |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Max | fb348ee | 2016-03-30 21:14:53 +0200 | [diff] [blame] | 258 | const struct value_string gsm48_chan_mode_names[] = { |
| 259 | { GSM48_CMODE_SIGN, "SIGNALLING" }, |
| 260 | { GSM48_CMODE_SPEECH_V1, "SPEECH_V1" }, |
| 261 | { GSM48_CMODE_SPEECH_EFR, "SPEECH_EFR" }, |
| 262 | { GSM48_CMODE_SPEECH_AMR, "SPEECH_AMR" }, |
| 263 | { GSM48_CMODE_DATA_14k5, "DATA_14k5" }, |
| 264 | { GSM48_CMODE_DATA_12k0, "DATA_12k0" }, |
| 265 | { GSM48_CMODE_DATA_6k0, "DATA_6k0" }, |
| 266 | { GSM48_CMODE_DATA_3k6, "DATA_3k6" }, |
| 267 | { 0, NULL }, |
| 268 | }; |
| 269 | |
| 270 | const struct value_string gsm_chan_t_names[] = { |
| 271 | { GSM_LCHAN_NONE, "NONE" }, |
| 272 | { GSM_LCHAN_SDCCH, "SDCCH" }, |
| 273 | { GSM_LCHAN_TCH_F, "TCH_F" }, |
| 274 | { GSM_LCHAN_TCH_H, "TCH_H" }, |
| 275 | { GSM_LCHAN_UNKNOWN, "UNKNOWN" }, |
| 276 | { GSM_LCHAN_CCCH, "CCCH" }, |
| 277 | { GSM_LCHAN_PDTCH, "PDTCH" }, |
| 278 | { GSM_LCHAN_CBCH, "CBCH" }, |
| 279 | { 0, NULL }, |
| 280 | }; |
| 281 | |
Harald Welte | 1a8c4e0 | 2015-08-16 17:56:25 +0200 | [diff] [blame] | 282 | static const struct value_string mi_type_names[] = { |
| 283 | { GSM_MI_TYPE_NONE, "NONE" }, |
| 284 | { GSM_MI_TYPE_IMSI, "IMSI" }, |
| 285 | { GSM_MI_TYPE_IMEI, "IMEI" }, |
| 286 | { GSM_MI_TYPE_IMEISV, "IMEI-SV" }, |
| 287 | { GSM_MI_TYPE_TMSI, "TMSI" }, |
| 288 | { 0, NULL } |
| 289 | }; |
| 290 | |
| 291 | const char *gsm48_mi_type_name(uint8_t mi) |
| 292 | { |
| 293 | return get_value_string(mi_type_names, mi); |
| 294 | } |
| 295 | |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 296 | static void to_bcd(uint8_t *bcd, uint16_t val) |
| 297 | { |
| 298 | bcd[2] = val % 10; |
| 299 | val = val / 10; |
| 300 | bcd[1] = val % 10; |
| 301 | val = val / 10; |
| 302 | bcd[0] = val % 10; |
| 303 | val = val / 10; |
| 304 | } |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 305 | |
Max | fdca25d | 2016-07-05 16:06:28 +0200 | [diff] [blame] | 306 | /*! \brief Checks is particular message is cipherable in A/Gb mode according to |
| 307 | * 3GPP TS 24.008 § 4.7.1.2 |
| 308 | * \param[in] hdr Message header |
| 309 | * \return true if message can be encrypted, false otherwise |
| 310 | */ |
| 311 | bool gsm48_hdr_gmm_cipherable(const struct gsm48_hdr *hdr) |
| 312 | { |
| 313 | switch(hdr->msg_type) { |
| 314 | case GSM48_MT_GMM_ATTACH_REQ: |
| 315 | case GSM48_MT_GMM_ATTACH_REJ: |
| 316 | case GSM48_MT_GMM_AUTH_CIPH_REQ: |
| 317 | case GSM48_MT_GMM_AUTH_CIPH_RESP: |
| 318 | case GSM48_MT_GMM_AUTH_CIPH_REJ: |
| 319 | case GSM48_MT_GMM_AUTH_CIPH_FAIL: |
| 320 | case GSM48_MT_GMM_ID_REQ: |
| 321 | case GSM48_MT_GMM_ID_RESP: |
| 322 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 323 | case GSM48_MT_GMM_RA_UPD_REJ: |
| 324 | return false; |
| 325 | default: |
| 326 | return true; |
| 327 | } |
| 328 | } |
| 329 | |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 330 | /* Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an |
| 331 | * allocated buffer of (at least) 3 bytes length. */ |
| 332 | void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc) |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 333 | { |
| 334 | uint8_t bcd[3]; |
| 335 | |
| 336 | to_bcd(bcd, mcc); |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 337 | bcd_dst[0] = bcd[0] | (bcd[1] << 4); |
| 338 | bcd_dst[1] = bcd[2]; |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 339 | |
| 340 | to_bcd(bcd, mnc); |
| 341 | /* FIXME: do we need three-digit MNC? See Table 10.5.3 */ |
Andreas Eversberg | ed00fe4 | 2010-07-12 09:12:46 +0200 | [diff] [blame] | 342 | if (mnc > 99) { |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 343 | bcd_dst[1] |= bcd[2] << 4; |
| 344 | bcd_dst[2] = bcd[0] | (bcd[1] << 4); |
Andreas Eversberg | ed00fe4 | 2010-07-12 09:12:46 +0200 | [diff] [blame] | 345 | } else { |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 346 | bcd_dst[1] |= 0xf << 4; |
| 347 | bcd_dst[2] = bcd[1] | (bcd[2] << 4); |
Andreas Eversberg | ed00fe4 | 2010-07-12 09:12:46 +0200 | [diff] [blame] | 348 | } |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 349 | } |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 350 | |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 351 | /* Convert given 3-byte BCD buffer to integers and write results to *mcc and |
| 352 | * *mnc. The first three BCD digits result in the MCC and the remaining ones in |
| 353 | * the MNC. */ |
| 354 | void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc) |
| 355 | { |
| 356 | *mcc = (bcd_src[0] & 0x0f) * 100 |
| 357 | + (bcd_src[0] >> 4) * 10 |
| 358 | + (bcd_src[1] & 0x0f); |
| 359 | |
| 360 | if ((bcd_src[1] & 0xf0) == 0xf0) { |
| 361 | *mnc = (bcd_src[2] & 0x0f) * 10 |
| 362 | + (bcd_src[2] >> 4); |
| 363 | } else { |
| 364 | *mnc = (bcd_src[2] & 0x0f) * 100 |
| 365 | + (bcd_src[2] >> 4) * 10 |
| 366 | + (bcd_src[1] >> 4); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc, |
| 371 | uint16_t mnc, uint16_t lac) |
| 372 | { |
| 373 | gsm48_mcc_mnc_to_bcd(&lai48->digits[0], mcc, mnc); |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 374 | lai48->lac = htons(lac); |
| 375 | } |
| 376 | |
Neels Hofmeyr | f20f5fd | 2016-02-29 09:56:39 +0100 | [diff] [blame] | 377 | /* Attention: this function returns true integers, not hex! */ |
Harald Welte | 774a9de | 2012-07-13 21:35:13 +0200 | [diff] [blame] | 378 | int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc, |
| 379 | uint16_t *mnc, uint16_t *lac) |
| 380 | { |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 381 | gsm48_mcc_mnc_from_bcd(&lai->digits[0], mcc, mnc); |
Harald Welte | 774a9de | 2012-07-13 21:35:13 +0200 | [diff] [blame] | 382 | *lac = ntohs(lai->lac); |
Harald Welte | 774a9de | 2012-07-13 21:35:13 +0200 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Max | fe65fa7 | 2016-05-10 17:17:05 +0200 | [diff] [blame] | 386 | /*! \brief Set DTX mode in Cell Options IE (3GPP TS 44.018) |
| 387 | * \param[in] op Cell Options structure in which DTX parameters will be set |
| 388 | * \param[in] full Mode for full-rate channels |
| 389 | * \param[in] half Mode for half-rate channels |
| 390 | * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of |
| 391 | * 10.5.2.3a.2 |
| 392 | * |
| 393 | * There is no space for separate DTX settings for Full and Half rate channels |
| 394 | * in BCCH - in this case full setting is used for both and half parameter is |
| 395 | * ignored. |
| 396 | */ |
| 397 | void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full, |
| 398 | enum gsm48_dtx_mode half, bool is_bcch) |
| 399 | { |
| 400 | if (is_bcch) { |
| 401 | switch (full) { |
| 402 | case GSM48_DTX_MAY_BE_USED: |
| 403 | op->dtx = 0; |
| 404 | return; |
| 405 | case GSM48_DTX_SHALL_BE_USED: |
| 406 | op->dtx = 1; |
| 407 | return; |
| 408 | case GSM48_DTX_SHALL_NOT_BE_USED: |
| 409 | op->dtx = 2; |
| 410 | return; |
| 411 | } |
| 412 | } else { |
| 413 | switch (full) { |
| 414 | case GSM48_DTX_MAY_BE_USED: |
| 415 | op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0; |
| 416 | op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1; |
| 417 | return; |
| 418 | case GSM48_DTX_SHALL_BE_USED: |
| 419 | op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1; |
| 420 | op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0; |
| 421 | return; |
| 422 | case GSM48_DTX_SHALL_NOT_BE_USED: |
| 423 | op->dtx = 2; |
| 424 | op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0; |
| 425 | return; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 430 | int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi) |
| 431 | { |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 432 | uint32_t tmsi_be = htonl(tmsi); |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 433 | |
| 434 | buf[0] = GSM48_IE_MOBILE_ID; |
| 435 | buf[1] = GSM48_TMSI_LEN; |
| 436 | buf[2] = 0xf0 | GSM_MI_TYPE_TMSI; |
Holger Hans Peter Freyther | cd252e3 | 2013-07-03 09:56:53 +0200 | [diff] [blame] | 437 | memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be)); |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 438 | |
| 439 | return 7; |
| 440 | } |
| 441 | |
| 442 | int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi) |
| 443 | { |
| 444 | unsigned int length = strlen(imsi), i, off = 0; |
| 445 | uint8_t odd = (length & 0x1) == 1; |
| 446 | |
| 447 | buf[0] = GSM48_IE_MOBILE_ID; |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 448 | buf[2] = osmo_char2bcd(imsi[0]) << 4 | GSM_MI_TYPE_IMSI | (odd << 3); |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 449 | |
| 450 | /* if the length is even we will fill half of the last octet */ |
| 451 | if (odd) |
| 452 | buf[1] = (length + 1) >> 1; |
| 453 | else |
| 454 | buf[1] = (length + 2) >> 1; |
| 455 | |
| 456 | for (i = 1; i < buf[1]; ++i) { |
| 457 | uint8_t lower, upper; |
| 458 | |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 459 | lower = osmo_char2bcd(imsi[++off]); |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 460 | if (!odd && off + 1 == length) |
| 461 | upper = 0x0f; |
| 462 | else |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 463 | upper = osmo_char2bcd(imsi[++off]) & 0x0f; |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 464 | |
| 465 | buf[2 + i] = (upper << 4) | lower; |
| 466 | } |
| 467 | |
| 468 | return 2 + buf[1]; |
| 469 | } |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 470 | |
| 471 | /* Convert Mobile Identity (10.5.1.4) to string */ |
Harald Welte | 163d0ea | 2010-04-09 07:57:40 +0200 | [diff] [blame] | 472 | int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi, |
| 473 | const int mi_len) |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 474 | { |
| 475 | int i; |
Harald Welte | 163d0ea | 2010-04-09 07:57:40 +0200 | [diff] [blame] | 476 | uint8_t mi_type; |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 477 | char *str_cur = string; |
Harald Welte | 163d0ea | 2010-04-09 07:57:40 +0200 | [diff] [blame] | 478 | uint32_t tmsi; |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 479 | |
| 480 | mi_type = mi[0] & GSM_MI_TYPE_MASK; |
| 481 | |
| 482 | switch (mi_type) { |
| 483 | case GSM_MI_TYPE_NONE: |
| 484 | break; |
| 485 | case GSM_MI_TYPE_TMSI: |
Holger Freyther | 45abec2 | 2016-05-20 19:21:27 +0000 | [diff] [blame] | 486 | /* Table 10.5.4.3, reverse generate_mid_from_tmsi */ |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 487 | if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) { |
| 488 | memcpy(&tmsi, &mi[1], 4); |
| 489 | tmsi = ntohl(tmsi); |
Holger Freyther | 45abec2 | 2016-05-20 19:21:27 +0000 | [diff] [blame] | 490 | return snprintf(string, str_len, "%u", tmsi); |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 491 | } |
| 492 | break; |
| 493 | case GSM_MI_TYPE_IMSI: |
| 494 | case GSM_MI_TYPE_IMEI: |
| 495 | case GSM_MI_TYPE_IMEISV: |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 496 | *str_cur++ = osmo_bcd2char(mi[0] >> 4); |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 497 | |
| 498 | for (i = 1; i < mi_len; i++) { |
| 499 | if (str_cur + 2 >= string + str_len) |
| 500 | return str_cur - string; |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 501 | *str_cur++ = osmo_bcd2char(mi[i] & 0xf); |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 502 | /* skip last nibble in last input byte when GSM_EVEN */ |
| 503 | if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD)) |
Pablo Neira Ayuso | 87f7b25 | 2011-05-07 12:43:08 +0200 | [diff] [blame] | 504 | *str_cur++ = osmo_bcd2char(mi[i] >> 4); |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 505 | } |
| 506 | break; |
| 507 | default: |
| 508 | break; |
| 509 | } |
| 510 | *str_cur++ = '\0'; |
| 511 | |
| 512 | return str_cur - string; |
| 513 | } |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 514 | |
| 515 | void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf) |
| 516 | { |
| 517 | raid->mcc = (buf[0] & 0xf) * 100; |
| 518 | raid->mcc += (buf[0] >> 4) * 10; |
| 519 | raid->mcc += (buf[1] & 0xf) * 1; |
| 520 | |
| 521 | /* I wonder who came up with the stupidity of encoding the MNC |
| 522 | * differently depending on how many digits its decimal number has! */ |
| 523 | if ((buf[1] >> 4) == 0xf) { |
| 524 | raid->mnc = (buf[2] & 0xf) * 10; |
| 525 | raid->mnc += (buf[2] >> 4) * 1; |
| 526 | } else { |
| 527 | raid->mnc = (buf[2] & 0xf) * 100; |
| 528 | raid->mnc += (buf[2] >> 4) * 10; |
| 529 | raid->mnc += (buf[1] >> 4) * 1; |
| 530 | } |
| 531 | |
| 532 | raid->lac = ntohs(*(uint16_t *)(buf + 3)); |
| 533 | raid->rac = buf[5]; |
| 534 | } |
Harald Welte | 35a9394 | 2010-05-01 14:25:22 +0200 | [diff] [blame] | 535 | |
| 536 | int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid) |
| 537 | { |
| 538 | uint16_t mcc = raid->mcc; |
| 539 | uint16_t mnc = raid->mnc; |
Andreas Eversberg | d430ef4 | 2012-11-05 10:47:59 +0100 | [diff] [blame] | 540 | uint16_t _lac; |
Harald Welte | 35a9394 | 2010-05-01 14:25:22 +0200 | [diff] [blame] | 541 | |
| 542 | buf[0] = ((mcc / 100) % 10) | (((mcc / 10) % 10) << 4); |
| 543 | buf[1] = (mcc % 10); |
| 544 | |
| 545 | /* I wonder who came up with the stupidity of encoding the MNC |
| 546 | * differently depending on how many digits its decimal number has! */ |
| 547 | if (mnc < 100) { |
| 548 | buf[1] |= 0xf0; |
| 549 | buf[2] = ((mnc / 10) % 10) | ((mnc % 10) << 4); |
| 550 | } else { |
| 551 | buf[1] |= (mnc % 10) << 4; |
Gus Bourg | 4b6bc3b | 2011-12-14 23:20:21 +0100 | [diff] [blame] | 552 | buf[2] = ((mnc / 100) % 10) | (((mnc / 10) % 10) << 4); |
Harald Welte | 35a9394 | 2010-05-01 14:25:22 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Andreas Eversberg | d430ef4 | 2012-11-05 10:47:59 +0100 | [diff] [blame] | 555 | _lac = htons(raid->lac); |
| 556 | memcpy(buf + 3, &_lac, 2); |
Harald Welte | 35a9394 | 2010-05-01 14:25:22 +0200 | [diff] [blame] | 557 | |
| 558 | buf[5] = raid->rac; |
| 559 | |
| 560 | return 6; |
| 561 | } |
Harald Welte | 2aee7b1 | 2011-06-26 14:20:04 +0200 | [diff] [blame] | 562 | |
| 563 | /* From Table 10.5.33 of GSM 04.08 */ |
| 564 | int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc) |
| 565 | { |
Harald Welte | 94df39e | 2011-06-26 14:33:57 +0200 | [diff] [blame] | 566 | unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc); |
| 567 | |
| 568 | if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C) |
| 569 | return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2); |
| 570 | else |
| 571 | return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2); |
Harald Welte | 2aee7b1 | 2011-06-26 14:20:04 +0200 | [diff] [blame] | 572 | } |