blob: 4fdad48a2644add80069864eab585acea517e6d0 [file] [log] [blame]
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02001/*! \file gsm23003.c
2 * Utility function implementations related to 3GPP TS 23.003 */
3/*
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2017 sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
5 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02006 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +02009 *
10 * This program is free software; you can redistribute it and/or modify
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
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
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26#include <ctype.h>
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010027#include <stdio.h>
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +010028#include <stdlib.h>
29#include <errno.h>
30#include <string.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020031
32#include <osmocom/gsm/gsm23003.h>
33#include <osmocom/gsm/protocol/gsm_23_003.h>
34
35static bool is_n_digits(const char *str, int min_digits, int max_digits)
36{
37 int len;
38 /* Use unsigned char * to avoid a compiler warning of
39 * "error: array subscript has type 'char' [-Werror=char-subscripts]" */
40 const unsigned char *pos = (const unsigned char *)str;
Neels Hofmeyr4b7c7912017-10-07 04:45:01 +020041 if (!pos)
42 return min_digits < 1;
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020043 for (len = 0; *pos && len < max_digits; len++, pos++)
44 if (!isdigit(*pos))
45 return false;
46 if (len < min_digits)
47 return false;
48 /* With not too many digits, we should have reached *str == nul */
49 if (*pos)
50 return false;
51 return true;
52}
53
54/*! Determine whether the given IMSI is valid according to 3GPP TS 23.003.
55 * \param imsi IMSI digits in ASCII string representation.
56 * \returns true when the IMSI is valid, false for invalid characters or number
57 * of digits.
58 */
59bool osmo_imsi_str_valid(const char *imsi)
60{
61 return is_n_digits(imsi, GSM23003_IMSI_MIN_DIGITS, GSM23003_IMSI_MAX_DIGITS);
62}
63
64/*! Determine whether the given MSISDN is valid according to 3GPP TS 23.003.
65 * \param msisdn MSISDN digits in ASCII string representation.
66 * \returns true when the MSISDN is valid, false for invalid characters or number
67 * of digits.
68 */
69bool osmo_msisdn_str_valid(const char *msisdn)
70{
71 return is_n_digits(msisdn, 1, 15);
72}
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010073
74/*! Return MCC string as standardized 3-digit with leading zeros.
75 * \param[in] mcc MCC value.
76 * \returns string in static buffer.
77 */
78const char *osmo_mcc_name(uint16_t mcc)
79{
80 static char buf[8];
81 snprintf(buf, sizeof(buf), "%03u", mcc);
82 return buf;
83}
84
85/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
86 * \param[in] mnc MNC value.
87 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
88 * \returns string in static buffer.
89 */
90const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
91{
92 static char buf[8];
93 snprintf(buf, sizeof(buf), "%0*u", mnc_3_digits ? 3 : 2, mnc);
94 return buf;
95}
96
97static inline void plmn_name(char *buf, size_t buflen, const struct osmo_plmn_id *plmn)
98{
99 snprintf(buf, buflen, "%s-%s", osmo_mcc_name(plmn->mcc),
100 osmo_mnc_name(plmn->mnc, plmn->mnc_3_digits));
101}
102
103/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
104 * \param[in] plmn MCC-MNC value.
105 * \returns string in static buffer.
106 */
107const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
108{
109 static char buf[16];
110 plmn_name(buf, sizeof(buf), plmn);
111 return buf;
112}
113
114/*! Same as osmo_mcc_mnc_name(), but returning in a different static buffer.
115 * \param[in] plmn MCC-MNC value.
116 * \returns string in static buffer.
117 */
118const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
119{
120 static char buf[16];
121 plmn_name(buf, sizeof(buf), plmn);
122 return buf;
123}
124
125/*! Return MCC-MNC-LAC as string, in a static buffer.
126 * \param[in] lai LAI to encode, the rac member is ignored.
127 * \returns Static string buffer.
128 */
129const char *osmo_lai_name(const struct osmo_location_area_id *lai)
130{
131 static char buf[32];
132 snprintf(buf, sizeof(buf), "%s-%u", osmo_plmn_name(&lai->plmn), lai->lac);
133 return buf;
134}
135
Neels Hofmeyr43496202018-03-22 14:04:30 +0100136static const char *_cgi_name(const struct osmo_cell_global_id *cgi, char *buf, size_t buflen)
137{
138 snprintf(buf, buflen, "%s-%u", osmo_lai_name(&cgi->lai), cgi->cell_identity);
139 return buf;
140}
141
142/*! Return MCC-MNC-LAC-CI as string, in a static buffer.
143 * \param[in] cgi CGI to encode.
144 * \returns Static string buffer.
145 */
146const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
147{
148 static char buf[32];
149 return _cgi_name(cgi, buf, sizeof(buf));
150}
151
152/*! Same as osmo_cgi_name(), but uses a different static buffer.
153 * Useful for printing two distinct CGIs in the same printf format.
154 * \param[in] cgi CGI to encode.
155 * \returns Static string buffer.
156 */
157const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
158{
159 static char buf[32];
160 return _cgi_name(cgi, buf, sizeof(buf));
161}
162
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100163static void to_bcd(uint8_t *bcd, uint16_t val)
164{
165 bcd[2] = val % 10;
166 val = val / 10;
167 bcd[1] = val % 10;
168 val = val / 10;
169 bcd[0] = val % 10;
170}
171
Harald Weltede1da352018-10-08 22:27:04 +0200172const char *osmo_gummei_name(const struct osmo_gummei *gummei)
173{
174 static char buf[32];
175 snprintf(buf, sizeof(buf), "%s-%04x-%02x", osmo_plmn_name(&gummei->plmn),
176 gummei->mme.group_id, gummei->mme.code);
177 return buf;
178}
179
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100180/* Convert MCC + MNC to BCD representation
181 * \param[out] bcd_dst caller-allocated memory for output
182 * \param[in] mcc Mobile Country Code
183 * \param[in] mnc Mobile Network Code
184 * \param[in] mnc_3_digits true if the MNC shall have three digits.
185 *
186 * Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an
187 * allocated buffer of (at least) 3 bytes length. Encode the MNC in three
188 * digits if its integer value is > 99, or if mnc_3_digits is passed true.
189 * Encode an MNC < 100 with mnc_3_digits passed as true as a three-digit MNC
190 * with leading zeros in the BCD representation.
191 */
192void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn)
193{
194 uint8_t bcd[3];
195
196 to_bcd(bcd, plmn->mcc);
197 bcd_dst[0] = bcd[0] | (bcd[1] << 4);
198 bcd_dst[1] = bcd[2];
199
200 to_bcd(bcd, plmn->mnc);
201 if (plmn->mnc > 99 || plmn->mnc_3_digits) {
202 bcd_dst[1] |= bcd[2] << 4;
203 bcd_dst[2] = bcd[0] | (bcd[1] << 4);
204 } else {
205 bcd_dst[1] |= 0xf << 4;
206 bcd_dst[2] = bcd[1] | (bcd[2] << 4);
207 }
208}
209
210/* Convert given 3-byte BCD buffer to integers and write results to *mcc and
211 * *mnc. The first three BCD digits result in the MCC and the remaining ones in
212 * the MNC. Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
213 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
214 * \param[in] bcd_src 3-byte BCD buffer containing MCC+MNC representations.
215 * \param[out] mcc MCC result buffer, or NULL.
216 * \param[out] mnc MNC result buffer, or NULL.
217 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
218 */
219void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn)
220{
221 plmn->mcc = (bcd_src[0] & 0x0f) * 100
222 + (bcd_src[0] >> 4) * 10
223 + (bcd_src[1] & 0x0f);
224
225 if ((bcd_src[1] & 0xf0) == 0xf0) {
226 plmn->mnc = (bcd_src[2] & 0x0f) * 10
227 + (bcd_src[2] >> 4);
228 plmn->mnc_3_digits = false;
229 } else {
230 plmn->mnc = (bcd_src[2] & 0x0f) * 100
231 + (bcd_src[2] >> 4) * 10
232 + (bcd_src[1] >> 4);
233 plmn->mnc_3_digits = true;
234 }
235}
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100236
237/* Convert string to MNC, detecting 3-digit MNC with leading zeros.
238 * Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
239 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
240 * \param mnc_str[in] String representation of an MNC, with or without leading zeros.
241 * \param mnc[out] MNC result buffer, or NULL.
242 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
243 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100244 * errors. In case of error, do not modify the out-arguments.
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100245 */
246int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits)
247{
248 long int _mnc = 0;
249 bool _mnc_3_digits = false;
250 char *endptr;
251 int rc = 0;
252
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100253 if (!mnc_str || !isdigit(mnc_str[0]) || strlen(mnc_str) > 3)
254 return -EINVAL;
255
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100256 errno = 0;
257 _mnc = strtol(mnc_str, &endptr, 10);
258 if (errno)
259 rc = -errno;
260 else if (*endptr)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100261 return -EINVAL;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100262 if (_mnc < 0 || _mnc > 999)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100263 return -ERANGE;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100264 _mnc_3_digits = strlen(mnc_str) > 2;
265
266 if (mnc)
267 *mnc = (uint16_t)_mnc;
268 if (mnc_3_digits)
269 *mnc_3_digits = _mnc_3_digits;
270 return rc;
271}
272
273/* Compare two MNC with three-digit flag.
274 * The mnc_3_digits flags passed in only have an effect if the MNC are < 100, i.e. if they would amount
275 * to a change in leading zeros in a BCD representation. An MNC >= 100 implies three digits, and the flag
276 * is actually ignored.
277 * \param a_mnc[in] "Left" side MNC.
278 * \param a_mnc_3_digits[in] "Left" side three-digits flag.
279 * \param b_mnc[in] "Right" side MNC.
280 * \param b_mnc_3_digits[in] "Right" side three-digits flag.
281 * \returns 0 if the MNC are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
282int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits)
283{
284 if (a_mnc < b_mnc)
285 return -1;
286 if (a_mnc > b_mnc)
287 return 1;
288 /* a_mnc == b_mnc, but same amount of leading zeros? */
289 if (a_mnc < 100 && a_mnc_3_digits != b_mnc_3_digits)
290 return a_mnc_3_digits ? 1 : -1;
291 return 0;
292}
293
294/* Compare two PLMN.
295 * \param a[in] "Left" side PLMN.
296 * \param b[in] "Right" side PLMN.
297 * \returns 0 if the PLMN are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
298int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b)
299{
300 if (a == b)
301 return 0;
302 if (a->mcc < b->mcc)
303 return -1;
304 if (a->mcc > b->mcc)
305 return 1;
306 return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits);
307}
Harald Weltede1da352018-10-08 22:27:04 +0200308
309/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form)
310 * \param out[out] caller-provided output buffer, at least 33 bytes long
311 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
312 * \returns number of characters printed (excluding NUL); negative on error */
313int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn)
314{
315 if (plmn->mcc > 999)
316 return -EINVAL;
317 if (plmn->mnc > 999)
318 return -EINVAL;
319 return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc);
320}
321
322/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id
323 * \param out[out] caller-allocated output structure
324 * \param in[in] character string representation to be parsed
325 * \returns 0 on success; negative on error */
326int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in)
327{
328 int rc;
329
330 memset(out, 0, sizeof(*out));
331 rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc);
332 if (rc < 0)
333 return rc;
334 if (rc != 2)
335 return -EINVAL;
336 return 0;
337}
338
339/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form)
340 * \param out[out] caller-provided output buffer, at least 56 bytes long
341 * \param gummei[in] Structure representing the Globally Unique MME Identifier
342 * \returns number of characters printed (excluding NUL); negative on error */
343int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei)
344{
345 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
346 int rc;
347 rc = osmo_gen_home_network_domain(domain, &gummei->plmn);
348 if (rc < 0)
349 return rc;
350 return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain);
351}
352
353/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei
354 * \param out[out] caller-allocated output GUMMEI structure
355 * \param in[in] character string representation to be parsed
356 * \returns 0 on success; negative on error */
357int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in)
358{
359 int rc;
360
361 memset(out, 0, sizeof(*out));
362 rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org",
363 &out->mme.code, &out->mme.group_id,
364 &out->plmn.mnc, &out->plmn.mcc);
365 if (rc < 0)
366 return rc;
367 if (rc != 4)
368 return -EINVAL;
369 return 0;
370}
371
372/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form)
373 * \param out[out] caller-provided output buffer, at least 56 bytes long
374 * \param mmegi[in] MME Group Identifier
375 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
376 * \returns number of characters printed (excluding NUL); negative on error */
377int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn)
378{
379 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
380 int rc;
381 rc = osmo_gen_home_network_domain(domain, plmn);
382 if (rc < 0)
383 return rc;
384 return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain);
385}