blob: b397b095e5b94750ee6ca05da255f273a96182e5 [file] [log] [blame]
Holger Hans Peter Freytherf3d1ae72010-11-03 16:40:03 +01001/* Interaction with the SCCP subsystem */
2/*
3 * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2009-2010 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <openbsc/gsm_data.h>
Holger Hans Peter Freytherb8781d22010-11-06 18:08:43 +010024#include <openbsc/osmo_bsc.h>
25#include <openbsc/osmo_bsc_grace.h>
Holger Hans Peter Freytherf3d1ae72010-11-03 16:40:03 +010026#include <openbsc/osmo_msc_data.h>
27#include <openbsc/debug.h>
28
29#include <osmocore/gsm0808.h>
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010030#include <osmocore/talloc.h>
Holger Hans Peter Freytherf3d1ae72010-11-03 16:40:03 +010031#include <osmocore/protocol/gsm_08_08.h>
32
33#include <osmocom/sccp/sccp.h>
34
Holger Hans Peter Freytherc97d9b42010-11-06 20:23:58 +010035/* SCCP helper */
36#define SCCP_IT_TIMER 60
37
Holger Hans Peter Freytherb8781d22010-11-06 18:08:43 +010038static LLIST_HEAD(active_connections);
39
40static void msc_outgoing_sccp_data(struct sccp_connection *conn,
41 struct msgb *msg, unsigned int len)
42{
43}
44
45static void msc_outgoing_sccp_state(struct sccp_connection *conn, int old_state)
46{
47}
48
Holger Hans Peter Freytherc97d9b42010-11-06 20:23:58 +010049static void sccp_it_timeout(void *_data)
50{
51 struct osmo_bsc_sccp_con *data =
52 (struct osmo_bsc_sccp_con *) _data;
53
54 sccp_connection_send_it(data->sccp);
55 bsc_schedule_timer(&data->sccp_it_timeout, SCCP_IT_TIMER, 0);
56}
57
58static void sccp_cc_timeout(void *_data)
59{
60 struct osmo_bsc_sccp_con *data =
61 (struct osmo_bsc_sccp_con *) _data;
62
63 if (data->sccp->connection_state >= SCCP_CONNECTION_STATE_ESTABLISHED)
64 return;
65
66 LOGP(DMSC, LOGL_ERROR, "The connection was never established.\n");
67
68
69 if (data->conn) {
70 gsm0808_clear(data->conn);
71 subscr_con_free(data->conn);
72 data->conn = NULL;
73 }
74 bsc_delete_connection(data);
75}
76
Holger Hans Peter Freytherf3d1ae72010-11-03 16:40:03 +010077static void msc_sccp_write_ipa(struct sccp_connection *conn, struct msgb *msg, void *data)
78{
79 LOGP(DMSC, LOGL_ERROR, "Writing is not implemented.\n");
80 msgb_free(msg);
81}
82
83static int msc_sccp_accept(struct sccp_connection *connection, void *data)
84{
85 LOGP(DMSC, LOGL_DEBUG, "Rejecting incoming SCCP connection.\n");
86 return -1;
87}
88
89static int msc_sccp_read(struct msgb *msgb, unsigned int length, void *data)
90{
91 struct bssmap_header *bs;
92
93 LOGP(DMSC, LOGL_DEBUG, "Incoming SCCP message ftom MSC: %s\n",
94 hexdump(msgb->l3h, length));
95
96 if (length < sizeof(*bs)) {
97 LOGP(DMSC, LOGL_ERROR, "The header is too short.\n");
98 return -1;
99 }
100
101 bs = (struct bssmap_header *) msgb->l3h;
102 if (bs->length < length - sizeof(*bs))
103 return -1;
104
105 switch (bs->type) {
106 case BSSAP_MSG_BSS_MANAGEMENT:
Holger Hans Peter Freyther1999cf12010-11-03 18:46:52 +0100107 LOGP(DMSC, LOGL_ERROR, "BSS management not implemented.\n");
Holger Hans Peter Freytherf3d1ae72010-11-03 16:40:03 +0100108 break;
109 default:
110 LOGP(DMSC, LOGL_ERROR, "Unimplemented msg type: %d\n", bs->type);
111 }
112
113 return 0;
114}
115
Holger Hans Peter Freyther45825862010-11-04 11:48:49 +0100116int bsc_queue_for_msc(struct gsm_subscriber_connection *conn, struct msgb *msg)
117{
Holger Hans Peter Freytherca5c0222010-11-06 23:35:02 +0100118 struct sccp_connection *sccp = conn->sccp_con->sccp;
119
120 if (sccp->connection_state != SCCP_CONNECTION_STATE_ESTABLISHED) {
121 LOGP(DMSC, LOGL_ERROR, "The connection is not established.\n");
122 msgb_free(msg);
123 return -1;
124 }
125
126 sccp_connection_write(sccp, msg);
Holger Hans Peter Freyther45825862010-11-04 11:48:49 +0100127 msgb_free(msg);
128 return 0;
129}
130
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100131int bsc_create_new_connection(struct gsm_subscriber_connection *conn)
Holger Hans Peter Freythercc48fb82010-11-05 11:10:46 +0100132{
Holger Hans Peter Freytherb8781d22010-11-06 18:08:43 +0100133 struct gsm_network *net;
134 struct osmo_bsc_sccp_con *bsc_con;
135 struct sccp_connection *sccp;
136
137 net = conn->bts->network;
138 if (!net->msc_data->msc_con->is_authenticated) {
139 LOGP(DMSC, LOGL_ERROR, "Not connected to a MSC. Not forwarding data.\n");
140 return -1;
141 }
142
143 if (!bsc_grace_allow_new_connection(net)) {
144 LOGP(DMSC, LOGL_NOTICE, "BSC in grace period. No new connections.\n");
145 return -1;
146 }
147
148 sccp = sccp_connection_socket();
149 if (!sccp) {
150 LOGP(DMSC, LOGL_ERROR, "Failed to allocate memory.\n");
151 return -ENOMEM;
152 }
153
154 bsc_con = talloc_zero(conn->bts, struct osmo_bsc_sccp_con);
155 if (!bsc_con) {
156 LOGP(DMSC, LOGL_ERROR, "Failed to allocate.\n");
157 sccp_connection_free(sccp);
158 return -1;
159 }
160
161 /* callbacks */
162 sccp->state_cb = msc_outgoing_sccp_state;
163 sccp->data_cb = msc_outgoing_sccp_data;
164 sccp->data_ctx = bsc_con;
165
Holger Hans Peter Freytherc97d9b42010-11-06 20:23:58 +0100166 /* prepare the timers */
167 bsc_con->sccp_it_timeout.cb = sccp_it_timeout;
168 bsc_con->sccp_it_timeout.data = bsc_con;
169 bsc_con->sccp_cc_timeout.cb = sccp_cc_timeout;
170 bsc_con->sccp_cc_timeout.data = bsc_con;
171
Holger Hans Peter Freytherb8781d22010-11-06 18:08:43 +0100172 bsc_con->sccp = sccp;
173 bsc_con->msc_con = net->msc_data->msc_con;
Holger Hans Peter Freyther7e901082010-11-06 20:15:17 +0100174 bsc_con->conn = conn;
Holger Hans Peter Freytherb8781d22010-11-06 18:08:43 +0100175 llist_add(&bsc_con->entry, &active_connections);
176 conn->sccp_con = bsc_con;
177 return 0;
Holger Hans Peter Freythercc48fb82010-11-05 11:10:46 +0100178}
179
Holger Hans Peter Freyther7e901082010-11-06 20:15:17 +0100180int bsc_open_connection(struct osmo_bsc_sccp_con *conn, struct msgb *msg)
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100181{
182 LOGP(DMSC, LOGL_ERROR, "Not implemented yet.\n");
183 return -1;
184}
185
Holger Hans Peter Freyther7e901082010-11-06 20:15:17 +0100186int bsc_delete_connection(struct osmo_bsc_sccp_con *sccp)
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100187{
Holger Hans Peter Freytherb8781d22010-11-06 18:08:43 +0100188 if (!sccp)
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100189 return 0;
190
Holger Hans Peter Freyther7e901082010-11-06 20:15:17 +0100191 if (sccp->conn)
192 LOGP(DMSC, LOGL_ERROR, "Should have been cleared.\n");
193
Holger Hans Peter Freytherb8781d22010-11-06 18:08:43 +0100194 llist_del(&sccp->entry);
195 bsc_del_timer(&sccp->sccp_it_timeout);
196 bsc_del_timer(&sccp->sccp_cc_timeout);
197 talloc_free(sccp);
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100198 return 0;
199}
200
Holger Hans Peter Freytherf3d1ae72010-11-03 16:40:03 +0100201int osmo_bsc_sccp_init(struct gsm_network *gsmnet)
202{
203 sccp_set_log_area(DSCCP);
204 sccp_system_init(msc_sccp_write_ipa, gsmnet);
205 sccp_connection_set_incoming(&sccp_ssn_bssap, msc_sccp_accept, NULL);
206 sccp_set_read(&sccp_ssn_bssap, msc_sccp_read, NULL);
207
208 return 0;
209}