blob: cedc2d0b8fe6634967210640633122ad675af00b [file] [log] [blame]
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +01001/*
2 * The SS7 Application part for forwarding or nat...
3 *
Holger Hans Peter Freytherb35c5c32012-12-11 15:32:09 +01004 * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2012 by On-Waves
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +01006 * 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 Freythere8334782012-12-11 14:19:03 +010030#include <isup_filter.h>
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +010031
Harald Welteff397ed2011-05-08 10:29:23 +020032#include <osmocom/core/talloc.h>
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +010033
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +010034
35/* the SS7 dispatch... maybe as function pointers in the future */
Holger Hans Peter Freythera59af642012-01-16 09:54:09 +010036static void forward_sccp_stp(struct mtp_link_set *set, struct msgb *_msg, int sls)
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010037{
38 struct mtp_link_set *other;
39 other = set->app->route_src.set == set ?
40 set->app->route_dst.set : set->app->route_src.set;
41 mtp_link_set_submit_sccp_data(other, sls, _msg->l2h, msgb_l2len(_msg));
42}
43
Holger Hans Peter Freythera59af642012-01-16 09:54:09 +010044static void forward_isup_stp(struct mtp_link_set *set, struct msgb *msg, int sls)
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010045{
46 struct mtp_link_set *other;
47 other = set->app->route_src.set == set ?
48 set->app->route_dst.set : set->app->route_src.set;
Holger Hans Peter Freythere8334782012-12-11 14:19:03 +010049 isup_scan_for_reset(set->app, msg);
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010050 mtp_link_set_submit_isup_data(other, sls, msg->l3h, msgb_l3len(msg));
51}
52
53void mtp_link_set_forward_sccp(struct mtp_link_set *set, struct msgb *_msg, int sls)
54{
55 if (!set->app) {
56 LOGP(DINP, LOGL_ERROR, "Linkset %d/%s has no application.\n",
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +010057 set->nr, set->name);
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010058 return;
59 }
60
61 switch (set->app->type) {
62 case APP_STP:
63 forward_sccp_stp(set, _msg, sls);
64 break;
65 case APP_CELLMGR:
66 case APP_RELAY:
67 app_forward_sccp(set->app, _msg, sls);
68 break;
69 }
70}
71
72void mtp_link_set_forward_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
73{
74 if (!set->app) {
75 LOGP(DINP, LOGL_ERROR, "Linkset %d/%s has no application.\n",
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +010076 set->nr, set->name);
Holger Hans Peter Freyther64b7d562011-02-16 23:00:50 +010077 return;
78 }
79
80
81 switch (set->app->type) {
82 case APP_STP:
83 forward_isup_stp(set, msg, sls);
84 break;
85 case APP_CELLMGR:
86 case APP_RELAY:
87 LOGP(DINP, LOGL_ERROR, "ISUP is not handled.\n");
88 break;
89 }
90}
91
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +010092void mtp_linkset_down(struct mtp_link_set *set)
93{
94 set->available = 0;
95 mtp_link_set_stop(set);
96
Holger Hans Peter Freytherd38b87a2011-03-03 00:45:49 +010097 if (!set->app)
98 return;
99
100 if (set->app->type == APP_STP) {
101 if (set->app->route_src.set == set)
102 set->app->route_src.up = 0;
103 else
104 set->app->route_dst.up = 0;
105 } else {
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +0100106 app_clear_connections(set->app);
107
108 /* If we have an A link send a reset to the MSC */
109 msc_mgcp_reset(set->app->route_dst.msc);
110 msc_send_reset(set->app->route_dst.msc);
111 }
112}
113
114void mtp_linkset_up(struct mtp_link_set *set)
115{
116 set->available = 1;
117
118 /* we have not gone through link down */
Holger Hans Peter Freytherd38b87a2011-03-03 00:45:49 +0100119 if (set->app) {
120 if (set->app->type == APP_STP) {
121 if (set->app->route_src.set == set)
122 set->app->route_src.up = 1;
123 else
124 set->app->route_dst.up = 1;
125 } else if (set->app->type != APP_STP &&
126 set->app->route_dst.msc->msc_link_down) {
127 app_clear_connections(set->app);
128 app_resources_released(set->app);
129 }
Holger Hans Peter Freytherc5200fc2011-02-16 22:35:30 +0100130 }
131
132 mtp_link_set_reset(set);
133}
134
135
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100136struct ss7_application *ss7_application_alloc(struct bsc_data *bsc)
137{
138 struct ss7_application *app;
139
140 app = talloc_zero(bsc, struct ss7_application);
141 if (!app) {
142 LOGP(DINP, LOGL_ERROR, "Failed to create SS7 Application.\n");
143 return NULL;
144 }
145
146 INIT_LLIST_HEAD(&app->sccp_connections);
Holger Hans Peter Freyther169a1a92011-02-22 15:45:52 +0100147 llist_add_tail(&app->entry, &bsc->apps);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100148 app->nr = bsc->num_apps++;
149 app->bsc = bsc;
150
151 return app;
152}
153
154struct ss7_application *ss7_application_num(struct bsc_data *bsc, int num)
155{
156 struct ss7_application *ss7;
157
158 llist_for_each_entry(ss7, &bsc->apps, entry)
159 if (ss7->nr == num)
160 return ss7;
161
162 return NULL;
163}
164
165static int ss7_app_setup_stp(struct ss7_application *app,
166 int src_type, int src_num,
167 int dst_type, int dst_num)
168{
169 struct mtp_link_set *src, *dst;
170
171 if (src_type != SS7_SET_LINKSET) {
172 LOGP(DINP, LOGL_ERROR,
173 "SS7 %d/%s source needs to be a linkset.\n",
174 app->nr, app->name);
175 return -1;
176 }
177
178 if (dst_type != SS7_SET_LINKSET) {
179 LOGP(DINP, LOGL_ERROR,
180 "SS7 %d/%s destination needs to be a linkset.\n",
181 app->nr, app->name);
182 return -1;
183 }
184
185 /* veryify the MTP Linkset */
186 src = mtp_link_set_num(app->bsc, src_num);
187 if (!src) {
188 LOGP(DINP, LOGL_ERROR,
189 "SS7 %d/%s source linkset not found with nr: %d.\n",
190 app->nr, app->name, src_num);
191 return -2;
192 }
193
194 if (src->app) {
195 LOGP(DINP, LOGL_ERROR,
196 "SS7 %d/%s is using linkset %d/%s\n",
197 src->app->nr, src->app->name,
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +0100198 src->nr, src->name);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100199 return -3;
200 }
201
202 /* veryify the MTP Linkset */
203 dst = mtp_link_set_num(app->bsc, dst_num);
204 if (!dst) {
205 LOGP(DINP, LOGL_ERROR,
206 "SS7 %d/%s destionation linkset not found with nr: %d.\n",
207 app->nr, app->name, dst_num);
208 return -2;
209 }
210
211 if (dst->app) {
212 LOGP(DINP, LOGL_ERROR,
213 "SS7 %d/%s is using linkset %d/%s\n",
214 dst->app->nr, dst->app->name,
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +0100215 dst->nr, dst->name);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100216 return -3;
217 }
218
219 /* now connect it */
220 src->app = app;
221 app->route_src.type = src_type;
222 app->route_src.nr = src_num;
223 app->route_src.set = src;
224 app->route_src.msc = NULL;
225
226 dst->app = app;
227 app->route_dst.type = dst_type;
228 app->route_dst.nr = dst_num;
229 app->route_dst.set = dst;
230 app->route_dst.msc = NULL;
231
232 app->type = APP_STP;
233 app->bsc->m2ua_trans->started = 1;
Holger Hans Peter Freyther71760302011-02-22 20:57:08 +0100234 app->route_is_set = 1;
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100235
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100236 return 0;
237}
238
239static int ss7_app_setup_relay(struct ss7_application *app, int type,
240 int src_type, int src_num, int dst_type, int dst_num)
241{
242 struct mtp_link_set *mtp;
243 struct msc_connection *msc;
244
245 /* verify the types */
246 if (src_type != SS7_SET_LINKSET) {
247 LOGP(DINP, LOGL_ERROR,
248 "SS7 %d/%s source needs to be a linkset.\n",
249 app->nr, app->name);
250 return -1;
251 }
252
253 if (dst_type != SS7_SET_MSC) {
254 LOGP(DINP, LOGL_ERROR,
255 "SS7 %d/%s dest needs to be a MSC.\n",
256 app->nr, app->name);
257 return -1;
258 }
259
260 /* veryify the MTP Linkset */
261 mtp = mtp_link_set_num(app->bsc, src_num);
262 if (!mtp) {
263 LOGP(DINP, LOGL_ERROR,
264 "SS7 %d/%s source linkset not found with nr: %d.\n",
265 app->nr, app->name, src_num);
266 return -2;
267 }
268
269 if (mtp->app) {
270 LOGP(DINP, LOGL_ERROR,
271 "SS7 %d/%s is using linkset %d/%s\n",
272 mtp->app->nr, mtp->app->name,
Holger Hans Peter Freythera33b23f2011-02-16 23:37:40 +0100273 mtp->nr, mtp->name);
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100274 return -3;
275 }
276
277 /* verify the MSC connection */
278 msc = msc_connection_num(app->bsc, dst_num);
279 if (!msc) {
280 LOGP(DINP, LOGL_ERROR,
281 "SS7 %d/%s dest MSC not found with nr: %d.\n",
282 app->nr, app->name, dst_num);
283 return -4;
284 }
285
286 if (msc->app) {
287 LOGP(DINP, LOGL_ERROR,
288 "SS7 %d/%s is using MSC connection %d/%s\n",
289 msc->app->nr, msc->app->name,
290 msc->nr, msc->name);
291 return -5;
292 }
293
294
295 /* now connect it and run the app */
296 mtp->app = app;
297 app->route_src.type = src_type;
298 app->route_src.nr = src_num;
299 app->route_src.set = mtp;
300 app->route_src.msc = NULL;
301
302 msc->app = app;
303 app->route_dst.type = dst_type;
304 app->route_dst.nr = dst_num;
305 app->route_dst.set = NULL;
306 app->route_dst.msc = msc;
307
308 app->type = type;
Holger Hans Peter Freyther71760302011-02-22 20:57:08 +0100309 app->route_is_set = 1;
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100310
Holger Hans Peter Freythera7bc3aa2011-02-16 16:12:07 +0100311 return 0;
312}
313
314int ss7_application_setup(struct ss7_application *ss7, int type,
315 int src_type, int src_num,
316 int dst_type, int dst_num)
317{
318 switch (type) {
319 case APP_CELLMGR:
320 case APP_RELAY:
321 return ss7_app_setup_relay(ss7, type, src_type, src_num,
322 dst_type, dst_num);
323 break;
324 case APP_STP:
325 return ss7_app_setup_stp(ss7, src_type, src_num,
326 dst_type, dst_num);
327 default:
328 LOGP(DINP, LOGL_ERROR,
329 "SS7 Application %d is not supported.\n", type);
330 return -1;
331 }
332}
Holger Hans Peter Freytherab7c6012011-02-16 22:23:52 +0100333
Holger Hans Peter Freytherab7c6012011-02-16 22:23:52 +0100334static void start_mtp(struct mtp_link_set *set)
335{
336 struct mtp_link *link;
337
338 llist_for_each_entry(link, &set->links, entry)
339 link->reset(link);
340}
341
342static void start_msc(struct msc_connection *msc)
343{
344 msc_connection_start(msc);
345}
346
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100347static void start_set(struct ss7_application *app, struct mtp_link_set *set)
348{
349 if (!set)
350 return;
351
352 set->isup_opc = set->isup_opc >= 0 ? set->isup_opc : set->opc;
353 set->sccp_opc = set->sccp_opc >= 0 ? set->sccp_opc : set->opc;
354 set->pass_all_isup = app->isup_pass;
355 start_mtp(set);
356}
357
Holger Hans Peter Freytherab7c6012011-02-16 22:23:52 +0100358int ss7_application_start(struct ss7_application *app)
359{
Holger Hans Peter Freyther71760302011-02-22 20:57:08 +0100360 if (!app->route_is_set) {
361 LOGP(DINP, LOGL_ERROR,
362 "The routes are not configured on app %d.\n", app->nr);
363 return -1;
364 }
365
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100366 start_set(app, app->route_src.set);
367 start_set(app, app->route_dst.set);
Holger Hans Peter Freytherab7c6012011-02-16 22:23:52 +0100368
369 if (app->route_src.msc)
370 start_msc(app->route_src.msc);
371 if (app->route_dst.msc)
372 start_msc(app->route_dst.msc);
373
374 LOGP(DINP, LOGL_NOTICE, "SS7 Application %d/%s is now running.\n",
375 app->nr, app->name);
376 return 0;
377}
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100378
379void ss7_application_pass_isup(struct ss7_application *app, int pass)
380{
Holger Hans Peter Freyther2068e8c2011-03-05 09:48:52 +0100381 app->isup_pass = pass;
382
Holger Hans Peter Freythercfe9d712011-02-17 20:32:09 +0100383 if (app->route_src.set)
384 app->route_src.set->pass_all_isup = pass;
385 if (app->route_dst.set)
386 app->route_dst.set->pass_all_isup = pass;
387}
Holger Hans Peter Freyther899198e2011-03-03 01:11:52 +0100388
389void mtp_link_submit(struct mtp_link *link, struct msgb *msg)
390{
391 if (link->set->app && link->set->app->type == APP_STP) {
392 if (!link->set->app->route_src.up || !link->set->app->route_dst.up) {
393 LOGP(DINP, LOGL_NOTICE, "Not sending data as application is down %d/%s.\n",
394 link->set->app->nr, link->set->app->name);
395 msgb_free(msg);
396 return;
397 }
398 }
399
400 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_OUT]);
401 rate_ctr_inc(&link->set->ctrg->ctr[MTP_LSET_TOTA_OUT_MSG]);
402 link->write(link, msg);
403}
Holger Hans Peter Freyther56cba9a2011-03-03 01:16:53 +0100404
405int mtp_link_set_data(struct mtp_link *link, struct msgb *msg)
406{
407 if (link->set->app && link->set->app->type == APP_STP) {
408 if (!link->set->app->route_src.up || !link->set->app->route_dst.up) {
409 LOGP(DINP, LOGL_NOTICE, "Not handling data as application is down %d/%s.\n",
410 link->set->app->nr, link->set->app->name);
411 return -1;
412 }
413 }
414
415 return mtp_link_handle_data(link, msg);
416}
Holger Hans Peter Freytherfc98ce22011-09-13 20:08:57 +0200417
418int ss7_application_mgcp_domain_name(struct ss7_application *app,
419 const char *name)
420{
421 talloc_free(app->mgcp_domain_name);
422 app->mgcp_domain_name = talloc_strdup(app, name);
423
424 return app->mgcp_domain_name == NULL;
425}
Holger Hans Peter Freytherb35c5c32012-12-11 15:32:09 +0100426
427int ss7_application_trunk_name(struct ss7_application *app, const char *name)
428{
429 talloc_free(app->trunk_name);
430 app->trunk_name = talloc_strdup(app, name);
431
432 return app->trunk_name == NULL;
433}