blob: 95fca91a6a82cda2ab9fa00d87ecee3bc3ee4eaf [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>
Oliver Smith894be2d2019-01-11 13:13:37 +010034#include <osmocom/core/utils.h>
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020035
36static bool is_n_digits(const char *str, int min_digits, int max_digits)
37{
38 int len;
39 /* Use unsigned char * to avoid a compiler warning of
40 * "error: array subscript has type 'char' [-Werror=char-subscripts]" */
41 const unsigned char *pos = (const unsigned char *)str;
Neels Hofmeyr4b7c7912017-10-07 04:45:01 +020042 if (!pos)
43 return min_digits < 1;
Neels Hofmeyr9cd1e742017-10-04 03:15:47 +020044 for (len = 0; *pos && len < max_digits; len++, pos++)
45 if (!isdigit(*pos))
46 return false;
47 if (len < min_digits)
48 return false;
49 /* With not too many digits, we should have reached *str == nul */
50 if (*pos)
51 return false;
52 return true;
53}
54
55/*! Determine whether the given IMSI is valid according to 3GPP TS 23.003.
56 * \param imsi IMSI digits in ASCII string representation.
57 * \returns true when the IMSI is valid, false for invalid characters or number
58 * of digits.
59 */
60bool osmo_imsi_str_valid(const char *imsi)
61{
62 return is_n_digits(imsi, GSM23003_IMSI_MIN_DIGITS, GSM23003_IMSI_MAX_DIGITS);
63}
64
65/*! Determine whether the given MSISDN is valid according to 3GPP TS 23.003.
66 * \param msisdn MSISDN digits in ASCII string representation.
67 * \returns true when the MSISDN is valid, false for invalid characters or number
68 * of digits.
69 */
70bool osmo_msisdn_str_valid(const char *msisdn)
71{
72 return is_n_digits(msisdn, 1, 15);
73}
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010074
Oliver Smith894be2d2019-01-11 13:13:37 +010075/*! Determine whether the given IMEI is valid according to 3GPP TS 23.003,
76 * Section 6.2.1. It consists of 14 digits, the 15th check digit is not
77 * intended for digital transmission.
78 * \param imei IMEI digits in ASCII string representation.
79 * \param with_15th_digit when true, expect the 15th digit to be present and
80 * verify it.
81 * \returns true when the IMEI is valid, false for invalid characters or number
82 * of digits.
83 */
84bool osmo_imei_str_valid(const char *imei, bool with_15th_digit)
85{
86 if (with_15th_digit)
87 return is_n_digits(imei, 15, 15) && osmo_luhn(imei, 14) == imei[14];
88 else
89 return is_n_digits(imei, 14, 14);
90}
91
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010092/*! Return MCC string as standardized 3-digit with leading zeros.
93 * \param[in] mcc MCC value.
94 * \returns string in static buffer.
95 */
96const char *osmo_mcc_name(uint16_t mcc)
97{
98 static char buf[8];
99 snprintf(buf, sizeof(buf), "%03u", mcc);
100 return buf;
101}
102
103/*! Return MNC string as standardized 2- or 3-digit with leading zeros.
104 * \param[in] mnc MNC value.
105 * \param[in] mnc_3_digits True if an MNC should fill three digits, only has an effect if MNC < 100.
106 * \returns string in static buffer.
107 */
108const char *osmo_mnc_name(uint16_t mnc, bool mnc_3_digits)
109{
110 static char buf[8];
111 snprintf(buf, sizeof(buf), "%0*u", mnc_3_digits ? 3 : 2, mnc);
112 return buf;
113}
114
115static inline void plmn_name(char *buf, size_t buflen, const struct osmo_plmn_id *plmn)
116{
117 snprintf(buf, buflen, "%s-%s", osmo_mcc_name(plmn->mcc),
118 osmo_mnc_name(plmn->mnc, plmn->mnc_3_digits));
119}
120
121/*! Return MCC-MNC string as standardized 3-digit-dash-2/3-digit with leading zeros.
122 * \param[in] plmn MCC-MNC value.
123 * \returns string in static buffer.
124 */
125const char *osmo_plmn_name(const struct osmo_plmn_id *plmn)
126{
127 static char buf[16];
128 plmn_name(buf, sizeof(buf), plmn);
129 return buf;
130}
131
132/*! Same as osmo_mcc_mnc_name(), but returning in a different static buffer.
133 * \param[in] plmn MCC-MNC value.
134 * \returns string in static buffer.
135 */
136const char *osmo_plmn_name2(const struct osmo_plmn_id *plmn)
137{
138 static char buf[16];
139 plmn_name(buf, sizeof(buf), plmn);
140 return buf;
141}
142
143/*! Return MCC-MNC-LAC as string, in a static buffer.
144 * \param[in] lai LAI to encode, the rac member is ignored.
145 * \returns Static string buffer.
146 */
147const char *osmo_lai_name(const struct osmo_location_area_id *lai)
148{
149 static char buf[32];
150 snprintf(buf, sizeof(buf), "%s-%u", osmo_plmn_name(&lai->plmn), lai->lac);
151 return buf;
152}
153
Neels Hofmeyr43496202018-03-22 14:04:30 +0100154static const char *_cgi_name(const struct osmo_cell_global_id *cgi, char *buf, size_t buflen)
155{
156 snprintf(buf, buflen, "%s-%u", osmo_lai_name(&cgi->lai), cgi->cell_identity);
157 return buf;
158}
159
160/*! Return MCC-MNC-LAC-CI as string, in a static buffer.
161 * \param[in] cgi CGI to encode.
162 * \returns Static string buffer.
163 */
164const char *osmo_cgi_name(const struct osmo_cell_global_id *cgi)
165{
166 static char buf[32];
167 return _cgi_name(cgi, buf, sizeof(buf));
168}
169
170/*! Same as osmo_cgi_name(), but uses a different static buffer.
171 * Useful for printing two distinct CGIs in the same printf format.
172 * \param[in] cgi CGI to encode.
173 * \returns Static string buffer.
174 */
175const char *osmo_cgi_name2(const struct osmo_cell_global_id *cgi)
176{
177 static char buf[32];
178 return _cgi_name(cgi, buf, sizeof(buf));
179}
180
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100181static void to_bcd(uint8_t *bcd, uint16_t val)
182{
183 bcd[2] = val % 10;
184 val = val / 10;
185 bcd[1] = val % 10;
186 val = val / 10;
187 bcd[0] = val % 10;
188}
189
Harald Weltede1da352018-10-08 22:27:04 +0200190const char *osmo_gummei_name(const struct osmo_gummei *gummei)
191{
192 static char buf[32];
193 snprintf(buf, sizeof(buf), "%s-%04x-%02x", osmo_plmn_name(&gummei->plmn),
194 gummei->mme.group_id, gummei->mme.code);
195 return buf;
196}
197
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100198/* Convert MCC + MNC to BCD representation
199 * \param[out] bcd_dst caller-allocated memory for output
200 * \param[in] mcc Mobile Country Code
201 * \param[in] mnc Mobile Network Code
202 * \param[in] mnc_3_digits true if the MNC shall have three digits.
203 *
204 * Convert given mcc and mnc to BCD and write to *bcd_dst, which must be an
205 * allocated buffer of (at least) 3 bytes length. Encode the MNC in three
206 * digits if its integer value is > 99, or if mnc_3_digits is passed true.
207 * Encode an MNC < 100 with mnc_3_digits passed as true as a three-digit MNC
208 * with leading zeros in the BCD representation.
209 */
210void osmo_plmn_to_bcd(uint8_t *bcd_dst, const struct osmo_plmn_id *plmn)
211{
212 uint8_t bcd[3];
213
214 to_bcd(bcd, plmn->mcc);
215 bcd_dst[0] = bcd[0] | (bcd[1] << 4);
216 bcd_dst[1] = bcd[2];
217
218 to_bcd(bcd, plmn->mnc);
219 if (plmn->mnc > 99 || plmn->mnc_3_digits) {
220 bcd_dst[1] |= bcd[2] << 4;
221 bcd_dst[2] = bcd[0] | (bcd[1] << 4);
222 } else {
223 bcd_dst[1] |= 0xf << 4;
224 bcd_dst[2] = bcd[1] | (bcd[2] << 4);
225 }
226}
227
228/* Convert given 3-byte BCD buffer to integers and write results to *mcc and
229 * *mnc. The first three BCD digits result in the MCC and the remaining ones in
230 * the MNC. Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
231 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
232 * \param[in] bcd_src 3-byte BCD buffer containing MCC+MNC representations.
233 * \param[out] mcc MCC result buffer, or NULL.
234 * \param[out] mnc MNC result buffer, or NULL.
235 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
236 */
237void osmo_plmn_from_bcd(const uint8_t *bcd_src, struct osmo_plmn_id *plmn)
238{
239 plmn->mcc = (bcd_src[0] & 0x0f) * 100
240 + (bcd_src[0] >> 4) * 10
241 + (bcd_src[1] & 0x0f);
242
243 if ((bcd_src[1] & 0xf0) == 0xf0) {
244 plmn->mnc = (bcd_src[2] & 0x0f) * 10
245 + (bcd_src[2] >> 4);
246 plmn->mnc_3_digits = false;
247 } else {
248 plmn->mnc = (bcd_src[2] & 0x0f) * 100
249 + (bcd_src[2] >> 4) * 10
250 + (bcd_src[1] >> 4);
251 plmn->mnc_3_digits = true;
252 }
253}
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100254
255/* Convert string to MNC, detecting 3-digit MNC with leading zeros.
256 * Return mnc_3_digits as false if the MNC's most significant digit is encoded as 0xF, true
257 * otherwise; i.e. true if MNC > 99 or if it is represented with leading zeros instead of 0xF.
258 * \param mnc_str[in] String representation of an MNC, with or without leading zeros.
259 * \param mnc[out] MNC result buffer, or NULL.
260 * \param[out] mnc_3_digits Result buffer for 3-digit flag, or NULL.
261 * \returns zero on success, -EINVAL in case of surplus characters, negative errno in case of conversion
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100262 * errors. In case of error, do not modify the out-arguments.
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100263 */
264int osmo_mnc_from_str(const char *mnc_str, uint16_t *mnc, bool *mnc_3_digits)
265{
266 long int _mnc = 0;
267 bool _mnc_3_digits = false;
268 char *endptr;
269 int rc = 0;
270
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100271 if (!mnc_str || !isdigit(mnc_str[0]) || strlen(mnc_str) > 3)
272 return -EINVAL;
273
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100274 errno = 0;
275 _mnc = strtol(mnc_str, &endptr, 10);
276 if (errno)
277 rc = -errno;
278 else if (*endptr)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100279 return -EINVAL;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100280 if (_mnc < 0 || _mnc > 999)
Neels Hofmeyr20f7d0e2018-03-05 04:19:21 +0100281 return -ERANGE;
Neels Hofmeyr721aa6d2018-02-20 21:38:00 +0100282 _mnc_3_digits = strlen(mnc_str) > 2;
283
284 if (mnc)
285 *mnc = (uint16_t)_mnc;
286 if (mnc_3_digits)
287 *mnc_3_digits = _mnc_3_digits;
288 return rc;
289}
290
291/* Compare two MNC with three-digit flag.
292 * The mnc_3_digits flags passed in only have an effect if the MNC are < 100, i.e. if they would amount
293 * to a change in leading zeros in a BCD representation. An MNC >= 100 implies three digits, and the flag
294 * is actually ignored.
295 * \param a_mnc[in] "Left" side MNC.
296 * \param a_mnc_3_digits[in] "Left" side three-digits flag.
297 * \param b_mnc[in] "Right" side MNC.
298 * \param b_mnc_3_digits[in] "Right" side three-digits flag.
299 * \returns 0 if the MNC are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
300int osmo_mnc_cmp(uint16_t a_mnc, bool a_mnc_3_digits, uint16_t b_mnc, bool b_mnc_3_digits)
301{
302 if (a_mnc < b_mnc)
303 return -1;
304 if (a_mnc > b_mnc)
305 return 1;
306 /* a_mnc == b_mnc, but same amount of leading zeros? */
307 if (a_mnc < 100 && a_mnc_3_digits != b_mnc_3_digits)
308 return a_mnc_3_digits ? 1 : -1;
309 return 0;
310}
311
312/* Compare two PLMN.
313 * \param a[in] "Left" side PLMN.
314 * \param b[in] "Right" side PLMN.
315 * \returns 0 if the PLMN are equal, -1 if a < b or a shorter, 1 if a > b or a longer. */
316int osmo_plmn_cmp(const struct osmo_plmn_id *a, const struct osmo_plmn_id *b)
317{
318 if (a == b)
319 return 0;
320 if (a->mcc < b->mcc)
321 return -1;
322 if (a->mcc > b->mcc)
323 return 1;
324 return osmo_mnc_cmp(a->mnc, a->mnc_3_digits, b->mnc, b->mnc_3_digits);
325}
Harald Weltede1da352018-10-08 22:27:04 +0200326
Neels Hofmeyrd01ef752018-09-21 15:57:26 +0200327/* Compare two LAI.
328 * The order of comparison is MCC, MNC, LAC. See also osmo_plmn_cmp().
329 * \param a[in] "Left" side LAI.
330 * \param b[in] "Right" side LAI.
331 * \returns 0 if the LAI are equal, -1 if a < b, 1 if a > b. */
332int osmo_lai_cmp(const struct osmo_location_area_id *a, const struct osmo_location_area_id *b)
333{
334 int rc = osmo_plmn_cmp(&a->plmn, &b->plmn);
335 if (rc)
336 return rc;
337 if (a->lac < b->lac)
338 return -1;
339 if (a->lac > b->lac)
340 return 1;
341 return 0;
342}
343
344/* Compare two CGI.
345 * The order of comparison is MCC, MNC, LAC, CI. See also osmo_lai_cmp().
346 * \param a[in] "Left" side CGI.
347 * \param b[in] "Right" side CGI.
348 * \returns 0 if the CGI are equal, -1 if a < b, 1 if a > b. */
349int osmo_cgi_cmp(const struct osmo_cell_global_id *a, const struct osmo_cell_global_id *b)
350{
351 int rc = osmo_lai_cmp(&a->lai, &b->lai);
352 if (rc)
353 return rc;
354 if (a->cell_identity < b->cell_identity)
355 return -1;
356 if (a->cell_identity > b->cell_identity)
357 return 1;
358 return 0;
359}
360
Harald Weltede1da352018-10-08 22:27:04 +0200361/*! Generate TS 23.003 Section 19.2 Home Network Realm/Domain (text form)
362 * \param out[out] caller-provided output buffer, at least 33 bytes long
363 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
364 * \returns number of characters printed (excluding NUL); negative on error */
365int osmo_gen_home_network_domain(char *out, const struct osmo_plmn_id *plmn)
366{
367 if (plmn->mcc > 999)
368 return -EINVAL;
369 if (plmn->mnc > 999)
370 return -EINVAL;
371 return sprintf(out, "epc.mnc%03u.mcc%03u.3gppnetwork.org", plmn->mnc, plmn->mcc);
372}
373
374/*! Parse a TS 23.003 Section 19.2 Home Network Realm/Domain (text form) into a \ref osmo_plmn_id
375 * \param out[out] caller-allocated output structure
376 * \param in[in] character string representation to be parsed
377 * \returns 0 on success; negative on error */
378int osmo_parse_home_network_domain(struct osmo_plmn_id *out, const char *in)
379{
380 int rc;
381
382 memset(out, 0, sizeof(*out));
383 rc = sscanf(in, "epc.mnc%03hu.mcc%03hu.3gppnetwork.org", &out->mnc, &out->mcc);
384 if (rc < 0)
385 return rc;
386 if (rc != 2)
387 return -EINVAL;
388 return 0;
389}
390
391/*! Generate TS 23.003 Section 19.4.2.4 MME Domain (text form)
392 * \param out[out] caller-provided output buffer, at least 56 bytes long
393 * \param gummei[in] Structure representing the Globally Unique MME Identifier
394 * \returns number of characters printed (excluding NUL); negative on error */
395int osmo_gen_mme_domain(char *out, const struct osmo_gummei *gummei)
396{
397 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
398 int rc;
399 rc = osmo_gen_home_network_domain(domain, &gummei->plmn);
400 if (rc < 0)
401 return rc;
402 return sprintf(out, "mmec%02x.mmegi%04x.mme.%s", gummei->mme.code, gummei->mme.group_id, domain);
403}
404
405/*! Parse a TS 23.003 Section 19.4.2.4 MME Domain (text form) into a \ref osmo_gummei
406 * \param out[out] caller-allocated output GUMMEI structure
407 * \param in[in] character string representation to be parsed
408 * \returns 0 on success; negative on error */
409int osmo_parse_mme_domain(struct osmo_gummei *out, const char *in)
410{
411 int rc;
412
413 memset(out, 0, sizeof(*out));
414 rc = sscanf(in, "mmec%02hhx.mmegi%04hx.mme.epc.mnc%03hu.mcc%03hu.3gppnetwork.org",
415 &out->mme.code, &out->mme.group_id,
416 &out->plmn.mnc, &out->plmn.mcc);
417 if (rc < 0)
418 return rc;
419 if (rc != 4)
420 return -EINVAL;
421 return 0;
422}
423
424/*! Generate TS 23.003 Section 19.4.2.4 MME Group Domain (text form)
425 * \param out[out] caller-provided output buffer, at least 56 bytes long
426 * \param mmegi[in] MME Group Identifier
427 * \param plmn[in] Osmocom representation of PLMN ID (MCC + MNC)
428 * \returns number of characters printed (excluding NUL); negative on error */
429int osmo_gen_mme_group_domain(char *out, uint16_t mmegi, const struct osmo_plmn_id *plmn)
430{
431 char domain[GSM23003_HOME_NETWORK_DOMAIN_LEN+1];
432 int rc;
433 rc = osmo_gen_home_network_domain(domain, plmn);
434 if (rc < 0)
435 return rc;
436 return sprintf(out, "mmegi%04x.mme.%s", mmegi, domain);
437}