blob: 00fb6f405ab2506f116306bc08d82e899b4a44a8 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm48.h */
2
Sylvain Munaut12ba7782014-06-16 10:13:40 +02003#pragma once
Harald Welte4fb20752010-03-02 23:17:33 +01004
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01005#include <stdbool.h>
6
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02007#include <osmocom/core/defs.h>
Vadim Yanitskiy30cfeeb2018-08-03 05:44:00 +07008#include <osmocom/core/msgb.h>
9
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010010#include <osmocom/gsm/tlv.h>
11#include <osmocom/gsm/protocol/gsm_04_08.h>
Neels Hofmeyr83025bf2020-05-26 02:45:23 +020012#include <osmocom/gsm/protocol/gsm_23_003.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010013#include <osmocom/gsm/gsm48_ie.h>
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010014#include <osmocom/gsm/gsm23003.h>
Harald Welte4fb20752010-03-02 23:17:33 +010015
Harald Welte276ca4b2014-08-24 17:35:19 +020016/* reserved according to GSM 03.03 ยง 2.4 */
17#define GSM_RESERVED_TMSI 0xFFFFFFFF
18
Neels Hofmeyrccfc3872018-02-20 15:14:14 +010019/* 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 Hofmeyr8d423942023-06-08 00:35:41 +020023/* 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 Weltea1c4f762010-05-01 11:59:42 +020026struct gprs_ra_id {
Harald Weltea1c4f762010-05-01 11:59:42 +020027 uint16_t mcc;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010028 uint16_t mnc;
29 bool mnc_3_digits;
Harald Weltea1c4f762010-05-01 11:59:42 +020030 uint16_t lac;
31 uint8_t rac;
32};
33
Harald Welte4fb20752010-03-02 23:17:33 +010034extern const struct tlv_definition gsm48_att_tlvdef;
Andreas Eversberg014cb872010-07-12 09:11:00 +020035extern const struct tlv_definition gsm48_rr_att_tlvdef;
36extern const struct tlv_definition gsm48_mm_att_tlvdef;
Harald Welte9eb6d882010-03-25 12:00:54 +080037const char *gsm48_cc_state_name(uint8_t state);
38const char *gsm48_cc_msg_name(uint8_t msgtype);
Philipp72e43f02016-10-27 13:35:20 +020039const char *gsm48_rr_msg_name(uint8_t msgtype);
Maxbc127282023-01-26 14:40:23 +030040const char *gsm48_rr_short_pd_msg_name(uint8_t msgtype);
Harald Welte4fb20752010-03-02 23:17:33 +010041const char *rr_cause_name(uint8_t cause);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010042const char *osmo_rai_name(const struct gprs_ra_id *rai);
Harald Welte4a62eda2019-03-18 18:27:00 +010043char *osmo_rai_name_buf(char *buf, size_t buf_len, const struct gprs_ra_id *rai);
Harald Welte179f3572019-03-18 18:38:47 +010044char *osmo_rai_name_c(const void *ctx, const struct gprs_ra_id *rai);
Harald Welte4fb20752010-03-02 23:17:33 +010045
Harald Welte774a9de2012-07-13 21:35:13 +020046int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
Neels Hofmeyrbd6c8b52018-03-22 14:05:21 +010047 uint16_t *mnc, uint16_t *lac)
48 OSMO_DEPRECATED("Use gsm48_decode_lai2() instead, to not lose leading zeros in the MNC");
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010049void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded);
Harald Welte61e2bfc2010-03-04 10:53:03 +010050void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
Neels Hofmeyrbd6c8b52018-03-22 14:05:21 +010051 uint16_t mnc, uint16_t lac)
52 OSMO_DEPRECATED("Use gsm48_generate_lai2() instead, to not lose leading zeros in the MNC");
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010053void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai);
54
Harald Welte1c3bae12019-01-20 10:37:49 +010055#define GSM48_MID_MAX_SIZE 11
Neels Hofmeyr83025bf2020-05-26 02:45:23 +020056int 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(...)");
59int 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(...)");
62uint8_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 Welte61e2bfc2010-03-04 10:53:03 +010065
Harald Welte1a8c4e02015-08-16 17:56:25 +020066const char *gsm48_mi_type_name(uint8_t mi);
Neels Hofmeyr83025bf2020-05-26 02:45:23 +020067/* Convert encoded Mobile Identity (10.5.1.4) to string */
68int 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()");
70const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
71 OSMO_DEPRECATED_OUTSIDE("Instead use osmo_mobile_identity_to_str_c()");
72char *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()");
74char *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(). */
79struct 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
96int osmo_mobile_identity_to_str_buf(char *buf, size_t buflen, const struct osmo_mobile_identity *mi);
97char *osmo_mobile_identity_to_str_c(void *ctx, const struct osmo_mobile_identity *mi);
98int osmo_mobile_identity_cmp(const struct osmo_mobile_identity *a, const struct osmo_mobile_identity *b);
99int osmo_mobile_identity_decode(struct osmo_mobile_identity *mi, const uint8_t *mi_data, uint8_t mi_len,
100 bool allow_hex);
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200101int 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 Hofmeyr83025bf2020-05-26 02:45:23 +0200103int osmo_mobile_identity_decode_from_l3(struct osmo_mobile_identity *mi, struct msgb *msg, bool allow_hex);
104int osmo_mobile_identity_encoded_len(const struct osmo_mobile_identity *mi, int *mi_digits);
105int osmo_mobile_identity_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_mobile_identity *mi, bool allow_hex);
106int osmo_mobile_identity_encode_msgb(struct msgb *msg, const struct osmo_mobile_identity *mi, bool allow_hex);
Harald Welte9bb553e2010-03-28 18:14:50 +0800107
Harald Weltea1c4f762010-05-01 11:59:42 +0200108/* Parse Routeing Area Identifier */
Neels Hofmeyr8d423942023-06-08 00:35:41 +0200109int osmo_routing_area_id_decode(struct osmo_routing_area_id *dst, const uint8_t *ra_data, size_t ra_data_len);
110int osmo_routing_area_id_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_routing_area_id *src);
111int osmo_routing_area_id_encode_msgb(struct msgb *msg, const struct osmo_routing_area_id *src);
Harald Weltea1c4f762010-05-01 11:59:42 +0200112void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf);
Maxf1ad60e2018-01-05 14:19:33 +0100113void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid);
Max309d0e52018-01-05 14:21:25 +0100114int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid) OSMO_DEPRECATED("Use gsm48_encode_ra() instead");
Philipp Maierd11a5d52021-02-03 22:11:46 +0100115bool gsm48_ra_equal(const struct gprs_ra_id *raid1, const struct gprs_ra_id *raid2);
Harald Weltea1c4f762010-05-01 11:59:42 +0200116
Pau Espin Pedrol6ca0a432022-11-24 17:09:03 +0100117int gsm48_number_of_paging_subchannels(const struct gsm48_control_channel_descr *chan_desc);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100118
Neels Hofmeyrbd6c8b52018-03-22 14:05:21 +0100119void 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");
121void 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 Yanitskiy30cfeeb2018-08-03 05:44:00 +0700123
124struct 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 Hofmeyra9da9f72021-04-16 15:36:58 +0200129
130enum gsm48_chan_mode gsm48_chan_mode_to_vamos(enum gsm48_chan_mode mode);
131enum gsm48_chan_mode gsm48_chan_mode_to_non_vamos(enum gsm48_chan_mode mode);