/* Code used by both libbsc and libmsc (xsc means "BSC or MSC"). * * (C) 2016 by sysmocom s.m.f.c. * (C) 2008-2010 by Harald Welte * (C) 2014 by Holger Hans Peter Freyther * All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ /* TODO: this file was created during the MSCSPLIT, separating the BSC from the * NITB to create a standalone MSC. Things from libbsc that are needed from * libmsc have been moved here, probably taking along some stuff not actually * needed by the MSC. It may make sense to move things to more appropriate * places or implement things differently when they become more obvious. I'm * taking that as an excuse to make a mess of this file in the sense of keeping * #includes close to their use, and not caring much about mixing things. */ /* FIXME parts of the gsm_network are BSC specific and don't belong here. */ #include #include #include #include struct gsm_network *gsm_network_init(void *ctx, uint16_t country_code, uint16_t network_code, mncc_recv_cb_t mncc_recv) { struct gsm_network *net; net = talloc_zero(ctx, struct gsm_network); if (!net) return NULL; net->bsc_data = talloc_zero(net, struct osmo_bsc_data); if (!net->bsc_data) { talloc_free(net); return NULL; } net->subscr_group = talloc_zero(net, struct gsm_subscriber_group); if (!net->subscr_group) { talloc_free(net); return NULL; } /* Init back pointer */ net->bsc_data->auto_off_timeout = -1; net->bsc_data->network = net; INIT_LLIST_HEAD(&net->bsc_data->mscs); net->subscr_group->net = net; net->create_subscriber = 1; net->country_code = country_code; net->network_code = network_code; net->num_bts = 0; net->reject_cause = GSM48_REJECT_ROAMING_NOT_ALLOWED; net->T3101 = GSM_T3101_DEFAULT; net->T3105 = GSM_T3105_DEFAULT; net->T3113 = GSM_T3113_DEFAULT; net->T3122 = GSM_T3122_DEFAULT; /* FIXME: initialize all other timers! */ /* default set of handover parameters */ net->handover.win_rxlev_avg = 10; net->handover.win_rxqual_avg = 1; net->handover.win_rxlev_avg_neigh = 10; net->handover.pwr_interval = 6; net->handover.pwr_hysteresis = 3; net->handover.max_distance = 9999; /* Use 30 min periodic update interval as sane default */ net->t3212 = 5; INIT_LLIST_HEAD(&net->trans_list); INIT_LLIST_HEAD(&net->upqueue); INIT_LLIST_HEAD(&net->bts_list); INIT_LLIST_HEAD(&net->subscr_conns); net->stats.chreq.total = osmo_counter_alloc("net.chreq.total"); net->stats.chreq.no_channel = osmo_counter_alloc("net.chreq.no_channel"); net->stats.handover.attempted = osmo_counter_alloc("net.handover.attempted"); net->stats.handover.no_channel = osmo_counter_alloc("net.handover.no_channel"); net->stats.handover.timeout = osmo_counter_alloc("net.handover.timeout"); net->stats.handover.completed = osmo_counter_alloc("net.handover.completed"); net->stats.handover.failed = osmo_counter_alloc("net.handover.failed"); net->stats.loc_upd_type.attach = osmo_counter_alloc("net.loc_upd_type.attach"); net->stats.loc_upd_type.normal = osmo_counter_alloc("net.loc_upd_type.normal"); net->stats.loc_upd_type.periodic = osmo_counter_alloc("net.loc_upd_type.periodic"); net->stats.loc_upd_type.detach = osmo_counter_alloc("net.imsi_detach.count"); net->stats.loc_upd_resp.reject = osmo_counter_alloc("net.loc_upd_resp.reject"); net->stats.loc_upd_resp.accept = osmo_counter_alloc("net.loc_upd_resp.accept"); net->stats.paging.attempted = osmo_counter_alloc("net.paging.attempted"); net->stats.paging.detached = osmo_counter_alloc("net.paging.detached"); net->stats.paging.completed = osmo_counter_alloc("net.paging.completed"); net->stats.paging.expired = osmo_counter_alloc("net.paging.expired"); net->stats.sms.submitted = osmo_counter_alloc("net.sms.submitted"); net->stats.sms.no_receiver = osmo_counter_alloc("net.sms.no_receiver"); net->stats.sms.delivered = osmo_counter_alloc("net.sms.delivered"); net->stats.sms.rp_err_mem = osmo_counter_alloc("net.sms.rp_err_mem"); net->stats.sms.rp_err_other = osmo_counter_alloc("net.sms.rp_err_other"); net->stats.call.mo_setup = osmo_counter_alloc("net.call.mo_setup"); net->stats.call.mo_connect_ack = osmo_counter_alloc("net.call.mo_connect_ack"); net->stats.call.mt_setup = osmo_counter_alloc("net.call.mt_setup"); net->stats.call.mt_connect = osmo_counter_alloc("net.call.mt_connect"); net->stats.chan.rf_fail = osmo_counter_alloc("net.chan.rf_fail"); net->stats.chan.rll_err = osmo_counter_alloc("net.chan.rll_err"); net->stats.bts.oml_fail = osmo_counter_alloc("net.bts.oml_fail"); net->stats.bts.rsl_fail = osmo_counter_alloc("net.bts.rsl_fail"); net->mncc_recv = mncc_recv; return net; } struct gsm_subscriber_connection *connection_for_subscr(struct gsm_subscriber *subscr) { /* FIXME: replace this with a backpointer in gsm_subscriber? */ struct gsm_network *net = subscr->group->net; struct gsm_subscriber_connection *conn; llist_for_each_entry(conn, &net->subscr_conns, entry) { if (conn->subscr == subscr) return conn; } return NULL; } /* from gsm_04_08_utils.c *****/ struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value) { struct msgb *msg; struct gsm48_hdr *gh; msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ"); if (!msg) return NULL; gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); gh->proto_discr = GSM48_PDISC_MM; gh->msg_type = GSM48_MT_MM_CM_SERV_REJ; gh->data[0] = value; return msg; } struct msgb *gsm48_create_loc_upd_rej(uint8_t cause) { struct gsm48_hdr *gh; struct msgb *msg; msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ"); if (!msg) return NULL; gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1); gh->proto_discr = GSM48_PDISC_MM; gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT; gh->data[0] = cause; return msg; } int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type) { /* Check the size for the classmark */ if (length < 1 + *classmark2_lv) return -1; uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1; if (length < 2 + *classmark2_lv + mi_lv[0]) return -2; *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK; return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv); } int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length, char *mi_string, uint8_t *mi_type) { static const uint32_t classmark_offset = offsetof(struct gsm48_pag_resp, classmark2); uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2; return gsm48_extract_mi(classmark2_lv, length - classmark_offset, mi_string, mi_type); } struct msgb *gsm0480_gen_ussdNotify(int level, const char *text) { struct gsm48_hdr *gh; struct msgb *msg; msg = gsm0480_create_unstructuredSS_Notify(level, text); if (!msg) return NULL; gsm0480_wrap_invoke(msg, GSM0480_OP_CODE_USS_NOTIFY, 0); gsm0480_wrap_facility(msg); /* And finally pre-pend the L3 header */ gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); gh->proto_discr = GSM48_PDISC_NC_SS; gh->msg_type = GSM0480_MTYPE_REGISTER; return msg; } struct msgb *gsm0480_gen_releaseComplete(void) { struct gsm48_hdr *gh; struct msgb *msg; msg = gsm48_msgb_alloc_name("GSM 04.08 USSD REL COMPL"); if (!msg) return NULL; gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh)); gh->proto_discr = GSM48_PDISC_NC_SS; gh->msg_type = GSM0480_MTYPE_RELEASE_COMPLETE; return msg; } /* Helpers for SMS/GSM 04.11 */ #include #include uint8_t sms_next_rp_msg_ref(uint8_t *next_rp_ref) { const uint8_t rp_msg_ref = *next_rp_ref; /* * This should wrap as the valid range is 0 to 255. We only * transfer one SMS at a time so we don't need to check if * the id has been already assigned. */ *next_rp_ref += 1; return rp_msg_ref; }