Harald Welte | fbc5ca0 | 2010-03-04 10:39:50 +0100 | [diff] [blame] | 1 | /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 2 | * |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 13 | * GNU Affero General Public License for more details. |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 14 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 17 | * |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include <stdlib.h> |
Harald Welte | 51f3845 | 2009-02-24 22:36:40 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 23 | #include <string.h> |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 24 | #include <errno.h> |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 25 | #include <ctype.h> |
Max | c08ee71 | 2016-05-11 12:45:13 +0200 | [diff] [blame] | 26 | #include <stdbool.h> |
Harald Welte | 97a282b | 2010-03-14 15:37:43 +0800 | [diff] [blame] | 27 | #include <netinet/in.h> |
| 28 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 29 | #include <osmocom/core/linuxlist.h> |
| 30 | #include <osmocom/core/talloc.h> |
| 31 | #include <osmocom/gsm/gsm_utils.h> |
Harald Welte | 867d9f3 | 2011-05-23 20:30:39 +0200 | [diff] [blame] | 32 | #include <osmocom/gsm/abis_nm.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 33 | #include <osmocom/core/statistics.h> |
Max | c08ee71 | 2016-05-11 12:45:13 +0200 | [diff] [blame] | 34 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 35 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 36 | #include <osmocom/msc/gsm_data.h> |
| 37 | #include <osmocom/msc/bsc_msc_data.h> |
| 38 | #include <osmocom/msc/abis_nm.h> |
Holger Hans Peter Freyther | ed83286 | 2010-06-28 18:20:22 +0800 | [diff] [blame] | 39 | |
Andreas Eversberg | 8226fa7 | 2009-06-29 15:19:38 +0200 | [diff] [blame] | 40 | void *tall_bsc_ctx; |
| 41 | |
Harald Welte | 39315c4 | 2010-01-10 18:01:52 +0100 | [diff] [blame] | 42 | static LLIST_HEAD(bts_models); |
| 43 | |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 44 | void set_ts_e1link(struct gsm_bts_trx_ts *ts, uint8_t e1_nr, |
| 45 | uint8_t e1_ts, uint8_t e1_ts_ss) |
Harald Welte | cd06bfb | 2009-02-10 17:33:56 +0000 | [diff] [blame] | 46 | { |
| 47 | ts->e1_link.e1_nr = e1_nr; |
| 48 | ts->e1_link.e1_ts = e1_ts; |
| 49 | ts->e1_link.e1_ts_ss = e1_ts_ss; |
| 50 | } |
| 51 | |
Harald Welte | e555c2b | 2012-08-17 13:02:12 +0200 | [diff] [blame] | 52 | const struct value_string bts_type_descs[_NUM_GSM_BTS_TYPE+1] = { |
| 53 | { GSM_BTS_TYPE_UNKNOWN, "Unknown BTS Type" }, |
| 54 | { GSM_BTS_TYPE_BS11, "Siemens BTS (BS-11 or compatible)" }, |
| 55 | { GSM_BTS_TYPE_NANOBTS, "ip.access nanoBTS or compatible" }, |
| 56 | { GSM_BTS_TYPE_RBS2000, "Ericsson RBS2000 Series" }, |
Harald Welte | e555c2b | 2012-08-17 13:02:12 +0200 | [diff] [blame] | 57 | { GSM_BTS_TYPE_NOKIA_SITE, "Nokia {Metro,Ultra,In}Site" }, |
Max | f9685c1 | 2017-03-23 12:01:07 +0100 | [diff] [blame] | 58 | { GSM_BTS_TYPE_OSMOBTS, "sysmocom sysmoBTS" }, |
Harald Welte | e555c2b | 2012-08-17 13:02:12 +0200 | [diff] [blame] | 59 | { 0, NULL } |
| 60 | }; |
| 61 | |
Harald Welte | 6e670aa | 2010-01-07 20:44:32 +0100 | [diff] [blame] | 62 | struct gsm_bts_trx *gsm_bts_trx_by_nr(struct gsm_bts *bts, int nr) |
| 63 | { |
| 64 | struct gsm_bts_trx *trx; |
| 65 | |
| 66 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 67 | if (trx->nr == nr) |
| 68 | return trx; |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
Harald Welte | be99149 | 2009-05-23 13:56:40 +0000 | [diff] [blame] | 73 | /* Search for a BTS in the given Location Area; optionally start searching |
| 74 | * with start_bts (for continuing to search after the first result) */ |
| 75 | struct gsm_bts *gsm_bts_by_lac(struct gsm_network *net, unsigned int lac, |
| 76 | struct gsm_bts *start_bts) |
| 77 | { |
| 78 | int i; |
| 79 | struct gsm_bts *bts; |
| 80 | int skip = 0; |
| 81 | |
| 82 | if (start_bts) |
| 83 | skip = 1; |
| 84 | |
| 85 | for (i = 0; i < net->num_bts; i++) { |
Harald Welte | e441d9c | 2009-06-21 16:17:15 +0200 | [diff] [blame] | 86 | bts = gsm_bts_num(net, i); |
Harald Welte | be99149 | 2009-05-23 13:56:40 +0000 | [diff] [blame] | 87 | |
| 88 | if (skip) { |
| 89 | if (start_bts == bts) |
| 90 | skip = 0; |
| 91 | continue; |
| 92 | } |
| 93 | |
Holger Hans Peter Freyther | e48b956 | 2009-10-01 04:07:15 +0200 | [diff] [blame] | 94 | if (lac == GSM_LAC_RESERVED_ALL_BTS || bts->location_area_code == lac) |
Harald Welte | be99149 | 2009-05-23 13:56:40 +0000 | [diff] [blame] | 95 | return bts; |
| 96 | } |
| 97 | return NULL; |
| 98 | } |
Harald Welte | fcd2445 | 2009-06-20 18:15:19 +0200 | [diff] [blame] | 99 | |
Harald Welte | 92b1fe4 | 2010-03-25 11:45:30 +0800 | [diff] [blame] | 100 | static const struct value_string auth_policy_names[] = { |
| 101 | { GSM_AUTH_POLICY_CLOSED, "closed" }, |
| 102 | { GSM_AUTH_POLICY_ACCEPT_ALL, "accept-all" }, |
| 103 | { GSM_AUTH_POLICY_TOKEN, "token" }, |
Max | ddee01f | 2016-05-24 14:23:27 +0200 | [diff] [blame] | 104 | { GSM_AUTH_POLICY_REGEXP, "regexp" }, |
Harald Welte | 92b1fe4 | 2010-03-25 11:45:30 +0800 | [diff] [blame] | 105 | { 0, NULL } |
Harald Welte (local) | 69de397 | 2009-08-12 14:42:23 +0200 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | enum gsm_auth_policy gsm_auth_policy_parse(const char *arg) |
| 109 | { |
Harald Welte | 92b1fe4 | 2010-03-25 11:45:30 +0800 | [diff] [blame] | 110 | return get_string_value(auth_policy_names, arg); |
Harald Welte (local) | 69de397 | 2009-08-12 14:42:23 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | const char *gsm_auth_policy_name(enum gsm_auth_policy policy) |
| 114 | { |
Harald Welte | 92b1fe4 | 2010-03-25 11:45:30 +0800 | [diff] [blame] | 115 | return get_value_string(auth_policy_names, policy); |
Harald Welte (local) | 69de397 | 2009-08-12 14:42:23 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Harald Welte | 92b1fe4 | 2010-03-25 11:45:30 +0800 | [diff] [blame] | 118 | static const struct value_string rrlp_mode_names[] = { |
| 119 | { RRLP_MODE_NONE, "none" }, |
| 120 | { RRLP_MODE_MS_BASED, "ms-based" }, |
| 121 | { RRLP_MODE_MS_PREF, "ms-preferred" }, |
| 122 | { RRLP_MODE_ASS_PREF, "ass-preferred" }, |
| 123 | { 0, NULL } |
Harald Welte | eab84a1 | 2009-12-13 10:53:12 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | enum rrlp_mode rrlp_mode_parse(const char *arg) |
| 127 | { |
Harald Welte | 92b1fe4 | 2010-03-25 11:45:30 +0800 | [diff] [blame] | 128 | return get_string_value(rrlp_mode_names, arg); |
Harald Welte | eab84a1 | 2009-12-13 10:53:12 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | const char *rrlp_mode_name(enum rrlp_mode mode) |
| 132 | { |
Harald Welte | 92b1fe4 | 2010-03-25 11:45:30 +0800 | [diff] [blame] | 133 | return get_value_string(rrlp_mode_names, mode); |
Harald Welte | eab84a1 | 2009-12-13 10:53:12 +0100 | [diff] [blame] | 134 | } |
Harald Welte | d12b0fd | 2009-12-15 21:36:05 +0100 | [diff] [blame] | 135 | |
Harald Welte | 4511d89 | 2010-04-18 15:51:20 +0200 | [diff] [blame] | 136 | static const struct value_string bts_gprs_mode_names[] = { |
| 137 | { BTS_GPRS_NONE, "none" }, |
| 138 | { BTS_GPRS_GPRS, "gprs" }, |
| 139 | { BTS_GPRS_EGPRS, "egprs" }, |
| 140 | { 0, NULL } |
| 141 | }; |
| 142 | |
Holger Hans Peter Freyther | 4e13a8f | 2015-01-31 22:16:00 +0100 | [diff] [blame] | 143 | enum bts_gprs_mode bts_gprs_mode_parse(const char *arg, int *valid) |
Harald Welte | 4511d89 | 2010-04-18 15:51:20 +0200 | [diff] [blame] | 144 | { |
Holger Hans Peter Freyther | 4e13a8f | 2015-01-31 22:16:00 +0100 | [diff] [blame] | 145 | int rc; |
| 146 | |
| 147 | rc = get_string_value(bts_gprs_mode_names, arg); |
| 148 | if (valid) |
| 149 | *valid = rc != -EINVAL; |
| 150 | return rc; |
Harald Welte | 4511d89 | 2010-04-18 15:51:20 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | const char *bts_gprs_mode_name(enum bts_gprs_mode mode) |
| 154 | { |
| 155 | return get_value_string(bts_gprs_mode_names, mode); |
| 156 | } |
| 157 | |
Harald Welte | 3300c01 | 2011-06-05 13:31:33 +0200 | [diff] [blame] | 158 | void gprs_ra_id_by_bts(struct gprs_ra_id *raid, struct gsm_bts *bts) |
| 159 | { |
| 160 | raid->mcc = bts->network->country_code; |
| 161 | raid->mnc = bts->network->network_code; |
| 162 | raid->lac = bts->location_area_code; |
| 163 | raid->rac = bts->gprs.rac; |
| 164 | } |
| 165 | |
| 166 | int gsm48_ra_id_by_bts(uint8_t *buf, struct gsm_bts *bts) |
| 167 | { |
| 168 | struct gprs_ra_id raid; |
| 169 | |
| 170 | gprs_ra_id_by_bts(&raid, bts); |
| 171 | |
| 172 | return gsm48_construct_ra(buf, &raid); |
| 173 | } |
Holger Hans Peter Freyther | 06c9da6 | 2011-06-09 21:48:49 +0200 | [diff] [blame] | 174 | |
| 175 | int gsm_parse_reg(void *ctx, regex_t *reg, char **str, int argc, const char **argv) |
| 176 | { |
| 177 | int ret; |
| 178 | |
| 179 | ret = 0; |
| 180 | if (*str) { |
| 181 | talloc_free(*str); |
| 182 | *str = NULL; |
| 183 | } |
| 184 | regfree(reg); |
| 185 | |
| 186 | if (argc > 0) { |
| 187 | *str = talloc_strdup(ctx, argv[0]); |
| 188 | ret = regcomp(reg, argv[0], 0); |
| 189 | |
| 190 | /* handle compilation failures */ |
| 191 | if (ret != 0) { |
| 192 | talloc_free(*str); |
| 193 | *str = NULL; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
Holger Hans Peter Freyther | c22930e | 2014-12-17 14:46:17 +0100 | [diff] [blame] | 200 | /* Assume there are only 256 possible bts */ |
| 201 | osmo_static_assert(sizeof(((struct gsm_bts *) 0)->nr) == 1, _bts_nr_is_256); |
Harald Welte | 2f8b9d2 | 2017-06-18 11:12:13 +0300 | [diff] [blame] | 202 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 203 | bool classmark_is_r99(struct gsm_classmark *cm) |
| 204 | { |
| 205 | int rev_lev = 0; |
| 206 | if (cm->classmark1_set) |
| 207 | rev_lev = cm->classmark1.rev_lev; |
| 208 | else if (cm->classmark2_len > 0) |
| 209 | rev_lev = (cm->classmark2[0] >> 5) & 0x3; |
| 210 | return rev_lev >= 2; |
| 211 | } |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 212 | |
| 213 | const struct value_string ran_type_names[] = { |
| 214 | OSMO_VALUE_STRING(RAN_UNKNOWN), |
| 215 | OSMO_VALUE_STRING(RAN_GERAN_A), |
| 216 | OSMO_VALUE_STRING(RAN_UTRAN_IU), |
| 217 | { 0, NULL } |
| 218 | }; |