Holger Hans Peter Freyther | 8ec4952 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 1 | /* (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 2 | * (C) 2009-2011 by On-Waves |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [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 |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [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. |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [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/>. |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <openbsc/osmo_bsc.h> |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 21 | #include <openbsc/osmo_msc_data.h> |
| 22 | #include <openbsc/gsm_04_80.h> |
Holger Hans Peter Freyther | 16e958d | 2010-11-15 13:34:03 +0100 | [diff] [blame] | 23 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | d65305f | 2010-11-05 11:31:08 +0100 | [diff] [blame] | 24 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 16e958d | 2010-11-15 13:34:03 +0100 | [diff] [blame] | 25 | #include <openbsc/paging.h> |
| 26 | |
| 27 | #include <stdlib.h> |
Holger Hans Peter Freyther | d65305f | 2010-11-05 11:31:08 +0100 | [diff] [blame] | 28 | |
| 29 | static void handle_lu_request(struct gsm_subscriber_connection *conn, |
| 30 | struct msgb *msg) |
| 31 | { |
| 32 | struct gsm48_hdr *gh; |
| 33 | struct gsm48_loc_upd_req *lu; |
| 34 | struct gsm48_loc_area_id lai; |
| 35 | struct gsm_network *net; |
| 36 | |
| 37 | if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*lu)) { |
| 38 | LOGP(DMSC, LOGL_ERROR, "LU too small to look at: %u\n", msgb_l3len(msg)); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | net = conn->bts->network; |
| 43 | |
| 44 | gh = msgb_l3(msg); |
| 45 | lu = (struct gsm48_loc_upd_req *) gh->data; |
| 46 | |
| 47 | gsm48_generate_lai(&lai, net->country_code, net->network_code, |
| 48 | conn->bts->location_area_code); |
| 49 | |
| 50 | if (memcmp(&lai, &lu->lai, sizeof(lai)) != 0) { |
| 51 | LOGP(DMSC, LOGL_DEBUG, "Marking con for welcome USSD.\n"); |
| 52 | conn->sccp_con->new_subscriber = 1; |
| 53 | } |
| 54 | } |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 55 | |
Holger Hans Peter Freyther | 16e958d | 2010-11-15 13:34:03 +0100 | [diff] [blame] | 56 | /* we will need to stop the paging request */ |
| 57 | static int handle_page_resp(struct gsm_subscriber_connection *conn, struct msgb *msg) |
| 58 | { |
| 59 | uint8_t mi_type; |
| 60 | char mi_string[GSM48_MI_SIZE]; |
| 61 | struct gsm48_hdr *gh; |
| 62 | struct gsm48_pag_resp *resp; |
| 63 | struct gsm_subscriber *subscr; |
| 64 | |
| 65 | if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*resp)) { |
| 66 | LOGP(DMSC, LOGL_ERROR, "PagingResponse too small: %u\n", msgb_l3len(msg)); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | gh = msgb_l3(msg); |
| 71 | resp = (struct gsm48_pag_resp *) &gh->data[0]; |
| 72 | |
| 73 | gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh), |
| 74 | mi_string, &mi_type); |
| 75 | DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n", |
| 76 | mi_type, mi_string); |
| 77 | |
| 78 | switch (mi_type) { |
| 79 | case GSM_MI_TYPE_TMSI: |
| 80 | subscr = subscr_active_by_tmsi(conn->bts->network, |
| 81 | tmsi_from_string(mi_string)); |
| 82 | break; |
| 83 | case GSM_MI_TYPE_IMSI: |
| 84 | subscr = subscr_active_by_imsi(conn->bts->network, mi_string); |
| 85 | break; |
Holger Hans Peter Freyther | 3fbd244 | 2011-01-16 18:18:56 +0100 | [diff] [blame] | 86 | default: |
| 87 | subscr = NULL; |
| 88 | break; |
Holger Hans Peter Freyther | 16e958d | 2010-11-15 13:34:03 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (!subscr) { |
| 92 | LOGP(DMSC, LOGL_ERROR, "Non active subscriber got paged.\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | |
Harald Welte | f604bba | 2010-12-15 15:33:08 +0100 | [diff] [blame] | 96 | paging_request_stop(conn->bts, subscr, conn, msg); |
Holger Hans Peter Freyther | 16e958d | 2010-11-15 13:34:03 +0100 | [diff] [blame] | 97 | subscr_put(subscr); |
| 98 | return 0; |
| 99 | } |
Holger Hans Peter Freyther | 076af1c | 2011-06-07 19:57:02 +0200 | [diff] [blame] | 100 | struct osmo_msc_data *bsc_find_msc(struct gsm_subscriber_connection *conn, |
| 101 | struct msgb *msg) |
| 102 | { |
| 103 | struct osmo_bsc_data *bsc; |
| 104 | struct osmo_msc_data *msc; |
| 105 | |
| 106 | bsc = conn->bts->network->bsc_data; |
| 107 | llist_for_each_entry(msc, &bsc->mscs, entry) { |
| 108 | if (!msc->msc_con->is_authenticated) |
| 109 | continue; |
| 110 | |
| 111 | /* force round robin by moving it to the end */ |
| 112 | llist_move_tail(&msc->entry, &bsc->mscs); |
| 113 | return msc; |
| 114 | } |
| 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Holger Hans Peter Freyther | 16e958d | 2010-11-15 13:34:03 +0100 | [diff] [blame] | 119 | |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 120 | /** |
| 121 | * This is used to scan a message for extra functionality of the BSC. This |
| 122 | * includes scanning for location updating requests/acceptd and then send |
| 123 | * a welcome USSD message to the subscriber. |
| 124 | */ |
| 125 | int bsc_scan_bts_msg(struct gsm_subscriber_connection *conn, struct msgb *msg) |
| 126 | { |
Holger Hans Peter Freyther | d65305f | 2010-11-05 11:31:08 +0100 | [diff] [blame] | 127 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 128 | uint8_t pdisc = gh->proto_discr & 0x0f; |
| 129 | uint8_t mtype = gh->msg_type & 0xbf; |
| 130 | |
| 131 | if (pdisc == GSM48_PDISC_MM) { |
| 132 | if (mtype == GSM48_MT_MM_LOC_UPD_REQUEST) |
| 133 | handle_lu_request(conn, msg); |
Holger Hans Peter Freyther | 16e958d | 2010-11-15 13:34:03 +0100 | [diff] [blame] | 134 | } else if (pdisc == GSM48_PDISC_RR) { |
| 135 | if (mtype == GSM48_MT_RR_PAG_RESP) |
| 136 | handle_page_resp(conn, msg); |
Holger Hans Peter Freyther | d65305f | 2010-11-05 11:31:08 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 139 | return 0; |
| 140 | } |
Holger Hans Peter Freyther | a54732d | 2010-11-05 18:11:19 +0100 | [diff] [blame] | 141 | |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 142 | static void send_welcome_ussd(struct gsm_subscriber_connection *conn) |
| 143 | { |
Holger Hans Peter Freyther | f936fb4 | 2011-06-04 15:12:57 +0200 | [diff] [blame] | 144 | struct osmo_bsc_sccp_con *bsc; |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 145 | |
Holger Hans Peter Freyther | f936fb4 | 2011-06-04 15:12:57 +0200 | [diff] [blame] | 146 | bsc = conn->sccp_con; |
| 147 | if (!bsc || !bsc->msc->ussd_welcome_txt); |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 148 | return; |
| 149 | |
Holger Hans Peter Freyther | f936fb4 | 2011-06-04 15:12:57 +0200 | [diff] [blame] | 150 | gsm0480_send_ussdNotify(conn, 1, bsc->msc->ussd_welcome_txt); |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 151 | gsm0480_send_releaseComplete(conn); |
| 152 | } |
| 153 | |
Holger Hans Peter Freyther | a54732d | 2010-11-05 18:11:19 +0100 | [diff] [blame] | 154 | /** |
| 155 | * Messages coming back from the MSC. |
| 156 | */ |
| 157 | int bsc_scan_msc_msg(struct gsm_subscriber_connection *conn, struct msgb *msg) |
| 158 | { |
Holger Hans Peter Freyther | f936fb4 | 2011-06-04 15:12:57 +0200 | [diff] [blame] | 159 | struct osmo_msc_data *msc; |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 160 | struct gsm_network *net; |
| 161 | struct gsm48_loc_area_id *lai; |
| 162 | struct gsm48_hdr *gh; |
| 163 | uint8_t mtype; |
| 164 | |
| 165 | if (msgb_l3len(msg) < sizeof(*gh)) { |
| 166 | LOGP(DMSC, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | gh = (struct gsm48_hdr *) msgb_l3(msg); |
| 171 | mtype = gh->msg_type & 0xbf; |
| 172 | net = conn->bts->network; |
Holger Hans Peter Freyther | f936fb4 | 2011-06-04 15:12:57 +0200 | [diff] [blame] | 173 | msc = conn->sccp_con->msc; |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 174 | |
| 175 | if (mtype == GSM48_MT_MM_LOC_UPD_ACCEPT) { |
Holger Hans Peter Freyther | f936fb4 | 2011-06-04 15:12:57 +0200 | [diff] [blame] | 176 | if (msc->core_ncc != -1 || msc->core_mcc != -1) { |
Holger Hans Peter Freyther | 52d42ab | 2010-11-05 18:41:04 +0100 | [diff] [blame] | 177 | if (msgb_l3len(msg) >= sizeof(*gh) + sizeof(*lai)) { |
| 178 | lai = (struct gsm48_loc_area_id *) &gh->data[0]; |
| 179 | gsm48_generate_lai(lai, net->country_code, |
| 180 | net->network_code, |
| 181 | conn->bts->location_area_code); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (conn->sccp_con->new_subscriber) |
| 186 | send_welcome_ussd(conn); |
| 187 | } |
| 188 | |
Holger Hans Peter Freyther | a54732d | 2010-11-05 18:11:19 +0100 | [diff] [blame] | 189 | return 0; |
| 190 | } |