Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 1 | /*! \addtogroup gsm0808 |
| 2 | * @{ |
| 3 | * \file gsm0808_utils.h */ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 4 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 18 | * GNU General Public License for more details. |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 19 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 20 | * You should have received a copy of the GNU General Public License |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | #pragma once |
| 25 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 26 | struct sockaddr_storage; |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 27 | |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 28 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Max | 969fb2e | 2018-12-10 11:01:10 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm29205.h> |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 31 | #include <osmocom/gsm/gsm23003.h> |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/gsm_utils.h> |
Max | adc4172 | 2018-11-19 10:44:55 +0100 | [diff] [blame] | 33 | #include <osmocom/gsm/tlv.h> |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 34 | |
| 35 | /*! (225-1)/2 is the maximum number of elements in a cell identifier list. */ |
| 36 | #define GSM0808_CELL_ID_LIST2_MAXLEN 127 |
| 37 | |
Neels Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 38 | /*! Instead of this, use either struct gsm0808_cell_id or gsm0808_cell_id_list2. |
| 39 | * All elements contain parsed representations of the data in the corresponding IE, in host-byte order. |
| 40 | */ |
| 41 | union gsm0808_cell_id_u { |
| 42 | struct osmo_cell_global_id global; |
| 43 | struct osmo_lac_and_ci_id lac_and_ci; |
| 44 | uint16_t ci; |
| 45 | struct osmo_location_area_id lai_and_lac; |
| 46 | uint16_t lac; |
| 47 | }; |
| 48 | |
| 49 | /*! Parsed representation of Cell Identifier IE (3GPP TS 48.008 3.2.2.17) */ |
| 50 | struct gsm0808_cell_id { |
| 51 | enum CELL_IDENT id_discr; |
| 52 | union gsm0808_cell_id_u id; |
| 53 | }; |
| 54 | |
| 55 | /*! Parsed representation of a Cell Identifier List IE (3GPP TS 48.008 3.2.2.27). */ |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 56 | struct gsm0808_cell_id_list2 { |
| 57 | enum CELL_IDENT id_discr; |
Neels Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 58 | union gsm0808_cell_id_u id_list[GSM0808_CELL_ID_LIST2_MAXLEN]; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 59 | unsigned int id_list_len; |
| 60 | }; |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 61 | |
Max | 7387824 | 2018-11-09 12:35:03 +0100 | [diff] [blame] | 62 | /*! LCLS-related parameters from 3GPP TS 48.008 */ |
| 63 | struct osmo_lcls { |
Max | 14f24c6 | 2018-11-29 19:30:07 +0100 | [diff] [blame] | 64 | enum gsm0808_lcls_config config; /**< §3.2.2.116 Configuration */ |
| 65 | enum gsm0808_lcls_control control; /**< §3.2.2.117 Connection Status Control */ |
Max | 3b90125 | 2019-01-15 14:15:11 +0100 | [diff] [blame] | 66 | struct osmo_gcr_parsed gcr; /**< §3.2.2.115 Global Call Reference */ |
| 67 | bool gcr_available; |
Max | 14f24c6 | 2018-11-29 19:30:07 +0100 | [diff] [blame] | 68 | bool corr_needed; /**< §3.2.2.118 Correlation-Not-Needed */ |
Max | 7387824 | 2018-11-09 12:35:03 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 71 | char *osmo_lcls_dump(const struct osmo_lcls *lcls); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 72 | char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 73 | char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls); |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 74 | char *osmo_gcr_dump(const struct osmo_lcls *lcls); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 75 | char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls); |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 76 | |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 77 | extern const struct value_string gsm0808_cell_id_discr_names[]; |
| 78 | static inline const char *gsm0808_cell_id_discr_name(enum CELL_IDENT id_discr) |
| 79 | { return get_value_string(gsm0808_cell_id_discr_names, id_discr); } |
| 80 | |
| 81 | const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid); |
| 82 | const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 83 | char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid); |
| 84 | char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid); |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 85 | const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil); |
| 86 | int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 87 | char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil); |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 88 | int gsm0808_cell_id_u_name(char *buf, size_t buflen, |
| 89 | enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u); |
Neels Hofmeyr | d01ef75 | 2018-09-21 15:57:26 +0200 | [diff] [blame] | 90 | bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match); |
| 91 | int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list, |
| 92 | unsigned int match_nr, bool exact_match); |
Neels Hofmeyr | 3a50453 | 2019-02-10 22:28:27 +0100 | [diff] [blame] | 93 | void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr, |
| 94 | const struct osmo_cell_global_id *cgi); |
| 95 | int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid); |
Harald Welte | e87693c | 2019-05-03 20:06:50 +0200 | [diff] [blame] | 96 | void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u); |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 97 | |
Philipp Maier | 4f4905f | 2018-11-30 13:36:12 +0100 | [diff] [blame] | 98 | uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause); |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 99 | uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg, |
| 100 | const struct sockaddr_storage *ss); |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 101 | int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss, |
| 102 | const uint8_t *elem, uint8_t len); |
Pau Espin Pedrol | 18506c8 | 2019-04-16 15:47:59 +0200 | [diff] [blame] | 103 | int gsm0808_dec_osmux_cid(uint8_t *cid, const uint8_t *elem, uint8_t len); |
Max | 969fb2e | 2018-12-10 11:01:10 +0100 | [diff] [blame] | 104 | |
Max | 4702215 | 2018-12-19 18:51:00 +0100 | [diff] [blame] | 105 | uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls); |
| 106 | int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp); |
Max | 969fb2e | 2018-12-10 11:01:10 +0100 | [diff] [blame] | 107 | |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 108 | uint8_t gsm0808_enc_speech_codec(struct msgb *msg, |
| 109 | const struct gsm0808_speech_codec *sc); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 110 | int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc, |
| 111 | const uint8_t *elem, uint8_t len); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 112 | uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg, |
| 113 | const struct gsm0808_speech_codec_list |
| 114 | *scl); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 115 | int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl, |
| 116 | const uint8_t *elem, uint8_t len); |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 117 | uint8_t gsm0808_enc_channel_type(struct msgb *msg, |
| 118 | const struct gsm0808_channel_type *ct); |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 119 | int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct, |
| 120 | const uint8_t *elem, uint8_t len); |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 121 | uint8_t gsm0808_enc_encrypt_info(struct msgb *msg, |
| 122 | const struct gsm0808_encrypt_info *ei); |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 123 | int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei, |
| 124 | const uint8_t *elem, uint8_t len); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 125 | uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg, const struct gsm0808_cell_id_list2 *cil); |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 126 | uint8_t gsm0808_enc_cell_id_list(struct msgb *msg, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 127 | const struct gsm0808_cell_id_list *cil) |
| 128 | OSMO_DEPRECATED("use gsm0808_enc_cell_id_list2 instead"); |
| 129 | int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil, const uint8_t *elem, uint8_t len); |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 130 | int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 131 | const uint8_t *elem, uint8_t len) |
| 132 | OSMO_DEPRECATED("use gsm0808_dec_cell_id_list2 instead"); |
Neels Hofmeyr | 74663d9 | 2018-03-23 01:46:42 +0100 | [diff] [blame] | 133 | int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src); |
Neels Hofmeyr | 38e5841 | 2018-05-25 16:56:35 +0200 | [diff] [blame] | 134 | void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src); |
Neels Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 135 | uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci); |
| 136 | int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len); |
Neels Hofmeyr | 9a391e2 | 2017-06-23 02:34:08 +0200 | [diff] [blame] | 137 | int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch); |
Philipp Maier | 884ba0f | 2017-06-02 13:49:16 +0200 | [diff] [blame] | 138 | int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc, |
| 139 | uint8_t perm_spch); |
Philipp Maier | 369015c | 2018-09-21 09:07:20 +0200 | [diff] [blame] | 140 | uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg, bool fr); |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 141 | int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg, uint16_t s15_s0); |
Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 142 | |
Max | aa93463 | 2018-11-07 13:16:54 +0100 | [diff] [blame] | 143 | /*! \returns 3GPP TS 08.08 §3.2.2.5 Class of a given Cause */ |
| 144 | static inline enum gsm0808_cause_class gsm0808_cause_class(enum gsm0808_cause cause) |
| 145 | { |
| 146 | return (cause << 1) >> 4; |
| 147 | } |
| 148 | |
| 149 | /*! \returns true if 3GPP TS 08.08 §3.2.2.5 Class has extended bit set */ |
| 150 | static inline bool gsm0808_cause_ext(enum gsm0808_cause cause) |
| 151 | { |
| 152 | /* check that cause looks like 1XXX0000 where XXX represent class */ |
| 153 | return (cause & 0x80) && !(cause & 0x0F); |
| 154 | } |
| 155 | |
Max | ed651d2 | 2018-11-07 15:25:05 +0100 | [diff] [blame] | 156 | int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp); |
| 157 | |
Max | aa93463 | 2018-11-07 13:16:54 +0100 | [diff] [blame] | 158 | /*! \returns 3GPP TS 48.008 3.2.2.49 Current Channel Type 1 from enum gsm_chan_t. */ |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 159 | static inline uint8_t gsm0808_current_channel_type_1(enum gsm_chan_t type) |
| 160 | { |
| 161 | switch (type) { |
| 162 | default: |
| 163 | return 0; |
| 164 | case GSM_LCHAN_SDCCH: |
| 165 | return 0x01; |
| 166 | case GSM_LCHAN_TCH_F: |
| 167 | return 0x18; |
| 168 | case GSM_LCHAN_TCH_H: |
| 169 | return 0x19; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /*! Return 3GPP TS 48.008 3.2.2.51 Speech Version aka permitted speech version indication in 3.2.2.11 |
| 174 | * Channel Type. */ |
| 175 | static inline enum gsm0808_permitted_speech gsm0808_permitted_speech(enum gsm_chan_t type, |
| 176 | enum gsm48_chan_mode mode) |
| 177 | { |
| 178 | switch (mode) { |
| 179 | case GSM48_CMODE_SPEECH_V1: |
| 180 | switch (type) { |
| 181 | case GSM_LCHAN_TCH_F: |
| 182 | return GSM0808_PERM_FR1; |
| 183 | case GSM_LCHAN_TCH_H: |
| 184 | return GSM0808_PERM_HR1; |
| 185 | default: |
| 186 | return 0; |
| 187 | } |
| 188 | case GSM48_CMODE_SPEECH_EFR: |
| 189 | switch (type) { |
| 190 | case GSM_LCHAN_TCH_F: |
| 191 | return GSM0808_PERM_FR2; |
| 192 | case GSM_LCHAN_TCH_H: |
| 193 | return GSM0808_PERM_HR2; |
| 194 | default: |
| 195 | return 0; |
| 196 | } |
| 197 | case GSM48_CMODE_SPEECH_AMR: |
| 198 | switch (type) { |
| 199 | case GSM_LCHAN_TCH_F: |
Neels Hofmeyr | 94e4265 | 2018-06-07 16:54:11 +0200 | [diff] [blame] | 200 | return GSM0808_PERM_FR3; |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 201 | case GSM_LCHAN_TCH_H: |
| 202 | return GSM0808_PERM_HR3; |
| 203 | default: |
| 204 | return 0; |
| 205 | } |
| 206 | default: |
| 207 | return 0; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /*! Return 3GPP TS 48.008 3.2.2.33 Chosen Channel. */ |
| 212 | static inline uint8_t gsm0808_chosen_channel(enum gsm_chan_t type, enum gsm48_chan_mode mode) |
| 213 | { |
| 214 | uint8_t channel_mode = 0, channel = 0; |
| 215 | |
| 216 | switch (mode) { |
| 217 | case GSM48_CMODE_SPEECH_V1: |
| 218 | case GSM48_CMODE_SPEECH_EFR: |
| 219 | case GSM48_CMODE_SPEECH_AMR: |
| 220 | channel_mode = 0x9; |
| 221 | break; |
| 222 | case GSM48_CMODE_SIGN: |
| 223 | channel_mode = 0x8; |
| 224 | break; |
| 225 | case GSM48_CMODE_DATA_14k5: |
| 226 | channel_mode = 0xe; |
| 227 | break; |
| 228 | case GSM48_CMODE_DATA_12k0: |
| 229 | channel_mode = 0xb; |
| 230 | break; |
| 231 | case GSM48_CMODE_DATA_6k0: |
| 232 | channel_mode = 0xc; |
| 233 | break; |
| 234 | case GSM48_CMODE_DATA_3k6: |
| 235 | channel_mode = 0xd; |
| 236 | break; |
| 237 | default: |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | switch (type) { |
| 242 | case GSM_LCHAN_NONE: |
| 243 | channel = 0x0; |
| 244 | break; |
| 245 | case GSM_LCHAN_SDCCH: |
| 246 | channel = 0x1; |
| 247 | break; |
| 248 | case GSM_LCHAN_TCH_F: |
| 249 | channel = 0x8; |
| 250 | break; |
| 251 | case GSM_LCHAN_TCH_H: |
| 252 | channel = 0x9; |
| 253 | break; |
| 254 | default: |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | return channel_mode << 4 | channel; |
| 259 | } |
| 260 | |
| 261 | const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 262 | char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 263 | char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct); |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 264 | |
Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 265 | /*! @} */ |