blob: cd40566eb8282bf0bf378ddb9883fb47167b2397 [file] [log] [blame]
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001/* USSD Filter Code */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010 by On-Waves
6 * 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 General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <openbsc/bsc_nat.h>
25#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020026#include <openbsc/ipaccess.h>
27#include <openbsc/socket.h>
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080028
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020029#include <osmocore/protocol/gsm_08_08.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020030#include <osmocore/gsm0480.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020031#include <osmocore/talloc.h>
32#include <osmocore/tlv.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020033
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +020034#include <osmocom/sccp/sccp.h>
35
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020036#include <sys/socket.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020037#include <string.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020038#include <unistd.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020039
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020040struct bsc_nat_ussd_con {
41 struct write_queue queue;
42 struct bsc_nat *nat;
43 int authorized;
44
45 struct timer_list auth_timeout;
46};
47
48static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *);
49
50static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat)
51{
52 struct bsc_nat_ussd_con *con;
53
54 con = talloc_zero(nat, struct bsc_nat_ussd_con);
55 if (!con)
56 return NULL;
57
58 con->nat = nat;
59 return con;
60}
61
62static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con)
63{
64 if (con->nat->ussd_con == con)
65 con->nat->ussd_con = NULL;
66 close(con->queue.bfd.fd);
67 bsc_unregister_fd(&con->queue.bfd);
68 bsc_del_timer(&con->auth_timeout);
69 write_queue_clear(&con->queue);
70 talloc_free(con);
71}
72
73static int ussd_read_cb(struct bsc_fd *bfd)
74{
75 int error;
76 struct bsc_nat_ussd_con *conn = bfd->data;
77 struct msgb *msg = ipaccess_read_msg(bfd, &error);
78 struct ipaccess_head *hh;
79
80 if (!msg) {
81 LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n");
82 bsc_nat_ussd_destroy(conn);
83 return -1;
84 }
85
86 LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n",
87 hexdump(msg->data, msg->len), msg->l2h[0]);
88 hh = (struct ipaccess_head *) msg->data;
89
90 if (hh->proto == IPAC_PROTO_IPACCESS) {
91 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
92 struct tlv_parsed tvp;
93 ipaccess_idtag_parse(&tvp,
94 (unsigned char *) msg->l2h + 2,
95 msgb_l2len(msg) - 2);
96 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
97 ussd_auth_con(&tvp, conn);
98 }
99
100 msgb_free(msg);
101 } else if (hh->proto == IPAC_PROTO_SCCP) {
102 LOGP(DNAT, LOGL_ERROR, "USSD SCCP is not handled\n");
103 msgb_free(msg);
104 } else {
105 msgb_free(msg);
106 }
107
108 return 0;
109}
110
111static void ussd_auth_cb(void *_data)
112{
113 LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n");
114 bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data);
115}
116
117static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn)
118{
119 const char *token;
120 int len;
121 if (!conn->nat->ussd_token) {
122 LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n");
123 bsc_nat_ussd_destroy(conn);
124 return;
125 }
126
127 token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
128 len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
129 if (strncmp(conn->nat->ussd_token, token, len) != 0) {
130 LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n",
131 conn->queue.bfd.fd);
132 bsc_nat_ussd_destroy(conn);
133 return;
134 }
135
136 /* it is authenticated now */
137 if (conn->nat->ussd_con && conn->nat->ussd_con != conn)
138 bsc_nat_ussd_destroy(conn->nat->ussd_con);
139
140 LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n");
141 bsc_del_timer(&conn->auth_timeout);
142 conn->authorized = 1;
143 conn->nat->ussd_con = conn;
144}
145
146static void ussd_start_auth(struct bsc_nat_ussd_con *conn)
147{
148 struct msgb *msg;
149
150 conn->auth_timeout.data = conn;
151 conn->auth_timeout.cb = ussd_auth_cb;
152 bsc_schedule_timer(&conn->auth_timeout, conn->nat->auth_timeout, 0);
153
154 msg = msgb_alloc_headroom(4096, 128, "auth message");
155 if (!msg) {
156 LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n");
157 return;
158 }
159
160 msgb_v_put(msg, IPAC_MSGT_ID_GET);
161 bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS);
162}
163
164static int ussd_listen_cb(struct bsc_fd *bfd, unsigned int what)
165{
166 struct bsc_nat_ussd_con *conn;
167 struct bsc_nat *nat;
168 struct sockaddr_in sa;
169 socklen_t sa_len = sizeof(sa);
170 int fd;
171
172 if (!(what & BSC_FD_READ))
173 return 0;
174
175 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
176 if (fd < 0) {
177 perror("accept");
178 return fd;
179 }
180
181 nat = (struct bsc_nat *) bfd->data;
182 counter_inc(nat->stats.ussd.reconn);
183
184 conn = bsc_nat_ussd_alloc(nat);
185 if (!conn) {
186 LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n");
187 close(fd);
188 return -1;
189 }
190
191 write_queue_init(&conn->queue, 10);
192 conn->queue.bfd.data = conn;
193 conn->queue.bfd.fd = fd;
194 conn->queue.bfd.when = BSC_FD_READ;
195 conn->queue.read_cb = ussd_read_cb;
196 conn->queue.write_cb = bsc_write_cb;
197
198 if (bsc_register_fd(&conn->queue.bfd) < 0) {
199 LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n");
200 bsc_nat_ussd_destroy(conn);
201 return -1;
202 }
203
204 LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n",
205 fd, inet_ntoa(sa.sin_addr));
206
207 /* do authentication */
208 ussd_start_auth(conn);
209 return 0;
210}
211
212int bsc_ussd_init(struct bsc_nat *nat)
213{
214 struct in_addr addr;
215
216 addr.s_addr = INADDR_ANY;
217 if (nat->ussd_local)
218 inet_aton(nat->ussd_local, &addr);
219
220 nat->ussd_listen.data = nat;
221 return make_sock(&nat->ussd_listen, IPPROTO_TCP,
222 ntohl(addr.s_addr), 5001, ussd_listen_cb);
223}
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800224
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200225static int forward_ussd(struct sccp_connections *con, const struct ussd_request *req,
226 struct msgb *input)
227{
228 struct msgb *msg;
229 struct ipac_msgt_sccp_state *state;
230 struct bsc_nat_ussd_con *ussd;
231
232 if (!con->bsc->nat->ussd_con)
233 return -1;
234
235 msg = msgb_alloc_headroom(4096, 128, "forward ussd");
236 if (!msg) {
237 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
238 return -1;
239 }
240
241 msg->l2h = msgb_put(msg, 1);
242 msg->l2h[0] = IPAC_MSGT_SCCP_STATE;
243
244 /* fill out the data */
245 state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state));
246 state->trans_id = req->transaction_id;
247 state->invoke_id = req->invoke_id;
248 memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref));
249 memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref));
250 memcpy(state->imsi, con->imsi, strlen(con->imsi));
251
252 ussd = con->bsc->nat->ussd_con;
253 bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS);
254
255 return 0;
256}
257
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800258int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed,
259 struct msgb *msg)
260{
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200261 uint32_t len;
262 uint8_t msg_type;
263 struct gsm48_hdr *hdr48;
264 struct bsc_nat_acc_lst *lst;
265 struct ussd_request req;
266
267 /*
268 * various checks to avoid the decoding work. Right now we only want to
269 * decode if the connection was created for USSD, we do have a USSD access
270 * list, a query, a IMSI and such...
271 */
272 if (con->con_type != NAT_CON_TYPE_SSA)
273 return 0;
274
275 if (!con->imsi)
276 return 0;
277
278 if (!con->bsc->nat->ussd_lst_name)
279 return 0;
280 if (!con->bsc->nat->ussd_query)
281 return 0;
282
283 if (parsed->bssap != BSSAP_MSG_DTAP)
284 return 0;
285
286 if (strlen(con->imsi) > GSM_IMSI_LENGTH)
287 return 0;
288
289 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
290 if (!hdr48)
291 return 0;
292
293 msg_type = hdr48->msg_type & 0xbf;
294 if (hdr48->proto_discr != GSM48_PDISC_NC_SS || msg_type != GSM0480_MTYPE_REGISTER)
295 return 0;
296
297 /* now check if it is a IMSI we care about */
298 lst = bsc_nat_acc_lst_find(con->bsc->nat, con->bsc->nat->ussd_lst_name);
299 if (!lst)
300 return 0;
301
302 if (bsc_nat_lst_check_allow(lst, con->imsi) != 0)
303 return 0;
304
305 /* now decode the message and see if we really want to handle it */
306 memset(&req, 0, sizeof(req));
307 if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1)
308 return 0;
309 if (req.text[0] == 0xff)
310 return 0;
311
312 if (strcmp(req.text, con->bsc->nat->ussd_query) != 0)
313 return 0;
314
315 /* found a USSD query for our subscriber */
316 LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi);
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200317 if (forward_ussd(con, &req, msg) != 0)
318 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200319 return 1;
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800320}