blob: 3bc2dfd7fdb649566ce3477073adcbd7842f8c8f [file] [log] [blame]
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +02001/* (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
2 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freyther5bb874d2010-11-05 11:21:18 +01003 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01006 * 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 Freyther5bb874d2010-11-05 11:21:18 +01008 * (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 Welte9af6ddf2011-01-01 15:25:50 +010013 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther5bb874d2010-11-05 11:21:18 +010014 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * 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 Freyther5bb874d2010-11-05 11:21:18 +010017 *
18 */
19
20#include <openbsc/osmo_bsc.h>
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +010021#include <openbsc/osmo_msc_data.h>
22#include <openbsc/gsm_04_80.h>
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +010023#include <openbsc/gsm_subscriber.h>
Holger Hans Peter Freytherd65305f2010-11-05 11:31:08 +010024#include <openbsc/debug.h>
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +010025#include <openbsc/paging.h>
26
27#include <stdlib.h>
Holger Hans Peter Freytherd65305f2010-11-05 11:31:08 +010028
29static 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 Freyther5bb874d2010-11-05 11:21:18 +010055
Holger Hans Peter Freytherf67d9a92011-06-07 20:12:33 +020056/* extract a subscriber from the paging response */
57static struct gsm_subscriber *extract_sub(struct gsm_subscriber_connection *conn,
58 struct msgb *msg)
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +010059{
60 uint8_t mi_type;
61 char mi_string[GSM48_MI_SIZE];
62 struct gsm48_hdr *gh;
63 struct gsm48_pag_resp *resp;
64 struct gsm_subscriber *subscr;
65
66 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*resp)) {
67 LOGP(DMSC, LOGL_ERROR, "PagingResponse too small: %u\n", msgb_l3len(msg));
Holger Hans Peter Freytherf67d9a92011-06-07 20:12:33 +020068 return NULL;
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +010069 }
70
71 gh = msgb_l3(msg);
72 resp = (struct gsm48_pag_resp *) &gh->data[0];
73
74 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
75 mi_string, &mi_type);
76 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
77 mi_type, mi_string);
78
79 switch (mi_type) {
80 case GSM_MI_TYPE_TMSI:
81 subscr = subscr_active_by_tmsi(conn->bts->network,
82 tmsi_from_string(mi_string));
83 break;
84 case GSM_MI_TYPE_IMSI:
85 subscr = subscr_active_by_imsi(conn->bts->network, mi_string);
86 break;
Holger Hans Peter Freyther3fbd2442011-01-16 18:18:56 +010087 default:
88 subscr = NULL;
89 break;
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +010090 }
91
Holger Hans Peter Freytherf67d9a92011-06-07 20:12:33 +020092 return subscr;
93}
94
95/* we will need to stop the paging request */
96static int handle_page_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
97{
98 struct gsm_subscriber *subscr = extract_sub(conn, msg);
99
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +0100100 if (!subscr) {
101 LOGP(DMSC, LOGL_ERROR, "Non active subscriber got paged.\n");
102 return -1;
103 }
104
Harald Weltef604bba2010-12-15 15:33:08 +0100105 paging_request_stop(conn->bts, subscr, conn, msg);
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +0100106 subscr_put(subscr);
107 return 0;
108}
Holger Hans Peter Freyther076af1c2011-06-07 19:57:02 +0200109struct osmo_msc_data *bsc_find_msc(struct gsm_subscriber_connection *conn,
110 struct msgb *msg)
111{
Holger Hans Peter Freytherf67d9a92011-06-07 20:12:33 +0200112 struct gsm48_hdr *gh;
113 int8_t pdisc;
114 uint8_t mtype;
Holger Hans Peter Freyther076af1c2011-06-07 19:57:02 +0200115 struct osmo_bsc_data *bsc;
Holger Hans Peter Freytherf67d9a92011-06-07 20:12:33 +0200116 struct osmo_msc_data *msc, *pag_msc;
117 struct gsm_subscriber *subscr;
Holger Hans Peter Freyther076af1c2011-06-07 19:57:02 +0200118
119 bsc = conn->bts->network->bsc_data;
Holger Hans Peter Freytherf67d9a92011-06-07 20:12:33 +0200120
121 if (msgb_l3len(msg) < sizeof(*gh)) {
122 LOGP(DMSC, LOGL_ERROR, "There is no GSM48 header here.\n");
123 return NULL;
124 }
125
126 gh = msgb_l3(msg);
127 pdisc = gh->proto_discr & 0x0f;
128 mtype = gh->msg_type & 0xbf;
129
130 /*
131 * We are asked to select a MSC here but they are not equal. We
132 * want to respond to a paging request on the MSC where we got the
133 * request from. This is where we need to decide where this connection
134 * will go.
135 */
136 if (pdisc == GSM48_PDISC_RR && mtype == GSM48_MT_RR_PAG_RESP)
137 goto paging;
138 else
139 goto round_robin;
140
141round_robin:
Holger Hans Peter Freyther076af1c2011-06-07 19:57:02 +0200142 llist_for_each_entry(msc, &bsc->mscs, entry) {
143 if (!msc->msc_con->is_authenticated)
144 continue;
Holger Hans Peter Freyther210565e2011-06-07 20:56:18 +0200145 if (msc->type != MSC_CON_TYPE_NORMAL)
146 continue;
Holger Hans Peter Freyther076af1c2011-06-07 19:57:02 +0200147
148 /* force round robin by moving it to the end */
149 llist_move_tail(&msc->entry, &bsc->mscs);
150 return msc;
151 }
152
153 return NULL;
Holger Hans Peter Freytherf67d9a92011-06-07 20:12:33 +0200154
155paging:
156 subscr = extract_sub(conn, msg);
157
158 if (!subscr) {
159 LOGP(DMSC, LOGL_ERROR, "Got paged but no subscriber found.\n");
160 return NULL;
161 }
162
163 pag_msc = paging_get_data(conn->bts, subscr);
164 subscr_put(subscr);
165
166 llist_for_each_entry(msc, &bsc->mscs, entry) {
167 if (msc != pag_msc)
168 continue;
169
170 /*
171 * We don't check if the MSC is connected. In case it
172 * is not the connection will be dropped.
173 */
174
175 /* force round robin by moving it to the end */
176 llist_move_tail(&msc->entry, &bsc->mscs);
177 return msc;
178 }
179
180 LOGP(DMSC, LOGL_ERROR, "Got paged but no request found.\n");
181 return NULL;
Holger Hans Peter Freyther076af1c2011-06-07 19:57:02 +0200182}
183
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +0100184
Holger Hans Peter Freyther5bb874d2010-11-05 11:21:18 +0100185/**
186 * This is used to scan a message for extra functionality of the BSC. This
187 * includes scanning for location updating requests/acceptd and then send
188 * a welcome USSD message to the subscriber.
189 */
190int bsc_scan_bts_msg(struct gsm_subscriber_connection *conn, struct msgb *msg)
191{
Holger Hans Peter Freytherd65305f2010-11-05 11:31:08 +0100192 struct gsm48_hdr *gh = msgb_l3(msg);
193 uint8_t pdisc = gh->proto_discr & 0x0f;
194 uint8_t mtype = gh->msg_type & 0xbf;
195
196 if (pdisc == GSM48_PDISC_MM) {
197 if (mtype == GSM48_MT_MM_LOC_UPD_REQUEST)
198 handle_lu_request(conn, msg);
Holger Hans Peter Freyther16e958d2010-11-15 13:34:03 +0100199 } else if (pdisc == GSM48_PDISC_RR) {
200 if (mtype == GSM48_MT_RR_PAG_RESP)
201 handle_page_resp(conn, msg);
Holger Hans Peter Freytherd65305f2010-11-05 11:31:08 +0100202 }
203
Holger Hans Peter Freyther5bb874d2010-11-05 11:21:18 +0100204 return 0;
205}
Holger Hans Peter Freythera54732d2010-11-05 18:11:19 +0100206
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +0100207static void send_welcome_ussd(struct gsm_subscriber_connection *conn)
208{
Holger Hans Peter Freytherf936fb42011-06-04 15:12:57 +0200209 struct osmo_bsc_sccp_con *bsc;
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +0100210
Holger Hans Peter Freytherf936fb42011-06-04 15:12:57 +0200211 bsc = conn->sccp_con;
212 if (!bsc || !bsc->msc->ussd_welcome_txt);
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +0100213 return;
214
Holger Hans Peter Freytherf936fb42011-06-04 15:12:57 +0200215 gsm0480_send_ussdNotify(conn, 1, bsc->msc->ussd_welcome_txt);
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +0100216 gsm0480_send_releaseComplete(conn);
217}
218
Holger Hans Peter Freythera54732d2010-11-05 18:11:19 +0100219/**
220 * Messages coming back from the MSC.
221 */
222int bsc_scan_msc_msg(struct gsm_subscriber_connection *conn, struct msgb *msg)
223{
Holger Hans Peter Freytherf936fb42011-06-04 15:12:57 +0200224 struct osmo_msc_data *msc;
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +0100225 struct gsm_network *net;
226 struct gsm48_loc_area_id *lai;
227 struct gsm48_hdr *gh;
228 uint8_t mtype;
229
230 if (msgb_l3len(msg) < sizeof(*gh)) {
231 LOGP(DMSC, LOGL_ERROR, "GSM48 header does not fit.\n");
232 return -1;
233 }
234
235 gh = (struct gsm48_hdr *) msgb_l3(msg);
236 mtype = gh->msg_type & 0xbf;
237 net = conn->bts->network;
Holger Hans Peter Freytherf936fb42011-06-04 15:12:57 +0200238 msc = conn->sccp_con->msc;
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +0100239
240 if (mtype == GSM48_MT_MM_LOC_UPD_ACCEPT) {
Holger Hans Peter Freytherf936fb42011-06-04 15:12:57 +0200241 if (msc->core_ncc != -1 || msc->core_mcc != -1) {
Holger Hans Peter Freyther52d42ab2010-11-05 18:41:04 +0100242 if (msgb_l3len(msg) >= sizeof(*gh) + sizeof(*lai)) {
243 lai = (struct gsm48_loc_area_id *) &gh->data[0];
244 gsm48_generate_lai(lai, net->country_code,
245 net->network_code,
246 conn->bts->location_area_code);
247 }
248 }
249
250 if (conn->sccp_con->new_subscriber)
251 send_welcome_ussd(conn);
252 }
253
Holger Hans Peter Freythera54732d2010-11-05 18:11:19 +0100254 return 0;
255}