blob: ef74f3ed3b9417a55911e12fe7621bd05489422b [file] [log] [blame]
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +01001/*
2 * The SS7 Application part for forwarding or nat...
3 *
4 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2011 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 Affero General Public License as published by
10 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
17 *
18 * 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/>.
20 *
21 */
22
23#include <ss7_application.h>
24#include <bsc_data.h>
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +010025#include <bsc_sccp.h>
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +010026#include <cellmgr_debug.h>
27#include <msc_connection.h>
28#include <sctp_m2ua.h>
Holger Hans Peter Freyther899198e2011-03-03 01:11:52 +010029#include <counter.h>
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +010030
31#include <osmocore/talloc.h>
32
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +010033
34/* the SS7 dispatch... maybe as function pointers in the future */
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010035void forward_sccp_stp(struct mtp_link_set *set, struct msgb *_msg, int sls)
36{
37 struct mtp_link_set *other;
38 other = set->app->route_src.set == set ?
39 set->app->route_dst.set : set->app->route_src.set;
40 mtp_link_set_submit_sccp_data(other, sls, _msg->l2h, msgb_l2len(_msg));
41}
42
43void forward_isup_stp(struct mtp_link_set *set, struct msgb *msg, int sls)
44{
45 struct mtp_link_set *other;
46 other = set->app->route_src.set == set ?
47 set->app->route_dst.set : set->app->route_src.set;
48 mtp_link_set_submit_isup_data(other, sls, msg->l3h, msgb_l3len(msg));
49}
50
51void mtp_link_set_forward_sccp(struct mtp_link_set *set, struct msgb *_msg, int sls)
52{
53 if (!set->app) {
54 LOGP(DINP, LOGL_ERROR, "Linkset %d/%s has no application.\n",
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +010055 set->nr, set->name);
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010056 return;
57 }
58
59 switch (set->app->type) {
60 case APP_STP:
61 forward_sccp_stp(set, _msg, sls);
62 break;
63 case APP_CELLMGR:
64 case APP_RELAY:
65 app_forward_sccp(set->app, _msg, sls);
66 break;
67 }
68}
69
70void mtp_link_set_forward_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
71{
72 if (!set->app) {
73 LOGP(DINP, LOGL_ERROR, "Linkset %d/%s has no application.\n",
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +010074 set->nr, set->name);
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010075 return;
76 }
77
78
79 switch (set->app->type) {
80 case APP_STP:
81 forward_isup_stp(set, msg, sls);
82 break;
83 case APP_CELLMGR:
84 case APP_RELAY:
85 LOGP(DINP, LOGL_ERROR, "ISUP is not handled.\n");
86 break;
87 }
88}
89
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +010090void mtp_linkset_down(struct mtp_link_set *set)
91{
92 set->available = 0;
93 mtp_link_set_stop(set);
94
Holger Hans Peter Freytherd38b87a2011-03-03 00:45:49 +010095 if (!set->app)
96 return;
97
98 if (set->app->type == APP_STP) {
99 if (set->app->route_src.set == set)
100 set->app->route_src.up = 0;
101 else
102 set->app->route_dst.up = 0;
103 } else {
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +0100104 app_clear_connections(set->app);
105
106 /* If we have an A link send a reset to the MSC */
107 msc_mgcp_reset(set->app->route_dst.msc);
108 msc_send_reset(set->app->route_dst.msc);
109 }
110}
111
112void mtp_linkset_up(struct mtp_link_set *set)
113{
114 set->available = 1;
115
116 /* we have not gone through link down */
Holger Hans Peter Freytherd38b87a2011-03-03 00:45:49 +0100117 if (set->app) {
118 if (set->app->type == APP_STP) {
119 if (set->app->route_src.set == set)
120 set->app->route_src.up = 1;
121 else
122 set->app->route_dst.up = 1;
123 } else if (set->app->type != APP_STP &&
124 set->app->route_dst.msc->msc_link_down) {
125 app_clear_connections(set->app);
126 app_resources_released(set->app);
127 }
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +0100128 }
129
130 mtp_link_set_reset(set);
131}
132
133
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100134struct ss7_application *ss7_application_alloc(struct bsc_data *bsc)
135{
136 struct ss7_application *app;
137
138 app = talloc_zero(bsc, struct ss7_application);
139 if (!app) {
140 LOGP(DINP, LOGL_ERROR, "Failed to create SS7 Application.\n");
141 return NULL;
142 }
143
144 INIT_LLIST_HEAD(&app->sccp_connections);
Holger Hans Peter Freyther169a1a92011-02-22 15:45:52 +0100145 llist_add_tail(&app->entry, &bsc->apps);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100146 app->nr = bsc->num_apps++;
147 app->bsc = bsc;
148
149 return app;
150}
151
152struct ss7_application *ss7_application_num(struct bsc_data *bsc, int num)
153{
154 struct ss7_application *ss7;
155
156 llist_for_each_entry(ss7, &bsc->apps, entry)
157 if (ss7->nr == num)
158 return ss7;
159
160 return NULL;
161}
162
163static int ss7_app_setup_stp(struct ss7_application *app,
164 int src_type, int src_num,
165 int dst_type, int dst_num)
166{
167 struct mtp_link_set *src, *dst;
168
169 if (src_type != SS7_SET_LINKSET) {
170 LOGP(DINP, LOGL_ERROR,
171 "SS7 %d/%s source needs to be a linkset.\n",
172 app->nr, app->name);
173 return -1;
174 }
175
176 if (dst_type != SS7_SET_LINKSET) {
177 LOGP(DINP, LOGL_ERROR,
178 "SS7 %d/%s destination needs to be a linkset.\n",
179 app->nr, app->name);
180 return -1;
181 }
182
183 /* veryify the MTP Linkset */
184 src = mtp_link_set_num(app->bsc, src_num);
185 if (!src) {
186 LOGP(DINP, LOGL_ERROR,
187 "SS7 %d/%s source linkset not found with nr: %d.\n",
188 app->nr, app->name, src_num);
189 return -2;
190 }
191
192 if (src->app) {
193 LOGP(DINP, LOGL_ERROR,
194 "SS7 %d/%s is using linkset %d/%s\n",
195 src->app->nr, src->app->name,
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +0100196 src->nr, src->name);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100197 return -3;
198 }
199
200 /* veryify the MTP Linkset */
201 dst = mtp_link_set_num(app->bsc, dst_num);
202 if (!dst) {
203 LOGP(DINP, LOGL_ERROR,
204 "SS7 %d/%s destionation linkset not found with nr: %d.\n",
205 app->nr, app->name, dst_num);
206 return -2;
207 }
208
209 if (dst->app) {
210 LOGP(DINP, LOGL_ERROR,
211 "SS7 %d/%s is using linkset %d/%s\n",
212 dst->app->nr, dst->app->name,
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +0100213 dst->nr, dst->name);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100214 return -3;
215 }
216
217 /* now connect it */
218 src->app = app;
219 app->route_src.type = src_type;
220 app->route_src.nr = src_num;
221 app->route_src.set = src;
222 app->route_src.msc = NULL;
223
224 dst->app = app;
225 app->route_dst.type = dst_type;
226 app->route_dst.nr = dst_num;
227 app->route_dst.set = dst;
228 app->route_dst.msc = NULL;
229
230 app->type = APP_STP;
231 app->bsc->m2ua_trans->started = 1;
Holger Hans Peter Freyther71760302011-02-22 20:57:08 +0100232 app->route_is_set = 1;
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100233
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100234 return 0;
235}
236
237static int ss7_app_setup_relay(struct ss7_application *app, int type,
238 int src_type, int src_num, int dst_type, int dst_num)
239{
240 struct mtp_link_set *mtp;
241 struct msc_connection *msc;
242
243 /* verify the types */
244 if (src_type != SS7_SET_LINKSET) {
245 LOGP(DINP, LOGL_ERROR,
246 "SS7 %d/%s source needs to be a linkset.\n",
247 app->nr, app->name);
248 return -1;
249 }
250
251 if (dst_type != SS7_SET_MSC) {
252 LOGP(DINP, LOGL_ERROR,
253 "SS7 %d/%s dest needs to be a MSC.\n",
254 app->nr, app->name);
255 return -1;
256 }
257
258 /* veryify the MTP Linkset */
259 mtp = mtp_link_set_num(app->bsc, src_num);
260 if (!mtp) {
261 LOGP(DINP, LOGL_ERROR,
262 "SS7 %d/%s source linkset not found with nr: %d.\n",
263 app->nr, app->name, src_num);
264 return -2;
265 }
266
267 if (mtp->app) {
268 LOGP(DINP, LOGL_ERROR,
269 "SS7 %d/%s is using linkset %d/%s\n",
270 mtp->app->nr, mtp->app->name,
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +0100271 mtp->nr, mtp->name);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100272 return -3;
273 }
274
275 /* verify the MSC connection */
276 msc = msc_connection_num(app->bsc, dst_num);
277 if (!msc) {
278 LOGP(DINP, LOGL_ERROR,
279 "SS7 %d/%s dest MSC not found with nr: %d.\n",
280 app->nr, app->name, dst_num);
281 return -4;
282 }
283
284 if (msc->app) {
285 LOGP(DINP, LOGL_ERROR,
286 "SS7 %d/%s is using MSC connection %d/%s\n",
287 msc->app->nr, msc->app->name,
288 msc->nr, msc->name);
289 return -5;
290 }
291
292
293 /* now connect it and run the app */
294 mtp->app = app;
295 app->route_src.type = src_type;
296 app->route_src.nr = src_num;
297 app->route_src.set = mtp;
298 app->route_src.msc = NULL;
299
300 msc->app = app;
301 app->route_dst.type = dst_type;
302 app->route_dst.nr = dst_num;
303 app->route_dst.set = NULL;
304 app->route_dst.msc = msc;
305
306 app->type = type;
Holger Hans Peter Freyther71760302011-02-22 20:57:08 +0100307 app->route_is_set = 1;
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100308
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100309 return 0;
310}
311
312int ss7_application_setup(struct ss7_application *ss7, int type,
313 int src_type, int src_num,
314 int dst_type, int dst_num)
315{
316 switch (type) {
317 case APP_CELLMGR:
318 case APP_RELAY:
319 return ss7_app_setup_relay(ss7, type, src_type, src_num,
320 dst_type, dst_num);
321 break;
322 case APP_STP:
323 return ss7_app_setup_stp(ss7, src_type, src_num,
324 dst_type, dst_num);
325 default:
326 LOGP(DINP, LOGL_ERROR,
327 "SS7 Application %d is not supported.\n", type);
328 return -1;
329 }
330}
Holger Hans Peter Freytherab7c6012011-02-16 22:23:52 +0100331
332
333static void start_mtp(struct mtp_link_set *set)
334{
335 struct mtp_link *link;
336
337 llist_for_each_entry(link, &set->links, entry)
338 link->reset(link);
339}
340
341static void start_msc(struct msc_connection *msc)
342{
343 msc_connection_start(msc);
344}
345
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100346static void start_set(struct ss7_application *app, struct mtp_link_set *set)
347{
348 if (!set)
349 return;
350
351 set->isup_opc = set->isup_opc >= 0 ? set->isup_opc : set->opc;
352 set->sccp_opc = set->sccp_opc >= 0 ? set->sccp_opc : set->opc;
353 set->pass_all_isup = app->isup_pass;
354 start_mtp(set);
355}
356
Holger Hans Peter Freytherab7c6012011-02-16 22:23:52 +0100357int ss7_application_start(struct ss7_application *app)
358{
Holger Hans Peter Freyther71760302011-02-22 20:57:08 +0100359 if (!app->route_is_set) {
360 LOGP(DINP, LOGL_ERROR,
361 "The routes are not configured on app %d.\n", app->nr);
362 return -1;
363 }
364
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100365 start_set(app, app->route_src.set);
366 start_set(app, app->route_dst.set);
Holger Hans Peter Freytherab7c6012011-02-16 22:23:52 +0100367
368 if (app->route_src.msc)
369 start_msc(app->route_src.msc);
370 if (app->route_dst.msc)
371 start_msc(app->route_dst.msc);
372
373 LOGP(DINP, LOGL_NOTICE, "SS7 Application %d/%s is now running.\n",
374 app->nr, app->name);
375 return 0;
376}
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100377
378void ss7_application_pass_isup(struct ss7_application *app, int pass)
379{
Holger Hans Peter Freyther2068e8c2011-03-05 09:48:52 +0100380 app->isup_pass = pass;
381
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100382 if (app->route_src.set)
383 app->route_src.set->pass_all_isup = pass;
384 if (app->route_dst.set)
385 app->route_dst.set->pass_all_isup = pass;
386}
Holger Hans Peter Freyther899198e2011-03-03 01:11:52 +0100387
388void mtp_link_submit(struct mtp_link *link, struct msgb *msg)
389{
390 if (link->set->app && link->set->app->type == APP_STP) {
391 if (!link->set->app->route_src.up || !link->set->app->route_dst.up) {
392 LOGP(DINP, LOGL_NOTICE, "Not sending data as application is down %d/%s.\n",
393 link->set->app->nr, link->set->app->name);
394 msgb_free(msg);
395 return;
396 }
397 }
398
399 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_OUT]);
400 rate_ctr_inc(&link->set->ctrg->ctr[MTP_LSET_TOTA_OUT_MSG]);
401 link->write(link, msg);
402}
Holger Hans Peter Freyther56cba9a2011-03-03 01:16:53 +0100403
404int mtp_link_set_data(struct mtp_link *link, struct msgb *msg)
405{
406 if (link->set->app && link->set->app->type == APP_STP) {
407 if (!link->set->app->route_src.up || !link->set->app->route_dst.up) {
408 LOGP(DINP, LOGL_NOTICE, "Not handling data as application is down %d/%s.\n",
409 link->set->app->nr, link->set->app->name);
410 return -1;
411 }
412 }
413
414 return mtp_link_handle_data(link, msg);
415}