blob: fd38f78006a158bb084e2ea16e5249ea3b66b1c6 [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
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020035#include <sys/socket.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020036#include <string.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020037#include <unistd.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020038
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020039struct bsc_nat_ussd_con {
40 struct write_queue queue;
41 struct bsc_nat *nat;
42 int authorized;
43
44 struct timer_list auth_timeout;
45};
46
47static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *);
48
49static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat)
50{
51 struct bsc_nat_ussd_con *con;
52
53 con = talloc_zero(nat, struct bsc_nat_ussd_con);
54 if (!con)
55 return NULL;
56
57 con->nat = nat;
58 return con;
59}
60
61static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con)
62{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020063 if (con->nat->ussd_con == con) {
64 bsc_close_ussd_connections(con->nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020065 con->nat->ussd_con = NULL;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020066 }
67
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020068 close(con->queue.bfd.fd);
69 bsc_unregister_fd(&con->queue.bfd);
70 bsc_del_timer(&con->auth_timeout);
71 write_queue_clear(&con->queue);
72 talloc_free(con);
73}
74
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +020075static int forward_sccp(struct bsc_nat *nat, struct msgb *msg)
76{
77 struct sccp_connections *con;
78 struct bsc_nat_parsed *parsed;
79
80
81 parsed = bsc_nat_parse(msg);
82 if (!parsed) {
83 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from USSD.\n");
84 msgb_free(msg);
85 return -1;
86 }
87
88 if (!parsed->dest_local_ref) {
89 LOGP(DNAT, LOGL_ERROR, "No destination local reference.\n");
90 msgb_free(msg);
91 return -1;
92 }
93
94 con = bsc_nat_find_con_by_bsc(nat, parsed->dest_local_ref);
95 if (!con || !con->bsc) {
96 LOGP(DNAT, LOGL_ERROR, "No active connection found.\n");
97 msgb_free(msg);
98 return -1;
99 }
100
101 talloc_free(parsed);
102 bsc_write_msg(&con->bsc->write_queue, msg);
103 return 0;
104}
105
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200106static int ussd_read_cb(struct bsc_fd *bfd)
107{
108 int error;
109 struct bsc_nat_ussd_con *conn = bfd->data;
110 struct msgb *msg = ipaccess_read_msg(bfd, &error);
111 struct ipaccess_head *hh;
112
113 if (!msg) {
114 LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n");
115 bsc_nat_ussd_destroy(conn);
116 return -1;
117 }
118
119 LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n",
120 hexdump(msg->data, msg->len), msg->l2h[0]);
121 hh = (struct ipaccess_head *) msg->data;
122
123 if (hh->proto == IPAC_PROTO_IPACCESS) {
124 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
125 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200126 int ret;
127 ret = ipaccess_idtag_parse(&tvp,
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200128 (unsigned char *) msg->l2h + 2,
129 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200130 if (ret < 0) {
131 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
132 "message with malformed TLVs\n");
133 return ret;
134 }
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200135 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
136 ussd_auth_con(&tvp, conn);
137 }
138
139 msgb_free(msg);
140 } else if (hh->proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200141 forward_sccp(conn->nat, msg);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200142 } else {
143 msgb_free(msg);
144 }
145
146 return 0;
147}
148
149static void ussd_auth_cb(void *_data)
150{
151 LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n");
152 bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data);
153}
154
155static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn)
156{
157 const char *token;
158 int len;
159 if (!conn->nat->ussd_token) {
160 LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n");
161 bsc_nat_ussd_destroy(conn);
162 return;
163 }
164
165 token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
166 len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
167 if (strncmp(conn->nat->ussd_token, token, len) != 0) {
168 LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n",
169 conn->queue.bfd.fd);
170 bsc_nat_ussd_destroy(conn);
171 return;
172 }
173
174 /* it is authenticated now */
175 if (conn->nat->ussd_con && conn->nat->ussd_con != conn)
176 bsc_nat_ussd_destroy(conn->nat->ussd_con);
177
178 LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n");
179 bsc_del_timer(&conn->auth_timeout);
180 conn->authorized = 1;
181 conn->nat->ussd_con = conn;
182}
183
184static void ussd_start_auth(struct bsc_nat_ussd_con *conn)
185{
186 struct msgb *msg;
187
188 conn->auth_timeout.data = conn;
189 conn->auth_timeout.cb = ussd_auth_cb;
190 bsc_schedule_timer(&conn->auth_timeout, conn->nat->auth_timeout, 0);
191
192 msg = msgb_alloc_headroom(4096, 128, "auth message");
193 if (!msg) {
194 LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n");
195 return;
196 }
197
198 msgb_v_put(msg, IPAC_MSGT_ID_GET);
199 bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS);
200}
201
202static int ussd_listen_cb(struct bsc_fd *bfd, unsigned int what)
203{
204 struct bsc_nat_ussd_con *conn;
205 struct bsc_nat *nat;
206 struct sockaddr_in sa;
207 socklen_t sa_len = sizeof(sa);
208 int fd;
209
210 if (!(what & BSC_FD_READ))
211 return 0;
212
213 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
214 if (fd < 0) {
215 perror("accept");
216 return fd;
217 }
218
219 nat = (struct bsc_nat *) bfd->data;
220 counter_inc(nat->stats.ussd.reconn);
221
222 conn = bsc_nat_ussd_alloc(nat);
223 if (!conn) {
224 LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n");
225 close(fd);
226 return -1;
227 }
228
229 write_queue_init(&conn->queue, 10);
230 conn->queue.bfd.data = conn;
231 conn->queue.bfd.fd = fd;
232 conn->queue.bfd.when = BSC_FD_READ;
233 conn->queue.read_cb = ussd_read_cb;
234 conn->queue.write_cb = bsc_write_cb;
235
236 if (bsc_register_fd(&conn->queue.bfd) < 0) {
237 LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n");
238 bsc_nat_ussd_destroy(conn);
239 return -1;
240 }
241
242 LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n",
243 fd, inet_ntoa(sa.sin_addr));
244
245 /* do authentication */
246 ussd_start_auth(conn);
247 return 0;
248}
249
250int bsc_ussd_init(struct bsc_nat *nat)
251{
252 struct in_addr addr;
253
254 addr.s_addr = INADDR_ANY;
255 if (nat->ussd_local)
256 inet_aton(nat->ussd_local, &addr);
257
258 nat->ussd_listen.data = nat;
259 return make_sock(&nat->ussd_listen, IPPROTO_TCP,
Holger Hans Peter Freyther0d93fb42011-04-11 10:27:10 +0200260 ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200261}
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800262
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200263static int forward_ussd(struct sccp_connections *con, const struct ussd_request *req,
264 struct msgb *input)
265{
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200266 struct msgb *msg, *copy;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200267 struct ipac_msgt_sccp_state *state;
268 struct bsc_nat_ussd_con *ussd;
269
270 if (!con->bsc->nat->ussd_con)
271 return -1;
272
273 msg = msgb_alloc_headroom(4096, 128, "forward ussd");
274 if (!msg) {
275 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
276 return -1;
277 }
278
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200279 copy = msgb_alloc_headroom(4096, 128, "forward bts");
280 if (!copy) {
281 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
282 msgb_free(msg);
283 return -1;
284 }
285
286 copy->l2h = msgb_put(copy, msgb_l2len(input));
287 memcpy(copy->l2h, input->l2h, msgb_l2len(input));
288
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200289 msg->l2h = msgb_put(msg, 1);
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100290 msg->l2h[0] = IPAC_MSGT_SCCP_OLD;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200291
292 /* fill out the data */
293 state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state));
294 state->trans_id = req->transaction_id;
295 state->invoke_id = req->invoke_id;
296 memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref));
297 memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref));
298 memcpy(state->imsi, con->imsi, strlen(con->imsi));
299
300 ussd = con->bsc->nat->ussd_con;
301 bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200302 bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200303
304 return 0;
305}
306
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800307int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed,
308 struct msgb *msg)
309{
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200310 uint32_t len;
311 uint8_t msg_type;
312 struct gsm48_hdr *hdr48;
313 struct bsc_nat_acc_lst *lst;
314 struct ussd_request req;
315
316 /*
317 * various checks to avoid the decoding work. Right now we only want to
318 * decode if the connection was created for USSD, we do have a USSD access
319 * list, a query, a IMSI and such...
320 */
321 if (con->con_type != NAT_CON_TYPE_SSA)
322 return 0;
323
324 if (!con->imsi)
325 return 0;
326
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200327 /* We have not verified the IMSI yet */
328 if (!con->authorized)
329 return 0;
330
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200331 if (!con->bsc->nat->ussd_lst_name)
332 return 0;
333 if (!con->bsc->nat->ussd_query)
334 return 0;
335
336 if (parsed->bssap != BSSAP_MSG_DTAP)
337 return 0;
338
339 if (strlen(con->imsi) > GSM_IMSI_LENGTH)
340 return 0;
341
342 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
343 if (!hdr48)
344 return 0;
345
346 msg_type = hdr48->msg_type & 0xbf;
347 if (hdr48->proto_discr != GSM48_PDISC_NC_SS || msg_type != GSM0480_MTYPE_REGISTER)
348 return 0;
349
350 /* now check if it is a IMSI we care about */
351 lst = bsc_nat_acc_lst_find(con->bsc->nat, con->bsc->nat->ussd_lst_name);
352 if (!lst)
353 return 0;
354
355 if (bsc_nat_lst_check_allow(lst, con->imsi) != 0)
356 return 0;
357
358 /* now decode the message and see if we really want to handle it */
359 memset(&req, 0, sizeof(req));
360 if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1)
361 return 0;
362 if (req.text[0] == 0xff)
363 return 0;
364
Holger Hans Peter Freythera18b1162011-04-01 17:32:21 +0200365 if (regexec(&con->bsc->nat->ussd_query_re, req.text, 0, NULL, 0) == REG_NOMATCH)
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200366 return 0;
367
368 /* found a USSD query for our subscriber */
369 LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi);
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200370 if (forward_ussd(con, &req, msg) != 0)
371 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200372 return 1;
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800373}