blob: f972ba5ef2b9398dcc4269afa280ef2cbfc8af2c [file] [log] [blame]
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001/* USSD Filter Code */
2
3/*
Holger Hans Peter Freythera18b1162011-04-01 17:32:21 +02004 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2011 by On-Waves
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * 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
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * 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/>.
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080020 *
21 */
22
23#include <openbsc/bsc_nat.h>
24#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020025#include <openbsc/ipaccess.h>
26#include <openbsc/socket.h>
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080027
Harald Welted36ff762011-03-23 18:26:56 +010028#include <osmocom/gsm/protocol/gsm_08_08.h>
29#include <osmocom/gsm/gsm0480.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010030#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010031#include <osmocom/gsm/tlv.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020032
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +020033#include <osmocom/sccp/sccp.h>
34
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020035#include <osmocom/abis/ipa.h>
36
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020037#include <sys/socket.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020038#include <string.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020039#include <unistd.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020040
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020041static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *);
42
43static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat)
44{
45 struct bsc_nat_ussd_con *con;
46
47 con = talloc_zero(nat, struct bsc_nat_ussd_con);
48 if (!con)
49 return NULL;
50
51 con->nat = nat;
52 return con;
53}
54
55static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con)
56{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020057 if (con->nat->ussd_con == con) {
58 bsc_close_ussd_connections(con->nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020059 con->nat->ussd_con = NULL;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020060 }
61
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020062 close(con->queue.bfd.fd);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020063 osmo_fd_unregister(&con->queue.bfd);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020064 osmo_timer_del(&con->auth_timeout);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +020065 osmo_wqueue_clear(&con->queue);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020066 talloc_free(con);
67}
68
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +020069static int forward_sccp(struct bsc_nat *nat, struct msgb *msg)
70{
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +020071 struct nat_sccp_connection *con;
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +020072 struct bsc_nat_parsed *parsed;
73
74
75 parsed = bsc_nat_parse(msg);
76 if (!parsed) {
77 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from USSD.\n");
78 msgb_free(msg);
79 return -1;
80 }
81
82 if (!parsed->dest_local_ref) {
83 LOGP(DNAT, LOGL_ERROR, "No destination local reference.\n");
84 msgb_free(msg);
85 return -1;
86 }
87
88 con = bsc_nat_find_con_by_bsc(nat, parsed->dest_local_ref);
89 if (!con || !con->bsc) {
90 LOGP(DNAT, LOGL_ERROR, "No active connection found.\n");
91 msgb_free(msg);
92 return -1;
93 }
94
95 talloc_free(parsed);
96 bsc_write_msg(&con->bsc->write_queue, msg);
97 return 0;
98}
99
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200100static int ussd_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200101{
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200102 struct bsc_nat_ussd_con *conn = bfd->data;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200103 struct msgb *msg;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200104 struct ipaccess_head *hh;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200105 int ret;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200106
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200107 ret = ipa_msg_recv(bfd->fd, &msg);
108 if (ret <= 0) {
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200109 LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n");
110 bsc_nat_ussd_destroy(conn);
111 return -1;
112 }
113
114 LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200115 osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200116 hh = (struct ipaccess_head *) msg->data;
117
118 if (hh->proto == IPAC_PROTO_IPACCESS) {
119 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
120 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200121 int ret;
122 ret = ipaccess_idtag_parse(&tvp,
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200123 (unsigned char *) msg->l2h + 2,
124 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200125 if (ret < 0) {
126 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
127 "message with malformed TLVs\n");
128 return ret;
129 }
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200130 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
131 ussd_auth_con(&tvp, conn);
132 }
133
134 msgb_free(msg);
135 } else if (hh->proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200136 forward_sccp(conn->nat, msg);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200137 } else {
138 msgb_free(msg);
139 }
140
141 return 0;
142}
143
144static void ussd_auth_cb(void *_data)
145{
146 LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n");
147 bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data);
148}
149
150static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn)
151{
152 const char *token;
153 int len;
154 if (!conn->nat->ussd_token) {
155 LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n");
156 bsc_nat_ussd_destroy(conn);
157 return;
158 }
159
160 token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
161 len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freyther2bc90c22013-04-22 10:54:02 +0200162
163 /* last byte should be a NULL */
164 if (strlen(conn->nat->ussd_token) != len - 1)
165 goto disconnect;
166 /* compare everything including the null byte */
167 if (memcmp(conn->nat->ussd_token, token, len) != 0)
168 goto disconnect;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200169
170 /* it is authenticated now */
171 if (conn->nat->ussd_con && conn->nat->ussd_con != conn)
172 bsc_nat_ussd_destroy(conn->nat->ussd_con);
173
174 LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n");
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200175 osmo_timer_del(&conn->auth_timeout);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200176 conn->authorized = 1;
177 conn->nat->ussd_con = conn;
Holger Hans Peter Freyther2bc90c22013-04-22 10:54:02 +0200178 return;
179
180disconnect:
181 LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n",
182 conn->queue.bfd.fd);
183 bsc_nat_ussd_destroy(conn);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200184}
185
186static void ussd_start_auth(struct bsc_nat_ussd_con *conn)
187{
188 struct msgb *msg;
189
190 conn->auth_timeout.data = conn;
191 conn->auth_timeout.cb = ussd_auth_cb;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200192 osmo_timer_schedule(&conn->auth_timeout, conn->nat->auth_timeout, 0);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200193
194 msg = msgb_alloc_headroom(4096, 128, "auth message");
195 if (!msg) {
196 LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n");
197 return;
198 }
199
200 msgb_v_put(msg, IPAC_MSGT_ID_GET);
201 bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS);
202}
203
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200204static int ussd_listen_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200205{
206 struct bsc_nat_ussd_con *conn;
207 struct bsc_nat *nat;
208 struct sockaddr_in sa;
209 socklen_t sa_len = sizeof(sa);
210 int fd;
211
212 if (!(what & BSC_FD_READ))
213 return 0;
214
215 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
216 if (fd < 0) {
217 perror("accept");
218 return fd;
219 }
220
221 nat = (struct bsc_nat *) bfd->data;
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200222 osmo_counter_inc(nat->stats.ussd.reconn);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200223
224 conn = bsc_nat_ussd_alloc(nat);
225 if (!conn) {
226 LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n");
227 close(fd);
228 return -1;
229 }
230
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200231 osmo_wqueue_init(&conn->queue, 10);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200232 conn->queue.bfd.data = conn;
233 conn->queue.bfd.fd = fd;
234 conn->queue.bfd.when = BSC_FD_READ;
235 conn->queue.read_cb = ussd_read_cb;
236 conn->queue.write_cb = bsc_write_cb;
237
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200238 if (osmo_fd_register(&conn->queue.bfd) < 0) {
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200239 LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n");
240 bsc_nat_ussd_destroy(conn);
241 return -1;
242 }
243
244 LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n",
245 fd, inet_ntoa(sa.sin_addr));
246
247 /* do authentication */
248 ussd_start_auth(conn);
249 return 0;
250}
251
252int bsc_ussd_init(struct bsc_nat *nat)
253{
254 struct in_addr addr;
255
256 addr.s_addr = INADDR_ANY;
257 if (nat->ussd_local)
258 inet_aton(nat->ussd_local, &addr);
259
260 nat->ussd_listen.data = nat;
261 return make_sock(&nat->ussd_listen, IPPROTO_TCP,
Holger Hans Peter Freyther0d93fb42011-04-11 10:27:10 +0200262 ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200263}
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800264
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200265static int forward_ussd_simple(struct nat_sccp_connection *con, struct msgb *input)
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200266{
267 struct msgb *copy;
268 struct bsc_nat_ussd_con *ussd;
269
270 if (!con->bsc->nat->ussd_con)
271 return -1;
272
273 copy = msgb_alloc_headroom(4096, 128, "forward bts");
274 if (!copy) {
275 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
276 return -1;
277 }
278
279 /* copy the data into the copy */
280 copy->l2h = msgb_put(copy, msgb_l2len(input));
281 memcpy(copy->l2h, input->l2h, msgb_l2len(input));
282
283 /* send it out */
284 ussd = con->bsc->nat->ussd_con;
285 bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP);
286 return 0;
287}
288
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200289static int forward_ussd(struct nat_sccp_connection *con, const struct ussd_request *req,
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200290 struct msgb *input)
291{
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200292 struct msgb *msg, *copy;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200293 struct ipac_msgt_sccp_state *state;
294 struct bsc_nat_ussd_con *ussd;
295
296 if (!con->bsc->nat->ussd_con)
297 return -1;
298
299 msg = msgb_alloc_headroom(4096, 128, "forward ussd");
300 if (!msg) {
301 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
302 return -1;
303 }
304
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200305 copy = msgb_alloc_headroom(4096, 128, "forward bts");
306 if (!copy) {
307 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
308 msgb_free(msg);
309 return -1;
310 }
311
312 copy->l2h = msgb_put(copy, msgb_l2len(input));
313 memcpy(copy->l2h, input->l2h, msgb_l2len(input));
314
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200315 msg->l2h = msgb_put(msg, 1);
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100316 msg->l2h[0] = IPAC_MSGT_SCCP_OLD;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200317
318 /* fill out the data */
319 state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state));
320 state->trans_id = req->transaction_id;
321 state->invoke_id = req->invoke_id;
322 memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref));
323 memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref));
324 memcpy(state->imsi, con->imsi, strlen(con->imsi));
325
326 ussd = con->bsc->nat->ussd_con;
327 bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200328 bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200329
330 return 0;
331}
332
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200333int bsc_check_ussd(struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed,
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800334 struct msgb *msg)
335{
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200336 uint32_t len;
337 uint8_t msg_type;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200338 uint8_t proto;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200339 uint8_t ti;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200340 struct gsm48_hdr *hdr48;
341 struct bsc_nat_acc_lst *lst;
342 struct ussd_request req;
343
344 /*
345 * various checks to avoid the decoding work. Right now we only want to
346 * decode if the connection was created for USSD, we do have a USSD access
347 * list, a query, a IMSI and such...
348 */
349 if (con->con_type != NAT_CON_TYPE_SSA)
350 return 0;
351
352 if (!con->imsi)
353 return 0;
354
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200355 /* We have not verified the IMSI yet */
356 if (!con->authorized)
357 return 0;
358
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200359 if (!con->bsc->nat->ussd_lst_name)
360 return 0;
361 if (!con->bsc->nat->ussd_query)
362 return 0;
363
364 if (parsed->bssap != BSSAP_MSG_DTAP)
365 return 0;
366
367 if (strlen(con->imsi) > GSM_IMSI_LENGTH)
368 return 0;
369
370 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
371 if (!hdr48)
372 return 0;
373
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200374 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200375 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200376 ti = (hdr48->proto_discr & 0x70) >> 4;
377 if (proto != GSM48_PDISC_NC_SS)
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200378 return 0;
379
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200380 if (msg_type == GSM0480_MTYPE_REGISTER) {
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200381
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200382 /* now check if it is a IMSI we care about */
383 lst = bsc_nat_acc_lst_find(con->bsc->nat,
384 con->bsc->nat->ussd_lst_name);
385 if (!lst)
386 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200387
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200388 if (bsc_nat_lst_check_allow(lst, con->imsi) != 0)
389 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200390
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200391 /* now decode the message and see if we really want to handle it */
392 memset(&req, 0, sizeof(req));
393 if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1)
394 return 0;
395 if (req.text[0] == 0xff)
396 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200397
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200398 if (regexec(&con->bsc->nat->ussd_query_re,
399 req.text, 0, NULL, 0) == REG_NOMATCH)
400 return 0;
401
402 /* found a USSD query for our subscriber */
403 LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi);
404 con->ussd_ti[ti] = 1;
405 if (forward_ussd(con, &req, msg) != 0)
406 return 0;
407 return 1;
408 } else if (msg_type == GSM0480_MTYPE_FACILITY && con->ussd_ti[ti]) {
409 LOGP(DNAT, LOGL_NOTICE, "Forwarding message part of TI: %d %s\n",
410 ti, con->imsi);
411 if (forward_ussd_simple(con, msg) != 0)
412 return 0;
413 return 1;
414 }
415
416 return 0;
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800417}