blob: a1d22981693e344aebcf3e2e0ac3b6bdaae8d98d [file] [log] [blame]
Neels Hofmeyrc69ee852016-05-10 12:50:31 +02001/* 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 Hofmeyre78ae212016-05-14 00:46:29 +02004 * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2014 by Holger Hans Peter Freyther
Neels Hofmeyrc69ee852016-05-10 12:50:31 +02006 * 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 Hofmeyre78ae212016-05-14 00:46:29 +020022
23#include <stdbool.h>
24
25#include <osmocom/gsm/gsm0480.h>
26
Neels Hofmeyr90843962017-09-04 15:04:35 +020027#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>
Max43b01b02017-09-15 11:22:30 +020032#include <osmocom/msc/gsm_04_08.h>
Neels Hofmeyre78ae212016-05-14 00:46:29 +020033
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 */
Neels Hofmeyr7f484202018-02-27 12:59:45 +010040struct gsm_network *gsm_network_init(void *ctx, mncc_recv_cb_t mncc_recv)
Neels Hofmeyre78ae212016-05-14 00:46:29 +020041{
42 struct gsm_network *net;
43
Neels Hofmeyre78ae212016-05-14 00:46:29 +020044 net = talloc_zero(ctx, struct gsm_network);
45 if (!net)
46 return NULL;
47
Neels Hofmeyr379d5792018-02-22 04:04:54 +010048 net->plmn = (struct osmo_plmn_id){ .mcc=1, .mnc=1 };
49
Harald Welte7b222aa2017-12-23 19:30:32 +010050 /* Permit a compile-time default of A5/3 and A5/1 */
51 net->a5_encryption_mask = (1 << 3) | (1 << 1);
Neels Hofmeyre78ae212016-05-14 00:46:29 +020052
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020053 /* Use 30 min periodic update interval as sane default */
54 net->t3212 = 5;
55
Neels Hofmeyr2ff5bcd2017-12-15 03:02:27 +010056 net->paging_response_timer = MSC_PAGING_RESPONSE_TIMER_DEFAULT;
57
Neels Hofmeyre78ae212016-05-14 00:46:29 +020058 INIT_LLIST_HEAD(&net->trans_list);
59 INIT_LLIST_HEAD(&net->upqueue);
60 INIT_LLIST_HEAD(&net->subscr_conns);
61
62 /* init statistics */
63 net->msc_ctrs = rate_ctr_group_alloc(net, &msc_ctrg_desc, 0);
Harald Welte3ee3b852017-07-12 00:25:51 +020064 if (!net->msc_ctrs) {
65 talloc_free(net);
66 return NULL;
67 }
Neels Hofmeyre78ae212016-05-14 00:46:29 +020068 net->active_calls = osmo_counter_alloc("msc.active_calls");
69
70 net->mncc_recv = mncc_recv;
Neels Hofmeyre78ae212016-05-14 00:46:29 +020071
Philipp Maierfbf66102017-04-09 12:32:51 +020072 INIT_LLIST_HEAD(&net->a.bscs);
73
Neels Hofmeyre78ae212016-05-14 00:46:29 +020074 return net;
75}
Neels Hofmeyr28f637e2016-05-10 14:58:51 +020076
77struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
78{
79 struct msgb *msg;
80 struct gsm48_hdr *gh;
81
82 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
83 if (!msg)
84 return NULL;
85
86 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
87 gh->proto_discr = GSM48_PDISC_MM;
88 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
89 gh->data[0] = value;
90
91 return msg;
92}
93
94struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
95{
96 struct gsm48_hdr *gh;
97 struct msgb *msg;
98
99 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
100 if (!msg)
101 return NULL;
102
103 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
104 gh->proto_discr = GSM48_PDISC_MM;
105 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
106 gh->data[0] = cause;
107 return msg;
108}
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200109
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200110int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
111{
112 /* Check the size for the classmark */
113 if (length < 1 + *classmark2_lv)
114 return -1;
115
116 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
117 if (length < 2 + *classmark2_lv + mi_lv[0])
118 return -2;
119
120 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
121 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
122}
123
124int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
125 char *mi_string, uint8_t *mi_type)
126{
127 static const uint32_t classmark_offset =
128 offsetof(struct gsm48_pag_resp, classmark2);
129 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
130 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
131 mi_string, mi_type);
132}
133
Neels Hofmeyr05667a02016-05-10 13:27:32 +0200134uint8_t sms_next_rp_msg_ref(uint8_t *next_rp_ref)
135{
136 const uint8_t rp_msg_ref = *next_rp_ref;
137 /*
138 * This should wrap as the valid range is 0 to 255. We only
139 * transfer one SMS at a time so we don't need to check if
140 * the id has been already assigned.
141 */
142 *next_rp_ref += 1;
143
144 return rp_msg_ref;
145}