Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 1 | /* Encoding/Decoding routines for GSM System Information messages |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 2 | * according to 3GPP TS 44.018 Version 12.3.0 Release 12 |
| 3 | * |
| 4 | * (C) 2018 Harald Welte <laforge@gnumonks.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Released under the terms of GNU General Public License, Version 2 or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 11 | */ |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 12 | |
| 13 | module GSM_SystemInformation { |
| 14 | |
| 15 | import from General_Types all; |
Harald Welte | a082a69 | 2017-07-15 15:58:13 +0200 | [diff] [blame] | 16 | import from GSM_Types all; |
Harald Welte | f976492 | 2017-08-20 22:47:44 +0200 | [diff] [blame] | 17 | import from GSM_RR_Types all; |
Neels Hofmeyr | a5f0ed2 | 2020-07-06 02:39:40 +0200 | [diff] [blame] | 18 | import from GSM_RestOctets all; |
Harald Welte | 615bd54 | 2017-07-14 18:28:55 +0200 | [diff] [blame] | 19 | import from Osmocom_Types all; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 20 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 21 | type union ArfcnOrMaio { |
| 22 | uint12_t arfcn, |
| 23 | MaioHsn maio_hsn |
| 24 | } with { variant "" }; |
| 25 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 26 | /* 24.008 10.5.1.1 */ |
Harald Welte | 484160b | 2017-07-28 13:30:24 +0200 | [diff] [blame] | 27 | type uint16_t SysinfoCellIdentity; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 28 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 29 | /* 44.018 10.5.2.1b */ |
| 30 | type octetstring CellChannelDescription with { variant "FIELDLENGTH(16)" }; |
| 31 | |
| 32 | /* 44.018 10.5.2.3 */ |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 33 | type enumerated CellOptions_DTX { |
| 34 | MS_MAY_USE_UL_DTX ('00'B), |
| 35 | MS_SHALL_USE_UL_DTX ('01'B), |
| 36 | MS_SHALL_NOT_USE_UL_DTX ('10'B) |
| 37 | } with { variant "FIELDLENGTH(2)" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 38 | type record CellOptions { |
| 39 | boolean dn_ind, |
| 40 | boolean pwrc, |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 41 | CellOptions_DTX dtx, |
| 42 | uint4_t radio_link_tout_div4 |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 43 | } with { variant "" }; |
| 44 | |
| 45 | /* 44.018 10.5.2.3a */ |
| 46 | type record CellOptionsSacch { |
| 47 | BIT1 dtx_ext, |
| 48 | boolean pwrc, |
| 49 | BIT2 dtx, |
| 50 | BIT4 radio_link_timeout |
| 51 | } with { variant "" }; |
| 52 | |
| 53 | /* 44.018 10.5.2.4 */ |
| 54 | type record CellSelectionParameters { |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 55 | uint3_t cell_resel_hyst_2dB, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 56 | uint5_t ms_txpwr_max_cch, |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 57 | BIT1 acs, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 58 | boolean neci, |
| 59 | uint6_t rxlev_access_min |
| 60 | } with { variant "" }; |
| 61 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 62 | /* 44.018 10.5.2.11 */ |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 63 | type enumerated CtrlChanDesc_CC { |
| 64 | CCHAN_DESC_1CCCH_NOT_COMBINED ('000'B), |
| 65 | CCHAN_DESC_1CCCH_COMBINED ('001'B), |
| 66 | CCHAN_DESC_2CCCH_NOT_COMBINED ('010'B), |
| 67 | CCHAN_DESC_3CCCH_NOT_COMBINED ('100'B), |
| 68 | CCHAN_DESC_4CCCH_NOT_COMBINED ('110'B) |
| 69 | } with { variant "FIELDLENGTH(3)" }; |
| 70 | type enumerated CBQ3 { |
| 71 | CBQ3_IU_MODE_NOT_SUPPORTED ('00'B), |
| 72 | CBQ3_IU_MODE_MS_BARRED ('01'B), |
| 73 | CBQ3_IU_MODE_NOT_BARRED ('10'B) |
| 74 | } with { variant "FIELDLENGTH(2)" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 75 | type record ControlChannelDescription { |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 76 | boolean msc_r99, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 77 | boolean att, |
| 78 | uint3_t bs_ag_blks_res, |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 79 | CtrlChanDesc_CC ccch_conf, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 80 | boolean si22ind, |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 81 | CBQ3 cbq3, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 82 | BIT2 spare, |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 83 | uint3_t bs_pa_mfrms, /* off by 2 */ |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 84 | uint8_t t3212 |
| 85 | } with { variant "" }; |
| 86 | |
Harald Welte | 162b160 | 2017-07-16 03:05:02 +0200 | [diff] [blame] | 87 | template ControlChannelDescription t_ControlChannelDescription := { ?, ?, ?, ?, ?, ?, '00'B, ?, ? }; |
| 88 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 89 | /* 44.018 10.5.2.22 */ |
| 90 | type octetstring NeighbourCellDescription with { variant "FIELDLENGTH(16)" }; |
| 91 | |
| 92 | /* 44.018 10.5.2.22a */ |
| 93 | type octetstring NeighbourCellDescription2 with { variant "FIELDLENGTH(16)" }; |
| 94 | |
| 95 | type bitstring AccessControlClass with { variant "FIELDLENGTH(16), BYTEORDER(last)" }; |
| 96 | |
| 97 | /* 44.018 10.5.2.29 */ |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 98 | type enumerated RachCtrlPar_MR { |
| 99 | RACH_MAX_RETRANS_1 ('00'B), |
| 100 | RACH_MAX_RETRANS_2 ('01'B), |
| 101 | RACH_MAX_RETRANS_4 ('10'B), |
| 102 | RACH_MAX_RETRANS_7 ('11'B) |
| 103 | } with { variant "FIELDLENGTH(2)" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 104 | type record RachControlParameters { |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 105 | RachCtrlPar_MR max_retrans, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 106 | BIT4 tx_integer, |
| 107 | boolean cell_barr_access, |
Harald Welte | 82ccef7 | 2018-02-25 16:17:33 +0100 | [diff] [blame] | 108 | boolean re_not_allowed, |
| 109 | AccessControlClass acc |
| 110 | } with { variant (acc) "FIELDLENGTH(16)" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 111 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 112 | /* 44.018 9.1.31 */ |
| 113 | type record SystemInformationType1 { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 114 | CellChannelDescription cell_chan_desc, |
| 115 | RachControlParameters rach_control, |
Vadim Yanitskiy | b7bc2e6 | 2020-05-04 21:22:41 +0700 | [diff] [blame] | 116 | RestOctets rest_octets length(0..1) |
| 117 | } with { variant "" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 118 | |
| 119 | /* 44.018 9.1.32 */ |
| 120 | type record SystemInformationType2 { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 121 | NeighbourCellDescription bcch_freq_list, |
| 122 | BIT8 ncc_permitted, |
| 123 | RachControlParameters rach_control |
| 124 | } with { variant "" }; |
| 125 | |
| 126 | /* 44.018 9.1.33 */ |
| 127 | type record SystemInformationType2bis { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 128 | NeighbourCellDescription extd_bcch_freq_list, |
| 129 | RachControlParameters rach_control, |
Vadim Yanitskiy | b7bc2e6 | 2020-05-04 21:22:41 +0700 | [diff] [blame] | 130 | RestOctets rest_octets length(0..1) |
| 131 | } with { variant "" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 132 | |
| 133 | /* 44.018 9.1.34 */ |
| 134 | type record SystemInformationType2ter { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 135 | NeighbourCellDescription2 extd_bcch_freq_list, |
Vadim Yanitskiy | b7bc2e6 | 2020-05-04 21:22:41 +0700 | [diff] [blame] | 136 | RestOctets rest_octets length(0..4) |
| 137 | } with { variant "" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 138 | |
Neels Hofmeyr | bb27774 | 2020-07-06 02:39:58 +0200 | [diff] [blame] | 139 | type record SystemInformationType2quater { |
| 140 | SI2quaterRestOctets rest_octets |
| 141 | } with { variant "" }; |
| 142 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 143 | /* 44.018 9.1.35 */ |
| 144 | type record SystemInformationType3 { |
Harald Welte | 484160b | 2017-07-28 13:30:24 +0200 | [diff] [blame] | 145 | SysinfoCellIdentity cell_id, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 146 | LocationAreaIdentification lai, |
| 147 | ControlChannelDescription ctrl_chan_desc, |
| 148 | CellOptions cell_options, |
| 149 | CellSelectionParameters cell_sel_par, |
| 150 | RachControlParameters rach_control, |
Neels Hofmeyr | a5f0ed2 | 2020-07-06 02:39:40 +0200 | [diff] [blame] | 151 | SI3RestOctets rest_octets |
Vadim Yanitskiy | b7bc2e6 | 2020-05-04 21:22:41 +0700 | [diff] [blame] | 152 | } with { variant "" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 153 | |
Harald Welte | 162b160 | 2017-07-16 03:05:02 +0200 | [diff] [blame] | 154 | template SystemInformationType3 t_SI3 := { |
| 155 | cell_id := ?, |
| 156 | lai := ?, |
| 157 | ctrl_chan_desc := t_ControlChannelDescription, |
| 158 | cell_options := ?, |
| 159 | cell_sel_par := ?, |
| 160 | rach_control := ?, |
| 161 | rest_octets := ? |
| 162 | }; |
| 163 | |
| 164 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 165 | /* 44.018 9.1.36 */ |
| 166 | type record SystemInformationType4 { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 167 | LocationAreaIdentification lai, |
| 168 | CellSelectionParameters cell_sel_par, |
| 169 | RachControlParameters rach_control, |
Harald Welte | ad091b6 | 2017-07-16 21:03:17 +0200 | [diff] [blame] | 170 | ChannelDescriptionTV cbch_chan_desc optional, |
Vadim Yanitskiy | 74e1f6b | 2020-08-25 03:59:45 +0700 | [diff] [blame] | 171 | MobileAllocationTLV cbch_mobile_alloc optional, |
Neels Hofmeyr | a5f0ed2 | 2020-07-06 02:39:40 +0200 | [diff] [blame] | 172 | SI4RestOctets rest_octets /* see 10.5.2.35 */ |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 173 | } with { variant "TAG(cbch_chan_desc, iei = '64'O; cbch_mobile_alloc, iei = '72'O)" }; |
| 174 | |
| 175 | /* 44.018 9.1.37 */ |
| 176 | type record SystemInformationType5 { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 177 | NeighbourCellDescription bcch_freq_list |
| 178 | } with { variant "" }; |
| 179 | |
| 180 | /* 44.018 9.1.38 */ |
| 181 | type record SystemInformationType5bis { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 182 | NeighbourCellDescription extd_bcch_freq_list |
| 183 | } with { variant "" }; |
| 184 | |
| 185 | /* 44.018 9.1.39 */ |
| 186 | type record SystemInformationType5ter { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 187 | NeighbourCellDescription2 extd_bcch_freq_list |
| 188 | } with { variant "" }; |
| 189 | |
| 190 | /* 44.018 9.1.40 */ |
| 191 | type record SystemInformationType6 { |
Harald Welte | 484160b | 2017-07-28 13:30:24 +0200 | [diff] [blame] | 192 | SysinfoCellIdentity cell_id, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 193 | LocationAreaIdentification lai, |
| 194 | CellOptionsSacch cell_options, |
| 195 | BIT8 ncc_permitted, |
Vadim Yanitskiy | b7bc2e6 | 2020-05-04 21:22:41 +0700 | [diff] [blame] | 196 | RestOctets rest_octets length(0..7) |
| 197 | } with { variant "" }; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 198 | |
Harald Welte | a418918 | 2017-07-15 19:59:33 +0200 | [diff] [blame] | 199 | type union SystemInformationUnion { |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 200 | SystemInformationType1 si1, |
| 201 | SystemInformationType2 si2, |
| 202 | SystemInformationType2bis si2bis, |
| 203 | SystemInformationType2ter si2ter, |
Neels Hofmeyr | bb27774 | 2020-07-06 02:39:58 +0200 | [diff] [blame] | 204 | SystemInformationType2quater si2quater, |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 205 | SystemInformationType3 si3, |
| 206 | SystemInformationType4 si4, |
| 207 | SystemInformationType5 si5, |
| 208 | SystemInformationType5bis si5bis, |
| 209 | SystemInformationType5ter si5ter, |
Harald Welte | 8c5b325 | 2017-07-15 23:59:49 +0200 | [diff] [blame] | 210 | SystemInformationType6 si6, |
| 211 | octetstring other |
Harald Welte | a418918 | 2017-07-15 19:59:33 +0200 | [diff] [blame] | 212 | } with { variant "" }; |
| 213 | |
| 214 | type record SystemInformation { |
Harald Welte | d2e342f | 2017-07-16 07:34:13 +0200 | [diff] [blame] | 215 | RrHeader header, |
Harald Welte | a418918 | 2017-07-15 19:59:33 +0200 | [diff] [blame] | 216 | SystemInformationUnion payload |
| 217 | } with { variant (payload) "CROSSTAG(si1, header.message_type = SYSTEM_INFORMATION_TYPE_1; |
Harald Welte | a082a69 | 2017-07-15 15:58:13 +0200 | [diff] [blame] | 218 | si2, header.message_type = SYSTEM_INFORMATION_TYPE_2; |
| 219 | si2bis, header.message_type = SYSTEM_INFORMATION_TYPE_2bis; |
| 220 | si2ter, header.message_type = SYSTEM_INFORMATION_TYPE_2ter; |
Neels Hofmeyr | bb27774 | 2020-07-06 02:39:58 +0200 | [diff] [blame] | 221 | si2quater, header.message_type = SYSTEM_INFORMATION_TYPE_2quater; |
Harald Welte | a082a69 | 2017-07-15 15:58:13 +0200 | [diff] [blame] | 222 | si3, header.message_type = SYSTEM_INFORMATION_TYPE_3; |
| 223 | si4, header.message_type = SYSTEM_INFORMATION_TYPE_4; |
| 224 | si5, header.message_type = SYSTEM_INFORMATION_TYPE_5; |
| 225 | si5bis, header.message_type = SYSTEM_INFORMATION_TYPE_5bis; |
| 226 | si5ter, header.message_type = SYSTEM_INFORMATION_TYPE_5ter; |
| 227 | si6, header.message_type = SYSTEM_INFORMATION_TYPE_6; |
Harald Welte | 8c5b325 | 2017-07-15 23:59:49 +0200 | [diff] [blame] | 228 | other, OTHERWISE; |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 229 | )" }; |
| 230 | |
Vadim Yanitskiy | 0217b05 | 2020-05-04 23:59:01 +0700 | [diff] [blame] | 231 | external function enc_SystemInformationNoPad(in SystemInformation si) return octetstring |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 232 | with { extension "prototype(convert) encode(RAW)" }; |
| 233 | external function dec_SystemInformation(in octetstring stream) return SystemInformation |
| 234 | with { extension "prototype(convert) decode(RAW)" }; |
| 235 | |
Vadim Yanitskiy | 0217b05 | 2020-05-04 23:59:01 +0700 | [diff] [blame] | 236 | /* Due to a buggy nature of TITAN's padding attributes, we have to apply padding manually. */ |
| 237 | function enc_SystemInformation(in SystemInformation si) return octetstring |
| 238 | { |
| 239 | var octetstring si_enc := enc_SystemInformationNoPad(si); |
| 240 | |
| 241 | /* Resulting message length depends on SI Type */ |
| 242 | select (si.header.message_type) { |
| 243 | case (SYSTEM_INFORMATION_TYPE_5, |
| 244 | SYSTEM_INFORMATION_TYPE_5bis, |
| 245 | SYSTEM_INFORMATION_TYPE_5ter) { |
| 246 | /* SACCH: no Rest Octets, return 'as-is' */ |
| 247 | return si_enc; |
| 248 | } |
| 249 | case (SYSTEM_INFORMATION_TYPE_6) { |
| 250 | /* SACCH: pad to 19 octets, leave room for L1/LAPDm headers */ |
| 251 | return f_pad_oct(si_enc, 19, '2B'O); |
| 252 | } |
| 253 | case else { |
| 254 | /* BCCH: pad to 23 octets */ |
| 255 | return f_pad_oct(si_enc, 23, '2B'O); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Vadim Yanitskiy | cb478ec | 2020-07-11 02:37:17 +0700 | [diff] [blame] | 260 | external function dec_SystemInformationSafeBT(in octetstring stream, out SystemInformation si) |
| 261 | return integer /* Decoding result: successful (0) or unsuccessful (1) */ |
| 262 | with { extension "prototype(backtrack) decode(RAW)" }; |
| 263 | |
| 264 | /* Some types of System Information (mostly the Rest Octets) are not fully implemented, |
| 265 | * so calling the generic dec_SystemInformation() may result in a DTE. This function |
| 266 | * additionally checks RR Protocol Discriminator, and should be used in the most cases. */ |
| 267 | function dec_SystemInformationSafe(in octetstring stream, out SystemInformation si) |
| 268 | return integer { |
| 269 | /* Try to decode a given octetstring as System Information */ |
| 270 | if (dec_SystemInformationSafeBT(stream, si) != 0) { |
| 271 | log("Failed to decode (RR) System Information: ", stream); |
| 272 | return 1; |
| 273 | } |
| 274 | |
| 275 | /* Check the protocol discriminator (we expect RR messages) */ |
| 276 | if (si.header.rr_protocol_discriminator != bit2int('0110'B)) { |
| 277 | log("Protocol discriminator is not RR (!= '0110'B): ", |
| 278 | si.header.rr_protocol_discriminator); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Harald Welte | 56db5fd | 2017-07-14 18:25:59 +0200 | [diff] [blame] | 285 | } with { encode "RAW"; variant "FIELDORDER(msb)" } |