blob: 60e665bb84d34f34fd120071d3468a562821cfe0 [file] [log] [blame]
Harald Welte37b61652017-10-16 18:46:03 +02001/*! \addtogroup gsm0808
2 * @{
3 * \file gsm0808_utils.h */
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02004/*
Harald Weltee08da972017-11-13 01:00:26 +09005 * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
Philipp Maier22401432017-03-24 17:59:26 +01006 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
Philipp Maier22401432017-03-24 17:59:26 +01009 *
10 * This program is free software; you can redistribute it and/or modify
Harald Weltee08da972017-11-13 01:00:26 +090011 * 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 Maier22401432017-03-24 17:59:26 +010013 * (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 Weltee08da972017-11-13 01:00:26 +090018 * GNU General Public License for more details.
Philipp Maier22401432017-03-24 17:59:26 +010019 *
Harald Weltee08da972017-11-13 01:00:26 +090020 * You should have received a copy of the GNU General Public License
Philipp Maier22401432017-03-24 17:59:26 +010021 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24#pragma once
25
Harald Welte20725b92017-05-15 12:50:04 +020026struct sockaddr_storage;
Philipp Maier22401432017-03-24 17:59:26 +010027
Philipp Maier6f725d62017-03-24 18:03:17 +010028#include <osmocom/gsm/protocol/gsm_08_08.h>
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +020029#include <osmocom/gsm/protocol/gsm_04_08.h>
Max969fb2e2018-12-10 11:01:10 +010030#include <osmocom/gsm/gsm29205.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010031#include <osmocom/gsm/gsm23003.h>
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +020032#include <osmocom/gsm/gsm_utils.h>
Maxadc41722018-11-19 10:44:55 +010033#include <osmocom/gsm/tlv.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010034
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 Hofmeyr250e7f72018-04-13 03:30:14 +020038/*! 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 */
41union 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;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +010047 struct osmo_cell_global_id_ps global_ps;
Neels Hofmeyr250e7f72018-04-13 03:30:14 +020048};
49
50/*! Parsed representation of Cell Identifier IE (3GPP TS 48.008 3.2.2.17) */
51struct gsm0808_cell_id {
52 enum CELL_IDENT id_discr;
53 union gsm0808_cell_id_u id;
54};
55
56/*! Parsed representation of a Cell Identifier List IE (3GPP TS 48.008 3.2.2.27). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010057struct gsm0808_cell_id_list2 {
58 enum CELL_IDENT id_discr;
Neels Hofmeyr250e7f72018-04-13 03:30:14 +020059 union gsm0808_cell_id_u id_list[GSM0808_CELL_ID_LIST2_MAXLEN];
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010060 unsigned int id_list_len;
61};
Philipp Maier6f725d62017-03-24 18:03:17 +010062
Max73878242018-11-09 12:35:03 +010063/*! LCLS-related parameters from 3GPP TS 48.008 */
64struct osmo_lcls {
Max14f24c62018-11-29 19:30:07 +010065 enum gsm0808_lcls_config config; /**< §3.2.2.116 Configuration */
66 enum gsm0808_lcls_control control; /**< §3.2.2.117 Connection Status Control */
Max3b901252019-01-15 14:15:11 +010067 struct osmo_gcr_parsed gcr; /**< §3.2.2.115 Global Call Reference */
68 bool gcr_available;
Max14f24c62018-11-29 19:30:07 +010069 bool corr_needed; /**< §3.2.2.118 Correlation-Not-Needed */
Max73878242018-11-09 12:35:03 +010070};
71
Max5ec0cf52019-01-15 16:37:09 +010072char *osmo_lcls_dump(const struct osmo_lcls *lcls);
Harald Welte4a62eda2019-03-18 18:27:00 +010073char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls);
Harald Welte179f3572019-03-18 18:38:47 +010074char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls);
Max5ec0cf52019-01-15 16:37:09 +010075char *osmo_gcr_dump(const struct osmo_lcls *lcls);
Harald Welte4a62eda2019-03-18 18:27:00 +010076char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls);
Max5ec0cf52019-01-15 16:37:09 +010077
Neels Hofmeyra4399c82018-04-17 02:26:10 +020078extern const struct value_string gsm0808_cell_id_discr_names[];
79static inline const char *gsm0808_cell_id_discr_name(enum CELL_IDENT id_discr)
80{ return get_value_string(gsm0808_cell_id_discr_names, id_discr); }
81
82const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid);
83const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid);
Harald Welte179f3572019-03-18 18:38:47 +010084char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid);
85char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +020086const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil);
87int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil);
Harald Welte179f3572019-03-18 18:38:47 +010088char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil);
Neels Hofmeyra4399c82018-04-17 02:26:10 +020089int gsm0808_cell_id_u_name(char *buf, size_t buflen,
90 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u);
Neels Hofmeyrd01ef752018-09-21 15:57:26 +020091bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match);
92int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
93 unsigned int match_nr, bool exact_match);
Neels Hofmeyr3a504532019-02-10 22:28:27 +010094void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
95 const struct osmo_cell_global_id *cgi);
96int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid);
Harald Weltee87693c2019-05-03 20:06:50 +020097void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u);
Harald Welteef7be492019-05-03 21:01:13 +020098int gsm0808_decode_cell_id_u(union gsm0808_cell_id_u *out, enum CELL_IDENT discr, const uint8_t *buf, unsigned int len);
Harald Weltef2210032019-08-31 21:25:05 +020099int gsm0808_cell_id_size(enum CELL_IDENT discr);
Neels Hofmeyra4399c82018-04-17 02:26:10 +0200100
Philipp Maier4f4905f2018-11-30 13:36:12 +0100101uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause);
Philipp Maier22401432017-03-24 17:59:26 +0100102uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
103 const struct sockaddr_storage *ss);
Philipp Maier22401432017-03-24 17:59:26 +0100104int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
105 const uint8_t *elem, uint8_t len);
Pau Espin Pedrol18506c82019-04-16 15:47:59 +0200106int gsm0808_dec_osmux_cid(uint8_t *cid, const uint8_t *elem, uint8_t len);
Max969fb2e2018-12-10 11:01:10 +0100107
Max47022152018-12-19 18:51:00 +0100108uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls);
109int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp);
Max969fb2e2018-12-10 11:01:10 +0100110
Philipp Maier6f725d62017-03-24 18:03:17 +0100111uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
112 const struct gsm0808_speech_codec *sc);
Philipp Maier6f725d62017-03-24 18:03:17 +0100113int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
114 const uint8_t *elem, uint8_t len);
Philipp Maier6f725d62017-03-24 18:03:17 +0100115uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
116 const struct gsm0808_speech_codec_list
117 *scl);
Philipp Maier6f725d62017-03-24 18:03:17 +0100118int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
119 const uint8_t *elem, uint8_t len);
Philipp Maiere0c65302017-03-28 17:05:40 +0200120uint8_t gsm0808_enc_channel_type(struct msgb *msg,
121 const struct gsm0808_channel_type *ct);
Philipp Maiere0c65302017-03-28 17:05:40 +0200122int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
123 const uint8_t *elem, uint8_t len);
Philipp Maier14e76b92017-03-28 18:36:52 +0200124uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
125 const struct gsm0808_encrypt_info *ei);
Philipp Maier14e76b92017-03-28 18:36:52 +0200126int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
127 const uint8_t *elem, uint8_t len);
Neels Hofmeyr4a9756c2021-06-10 00:48:15 +0200128int gsm0808_enc_kc128(struct msgb *msg, const uint8_t *kc128);
129int gsm0808_dec_kc128(uint8_t *kc128, const uint8_t *elem, uint8_t len);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100130uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg, const struct gsm0808_cell_id_list2 *cil);
Philipp Maier783047e2017-03-29 11:35:50 +0200131uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100132 const struct gsm0808_cell_id_list *cil)
133 OSMO_DEPRECATED("use gsm0808_enc_cell_id_list2 instead");
134int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil, const uint8_t *elem, uint8_t len);
Philipp Maier783047e2017-03-29 11:35:50 +0200135int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100136 const uint8_t *elem, uint8_t len)
137 OSMO_DEPRECATED("use gsm0808_dec_cell_id_list2 instead");
Neels Hofmeyr74663d92018-03-23 01:46:42 +0100138int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src);
Neels Hofmeyr38e58412018-05-25 16:56:35 +0200139void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src);
Neels Hofmeyr250e7f72018-04-13 03:30:14 +0200140uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci);
141int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len);
Neels Hofmeyr9a391e22017-06-23 02:34:08 +0200142int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch);
Philipp Maier884ba0f2017-06-02 13:49:16 +0200143int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
144 uint8_t perm_spch);
Philipp Maier369015c2018-09-21 09:07:20 +0200145uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg, bool fr);
Philipp Maier3713af82019-02-27 16:48:25 +0100146int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg, uint16_t s15_s0);
Harald Welte37b61652017-10-16 18:46:03 +0200147
Maxaa934632018-11-07 13:16:54 +0100148/*! \returns 3GPP TS 08.08 §3.2.2.5 Class of a given Cause */
149static inline enum gsm0808_cause_class gsm0808_cause_class(enum gsm0808_cause cause)
150{
Alexander Chemerisecfb0d62020-05-13 00:39:15 +0300151 return (cause >> 4) & 0x7;
Maxaa934632018-11-07 13:16:54 +0100152}
153
154/*! \returns true if 3GPP TS 08.08 §3.2.2.5 Class has extended bit set */
155static inline bool gsm0808_cause_ext(enum gsm0808_cause cause)
156{
157 /* check that cause looks like 1XXX0000 where XXX represent class */
158 return (cause & 0x80) && !(cause & 0x0F);
159}
160
Alexander Chemeris2ac8f912020-05-13 22:38:08 +0300161int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
162OSMO_DEPRECATED("Use gsm0808_get_cause() instead");
163
Maxaa934632018-11-07 13:16:54 +0100164/*! \returns 3GPP TS 48.008 3.2.2.49 Current Channel Type 1 from enum gsm_chan_t. */
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +0200165static inline uint8_t gsm0808_current_channel_type_1(enum gsm_chan_t type)
166{
167 switch (type) {
168 default:
169 return 0;
170 case GSM_LCHAN_SDCCH:
171 return 0x01;
172 case GSM_LCHAN_TCH_F:
173 return 0x18;
174 case GSM_LCHAN_TCH_H:
175 return 0x19;
176 }
177}
178
179/*! Return 3GPP TS 48.008 3.2.2.51 Speech Version aka permitted speech version indication in 3.2.2.11
180 * Channel Type. */
181static inline enum gsm0808_permitted_speech gsm0808_permitted_speech(enum gsm_chan_t type,
182 enum gsm48_chan_mode mode)
183{
184 switch (mode) {
185 case GSM48_CMODE_SPEECH_V1:
186 switch (type) {
187 case GSM_LCHAN_TCH_F:
188 return GSM0808_PERM_FR1;
189 case GSM_LCHAN_TCH_H:
190 return GSM0808_PERM_HR1;
191 default:
192 return 0;
193 }
194 case GSM48_CMODE_SPEECH_EFR:
195 switch (type) {
196 case GSM_LCHAN_TCH_F:
197 return GSM0808_PERM_FR2;
198 case GSM_LCHAN_TCH_H:
199 return GSM0808_PERM_HR2;
200 default:
201 return 0;
202 }
203 case GSM48_CMODE_SPEECH_AMR:
204 switch (type) {
205 case GSM_LCHAN_TCH_F:
Neels Hofmeyr94e42652018-06-07 16:54:11 +0200206 return GSM0808_PERM_FR3;
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +0200207 case GSM_LCHAN_TCH_H:
208 return GSM0808_PERM_HR3;
209 default:
210 return 0;
211 }
212 default:
213 return 0;
214 }
215}
216
217/*! Return 3GPP TS 48.008 3.2.2.33 Chosen Channel. */
218static inline uint8_t gsm0808_chosen_channel(enum gsm_chan_t type, enum gsm48_chan_mode mode)
219{
220 uint8_t channel_mode = 0, channel = 0;
221
222 switch (mode) {
223 case GSM48_CMODE_SPEECH_V1:
224 case GSM48_CMODE_SPEECH_EFR:
225 case GSM48_CMODE_SPEECH_AMR:
226 channel_mode = 0x9;
227 break;
228 case GSM48_CMODE_SIGN:
229 channel_mode = 0x8;
230 break;
231 case GSM48_CMODE_DATA_14k5:
232 channel_mode = 0xe;
233 break;
234 case GSM48_CMODE_DATA_12k0:
235 channel_mode = 0xb;
236 break;
237 case GSM48_CMODE_DATA_6k0:
238 channel_mode = 0xc;
239 break;
240 case GSM48_CMODE_DATA_3k6:
241 channel_mode = 0xd;
242 break;
243 default:
244 return 0;
245 }
246
247 switch (type) {
248 case GSM_LCHAN_NONE:
249 channel = 0x0;
250 break;
251 case GSM_LCHAN_SDCCH:
252 channel = 0x1;
253 break;
254 case GSM_LCHAN_TCH_F:
255 channel = 0x8;
256 break;
257 case GSM_LCHAN_TCH_H:
258 channel = 0x9;
259 break;
260 default:
261 return 0;
262 }
263
264 return channel_mode << 4 | channel;
265}
266
267const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct);
Harald Welte4a62eda2019-03-18 18:27:00 +0100268char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct);
Harald Welte179f3572019-03-18 18:38:47 +0100269char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct);
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +0200270
Harald Welte37b61652017-10-16 18:46:03 +0200271/*! @} */