Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gsm48.h */ |
| 2 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 3 | #pragma once |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 4 | |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 5 | #include <stdbool.h> |
| 6 | |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 7 | #include <osmocom/core/defs.h> |
Vadim Yanitskiy | 30cfeeb | 2018-08-03 05:44:00 +0700 | [diff] [blame] | 8 | #include <osmocom/core/msgb.h> |
| 9 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 10 | #include <osmocom/gsm/tlv.h> |
| 11 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 12 | #include <osmocom/gsm/protocol/gsm_23_003.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 13 | #include <osmocom/gsm/gsm48_ie.h> |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 14 | #include <osmocom/gsm/gsm23003.h> |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 15 | |
Harald Welte | 276ca4b | 2014-08-24 17:35:19 +0200 | [diff] [blame] | 16 | /* reserved according to GSM 03.03 ยง 2.4 */ |
| 17 | #define GSM_RESERVED_TMSI 0xFFFFFFFF |
| 18 | |
Neels Hofmeyr | ccfc387 | 2018-02-20 15:14:14 +0100 | [diff] [blame] | 19 | /* Valid MCC and MNC range from 0 to 999. |
| 20 | * To mark an invalid / unset MNC, this value shall be used. */ |
| 21 | #define GSM_MCC_MNC_INVALID 0xFFFF |
| 22 | |
Neels Hofmeyr | 8d42394 | 2023-06-08 00:35:41 +0200 | [diff] [blame] | 23 | /* A parsed GPRS routing area. |
| 24 | * Preferably use struct osmo_routing_area_id, it is better integrated with API like osmo_plmn_cmp(). |
| 25 | */ |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 26 | struct gprs_ra_id { |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 27 | uint16_t mcc; |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 28 | uint16_t mnc; |
| 29 | bool mnc_3_digits; |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 30 | uint16_t lac; |
| 31 | uint8_t rac; |
| 32 | }; |
| 33 | |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 34 | extern const struct tlv_definition gsm48_att_tlvdef; |
Andreas Eversberg | 014cb87 | 2010-07-12 09:11:00 +0200 | [diff] [blame] | 35 | extern const struct tlv_definition gsm48_rr_att_tlvdef; |
| 36 | extern const struct tlv_definition gsm48_mm_att_tlvdef; |
Harald Welte | 9eb6d88 | 2010-03-25 12:00:54 +0800 | [diff] [blame] | 37 | const char *gsm48_cc_state_name(uint8_t state); |
| 38 | const char *gsm48_cc_msg_name(uint8_t msgtype); |
Philipp | 72e43f0 | 2016-10-27 13:35:20 +0200 | [diff] [blame] | 39 | const char *gsm48_rr_msg_name(uint8_t msgtype); |
Max | bc12728 | 2023-01-26 14:40:23 +0300 | [diff] [blame] | 40 | const char *gsm48_rr_short_pd_msg_name(uint8_t msgtype); |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 41 | const char *rr_cause_name(uint8_t cause); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 42 | const char *osmo_rai_name(const struct gprs_ra_id *rai); |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 43 | char *osmo_rai_name_buf(char *buf, size_t buf_len, const struct gprs_ra_id *rai); |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 44 | char *osmo_rai_name_c(const void *ctx, const struct gprs_ra_id *rai); |
Harald Welte | 4fb2075 | 2010-03-02 23:17:33 +0100 | [diff] [blame] | 45 | |
Harald Welte | 774a9de | 2012-07-13 21:35:13 +0200 | [diff] [blame] | 46 | int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc, |
Neels Hofmeyr | bd6c8b5 | 2018-03-22 14:05:21 +0100 | [diff] [blame] | 47 | uint16_t *mnc, uint16_t *lac) |
| 48 | OSMO_DEPRECATED("Use gsm48_decode_lai2() instead, to not lose leading zeros in the MNC"); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 49 | void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded); |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 50 | void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc, |
Neels Hofmeyr | bd6c8b5 | 2018-03-22 14:05:21 +0100 | [diff] [blame] | 51 | uint16_t mnc, uint16_t lac) |
| 52 | OSMO_DEPRECATED("Use gsm48_generate_lai2() instead, to not lose leading zeros in the MNC"); |
Neels Hofmeyr | c4fce14 | 2018-02-20 13:47:08 +0100 | [diff] [blame] | 53 | void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai); |
| 54 | |
Harald Welte | 1c3bae1 | 2019-01-20 10:37:49 +0100 | [diff] [blame] | 55 | #define GSM48_MID_MAX_SIZE 11 |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 56 | int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi) |
| 57 | OSMO_DEPRECATED_OUTSIDE("Instead use: l = msgb_tl_put(msg, GSM48_IE_MOBILE_ID);" |
| 58 | " *l = osmo_mobile_identity_encode_msgb(...)"); |
| 59 | int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi) |
| 60 | OSMO_DEPRECATED_OUTSIDE("Instead use: l = msgb_tl_put(msg, GSM48_IE_MOBILE_ID);" |
| 61 | " *l = osmo_mobile_identity_encode_msgb(...)"); |
| 62 | uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type) |
| 63 | OSMO_DEPRECATED_OUTSIDE("Instead use: l = msgb_tl_put(msg, GSM48_IE_MOBILE_ID);" |
| 64 | " *l = osmo_mobile_identity_encode_msgb(...)"); |
Harald Welte | 61e2bfc | 2010-03-04 10:53:03 +0100 | [diff] [blame] | 65 | |
Harald Welte | 1a8c4e0 | 2015-08-16 17:56:25 +0200 | [diff] [blame] | 66 | const char *gsm48_mi_type_name(uint8_t mi); |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 67 | /* Convert encoded Mobile Identity (10.5.1.4) to string */ |
| 68 | int gsm48_mi_to_string(char *string, int str_len, const uint8_t *mi, int mi_len) |
| 69 | OSMO_DEPRECATED_OUTSIDE("Instead use osmo_mobile_identity_decode()"); |
| 70 | const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len) |
| 71 | OSMO_DEPRECATED_OUTSIDE("Instead use osmo_mobile_identity_to_str_c()"); |
| 72 | char *osmo_mi_name_buf(char *buf, size_t buf_len, const uint8_t *mi, uint8_t mi_len) |
| 73 | OSMO_DEPRECATED_OUTSIDE("Instead use osmo_mobile_identity_to_str_buf()"); |
| 74 | char *osmo_mi_name_c(const void *ctx, const uint8_t *mi, uint8_t mi_len) |
| 75 | OSMO_DEPRECATED_OUTSIDE("Instead use osmo_mobile_identity_to_str_c()"); |
| 76 | |
| 77 | /*! Decoded representation of a Mobile Identity (3GPP TS 24.008 10.5.1.4). |
| 78 | * See osmo_mobile_identity_decode() and osmo_mobile_identity_from_l3(). */ |
| 79 | struct osmo_mobile_identity { |
| 80 | /*! A GSM_MI_TYPE_* constant (like GSM_MI_TYPE_IMSI). */ |
| 81 | uint8_t type; |
| 82 | /*! Decoded Mobile Identity digits or TMSI value. IMSI, IMEI and IMEISV as digits like |
| 83 | * "12345678", and TMSI is represented as raw uint32_t. */ |
| 84 | union { |
| 85 | /*! type == GSM_MI_TYPE_IMSI. */ |
| 86 | char imsi[GSM23003_IMSI_MAX_DIGITS + 1]; |
| 87 | /*! type == GSM_MI_TYPE_IMEI. */ |
| 88 | char imei[GSM23003_IMEI_NUM_DIGITS + 1]; |
| 89 | /*! type == GSM_MI_TYPE_IMEISV. */ |
| 90 | char imeisv[GSM23003_IMEISV_NUM_DIGITS + 1]; |
| 91 | /*! TMSI / P-TMSI / M-TMSI integer value if type == GSM_MI_TYPE_TMSI. */ |
| 92 | uint32_t tmsi; |
| 93 | }; |
| 94 | }; |
| 95 | |
| 96 | int osmo_mobile_identity_to_str_buf(char *buf, size_t buflen, const struct osmo_mobile_identity *mi); |
| 97 | char *osmo_mobile_identity_to_str_c(void *ctx, const struct osmo_mobile_identity *mi); |
| 98 | int osmo_mobile_identity_cmp(const struct osmo_mobile_identity *a, const struct osmo_mobile_identity *b); |
| 99 | int osmo_mobile_identity_decode(struct osmo_mobile_identity *mi, const uint8_t *mi_data, uint8_t mi_len, |
| 100 | bool allow_hex); |
Neels Hofmeyr | e25786a | 2023-05-03 02:13:16 +0200 | [diff] [blame] | 101 | int osmo_mobile_identity_decode_from_l3_buf(struct osmo_mobile_identity *mi, const uint8_t *l3_data, size_t l3_len, |
| 102 | bool allow_hex); |
Neels Hofmeyr | 83025bf | 2020-05-26 02:45:23 +0200 | [diff] [blame] | 103 | int osmo_mobile_identity_decode_from_l3(struct osmo_mobile_identity *mi, struct msgb *msg, bool allow_hex); |
| 104 | int osmo_mobile_identity_encoded_len(const struct osmo_mobile_identity *mi, int *mi_digits); |
| 105 | int osmo_mobile_identity_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_mobile_identity *mi, bool allow_hex); |
| 106 | int osmo_mobile_identity_encode_msgb(struct msgb *msg, const struct osmo_mobile_identity *mi, bool allow_hex); |
Harald Welte | 9bb553e | 2010-03-28 18:14:50 +0800 | [diff] [blame] | 107 | |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 108 | /* Parse Routeing Area Identifier */ |
Neels Hofmeyr | 8d42394 | 2023-06-08 00:35:41 +0200 | [diff] [blame] | 109 | int osmo_routing_area_id_decode(struct osmo_routing_area_id *dst, const uint8_t *ra_data, size_t ra_data_len); |
| 110 | int osmo_routing_area_id_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_routing_area_id *src); |
| 111 | int osmo_routing_area_id_encode_msgb(struct msgb *msg, const struct osmo_routing_area_id *src); |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 112 | void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf); |
Max | f1ad60e | 2018-01-05 14:19:33 +0100 | [diff] [blame] | 113 | void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid); |
Max | 309d0e5 | 2018-01-05 14:21:25 +0100 | [diff] [blame] | 114 | int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid) OSMO_DEPRECATED("Use gsm48_encode_ra() instead"); |
Philipp Maier | d11a5d5 | 2021-02-03 22:11:46 +0100 | [diff] [blame] | 115 | bool gsm48_ra_equal(const struct gprs_ra_id *raid1, const struct gprs_ra_id *raid2); |
Harald Welte | a1c4f76 | 2010-05-01 11:59:42 +0200 | [diff] [blame] | 116 | |
Pau Espin Pedrol | 6ca0a43 | 2022-11-24 17:09:03 +0100 | [diff] [blame] | 117 | int gsm48_number_of_paging_subchannels(const struct gsm48_control_channel_descr *chan_desc); |
Neels Hofmeyr | bdccc1b | 2016-03-15 13:28:10 +0100 | [diff] [blame] | 118 | |
Neels Hofmeyr | bd6c8b5 | 2018-03-22 14:05:21 +0100 | [diff] [blame] | 119 | void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc) |
| 120 | OSMO_DEPRECATED("Use osmo_plmn_to_bcd() instead, to not lose leading zeros in the MNC"); |
| 121 | void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc) |
| 122 | OSMO_DEPRECATED("Use osmo_plmn_from_bcd() instead, to not lose leading zeros in the MNC"); |
Vadim Yanitskiy | 30cfeeb | 2018-08-03 05:44:00 +0700 | [diff] [blame] | 123 | |
| 124 | struct gsm48_hdr *gsm48_push_l3hdr(struct msgb *msg, |
| 125 | uint8_t pdisc, uint8_t msg_type); |
| 126 | |
| 127 | #define gsm48_push_l3hdr_tid(msg, pdisc, tid, msg_type) \ |
| 128 | gsm48_push_l3hdr(msg, (pdisc & 0x0f) | (tid << 4), msg_type) |
Neels Hofmeyr | a9da9f7 | 2021-04-16 15:36:58 +0200 | [diff] [blame] | 129 | |
| 130 | enum gsm48_chan_mode gsm48_chan_mode_to_vamos(enum gsm48_chan_mode mode); |
| 131 | enum gsm48_chan_mode gsm48_chan_mode_to_non_vamos(enum gsm48_chan_mode mode); |