Neels Hofmeyr | c69ee85 | 2016-05-10 12:50:31 +0200 | [diff] [blame] | 1 | /* Code used by both libbsc and libmsc (common_cs means "BSC or MSC"). |
| 2 | * |
| 3 | * (C) 2016 by sysmocom s.m.f.c. <info@sysmocom.de> |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 4 | * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2014 by Holger Hans Peter Freyther |
Neels Hofmeyr | c69ee85 | 2016-05-10 12:50:31 +0200 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 22 | |
| 23 | #include <stdbool.h> |
| 24 | |
| 25 | #include <osmocom/gsm/gsm0480.h> |
| 26 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 27 | #include <osmocom/msc/common_cs.h> |
| 28 | #include <osmocom/msc/gsm_data.h> |
| 29 | #include <osmocom/msc/gsm_subscriber.h> |
| 30 | #include <osmocom/msc/gsm_data.h> |
| 31 | #include <osmocom/msc/gsm_04_11.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 32 | #include <osmocom/msc/gsm_04_08.h> |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 33 | |
| 34 | /* Warning: if bsc_network_init() is not called, some of the members of |
| 35 | * gsm_network are not initialized properly and must not be used! (In |
| 36 | * particular the llist heads and stats counters.) |
| 37 | * The long term aim should be to have entirely separate structs for libbsc and |
| 38 | * libmsc with some common general items. |
| 39 | */ |
| 40 | struct gsm_network *gsm_network_init(void *ctx, |
| 41 | uint16_t country_code, |
| 42 | uint16_t network_code, |
| 43 | mncc_recv_cb_t mncc_recv) |
| 44 | { |
| 45 | struct gsm_network *net; |
| 46 | |
| 47 | const char *default_regexp = ".*"; |
| 48 | |
| 49 | net = talloc_zero(ctx, struct gsm_network); |
| 50 | if (!net) |
| 51 | return NULL; |
| 52 | |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 53 | if (gsm_parse_reg(net, &net->authorized_regexp, &net->authorized_reg_str, 1, |
| 54 | &default_regexp) != 0) |
| 55 | return NULL; |
| 56 | |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 57 | net->country_code = country_code; |
| 58 | net->network_code = network_code; |
| 59 | |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 60 | /* Use 30 min periodic update interval as sane default */ |
| 61 | net->t3212 = 5; |
| 62 | |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 63 | INIT_LLIST_HEAD(&net->trans_list); |
| 64 | INIT_LLIST_HEAD(&net->upqueue); |
| 65 | INIT_LLIST_HEAD(&net->subscr_conns); |
| 66 | |
Neels Hofmeyr | 6d804b1 | 2017-02-18 22:20:46 +0100 | [diff] [blame] | 67 | net->bsc_subscribers = talloc_zero(net, struct llist_head); |
| 68 | INIT_LLIST_HEAD(net->bsc_subscribers); |
| 69 | |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 70 | /* init statistics */ |
| 71 | net->msc_ctrs = rate_ctr_group_alloc(net, &msc_ctrg_desc, 0); |
Harald Welte | 3ee3b85 | 2017-07-12 00:25:51 +0200 | [diff] [blame] | 72 | if (!net->msc_ctrs) { |
| 73 | talloc_free(net); |
| 74 | return NULL; |
| 75 | } |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 76 | net->active_calls = osmo_counter_alloc("msc.active_calls"); |
| 77 | |
| 78 | net->mncc_recv = mncc_recv; |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 79 | |
| 80 | net->dyn_ts_allow_tch_f = true; |
| 81 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 82 | INIT_LLIST_HEAD(&net->a.bscs); |
| 83 | |
Neels Hofmeyr | e78ae21 | 2016-05-14 00:46:29 +0200 | [diff] [blame] | 84 | return net; |
| 85 | } |
Neels Hofmeyr | 28f637e | 2016-05-10 14:58:51 +0200 | [diff] [blame] | 86 | |
| 87 | struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value) |
| 88 | { |
| 89 | struct msgb *msg; |
| 90 | struct gsm48_hdr *gh; |
| 91 | |
| 92 | msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ"); |
| 93 | if (!msg) |
| 94 | return NULL; |
| 95 | |
| 96 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 97 | gh->proto_discr = GSM48_PDISC_MM; |
| 98 | gh->msg_type = GSM48_MT_MM_CM_SERV_REJ; |
| 99 | gh->data[0] = value; |
| 100 | |
| 101 | return msg; |
| 102 | } |
| 103 | |
| 104 | struct msgb *gsm48_create_loc_upd_rej(uint8_t cause) |
| 105 | { |
| 106 | struct gsm48_hdr *gh; |
| 107 | struct msgb *msg; |
| 108 | |
| 109 | msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ"); |
| 110 | if (!msg) |
| 111 | return NULL; |
| 112 | |
| 113 | gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); |
| 114 | gh->proto_discr = GSM48_PDISC_MM; |
| 115 | gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT; |
| 116 | gh->data[0] = cause; |
| 117 | return msg; |
| 118 | } |
Neels Hofmeyr | 05667a0 | 2016-05-10 13:27:32 +0200 | [diff] [blame] | 119 | |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 120 | int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type) |
| 121 | { |
| 122 | /* Check the size for the classmark */ |
| 123 | if (length < 1 + *classmark2_lv) |
| 124 | return -1; |
| 125 | |
| 126 | uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1; |
| 127 | if (length < 2 + *classmark2_lv + mi_lv[0]) |
| 128 | return -2; |
| 129 | |
| 130 | *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK; |
| 131 | return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv); |
| 132 | } |
| 133 | |
| 134 | int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length, |
| 135 | char *mi_string, uint8_t *mi_type) |
| 136 | { |
| 137 | static const uint32_t classmark_offset = |
| 138 | offsetof(struct gsm48_pag_resp, classmark2); |
| 139 | uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2; |
| 140 | return gsm48_extract_mi(classmark2_lv, length - classmark_offset, |
| 141 | mi_string, mi_type); |
| 142 | } |
| 143 | |
Neels Hofmeyr | 05667a0 | 2016-05-10 13:27:32 +0200 | [diff] [blame] | 144 | uint8_t sms_next_rp_msg_ref(uint8_t *next_rp_ref) |
| 145 | { |
| 146 | const uint8_t rp_msg_ref = *next_rp_ref; |
| 147 | /* |
| 148 | * This should wrap as the valid range is 0 to 255. We only |
| 149 | * transfer one SMS at a time so we don't need to check if |
| 150 | * the id has been already assigned. |
| 151 | */ |
| 152 | *next_rp_ref += 1; |
| 153 | |
| 154 | return rp_msg_ref; |
| 155 | } |