Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 1 | /* |
| 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 Freyther | c5200fc | 2011-02-16 22:35:30 +0100 | [diff] [blame] | 25 | #include <bsc_sccp.h> |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 26 | #include <cellmgr_debug.h> |
| 27 | #include <msc_connection.h> |
| 28 | #include <sctp_m2ua.h> |
Holger Hans Peter Freyther | 899198e | 2011-03-03 01:11:52 +0100 | [diff] [blame] | 29 | #include <counter.h> |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 30 | |
Harald Welte | ff397ed | 2011-05-08 10:29:23 +0200 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | c5200fc | 2011-02-16 22:35:30 +0100 | [diff] [blame] | 33 | |
| 34 | /* the SS7 dispatch... maybe as function pointers in the future */ |
Holger Hans Peter Freyther | 64b7d56 | 2011-02-16 23:00:50 +0100 | [diff] [blame] | 35 | void 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 | |
| 43 | void 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 | |
| 51 | void 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 Freyther | a33b23f | 2011-02-16 23:37:40 +0100 | [diff] [blame] | 55 | set->nr, set->name); |
Holger Hans Peter Freyther | 64b7d56 | 2011-02-16 23:00:50 +0100 | [diff] [blame] | 56 | 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 | |
| 70 | void 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 Freyther | a33b23f | 2011-02-16 23:37:40 +0100 | [diff] [blame] | 74 | set->nr, set->name); |
Holger Hans Peter Freyther | 64b7d56 | 2011-02-16 23:00:50 +0100 | [diff] [blame] | 75 | 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 Freyther | c5200fc | 2011-02-16 22:35:30 +0100 | [diff] [blame] | 90 | void mtp_linkset_down(struct mtp_link_set *set) |
| 91 | { |
| 92 | set->available = 0; |
| 93 | mtp_link_set_stop(set); |
| 94 | |
Holger Hans Peter Freyther | d38b87a | 2011-03-03 00:45:49 +0100 | [diff] [blame] | 95 | 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 Freyther | c5200fc | 2011-02-16 22:35:30 +0100 | [diff] [blame] | 104 | 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 | |
| 112 | void 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 Freyther | d38b87a | 2011-03-03 00:45:49 +0100 | [diff] [blame] | 117 | 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 Freyther | c5200fc | 2011-02-16 22:35:30 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | mtp_link_set_reset(set); |
| 131 | } |
| 132 | |
| 133 | |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 134 | struct 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 Freyther | 169a1a9 | 2011-02-22 15:45:52 +0100 | [diff] [blame] | 145 | llist_add_tail(&app->entry, &bsc->apps); |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 146 | app->nr = bsc->num_apps++; |
| 147 | app->bsc = bsc; |
| 148 | |
| 149 | return app; |
| 150 | } |
| 151 | |
| 152 | struct 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 | |
| 163 | static 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 Freyther | a33b23f | 2011-02-16 23:37:40 +0100 | [diff] [blame] | 196 | src->nr, src->name); |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 197 | 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 Freyther | a33b23f | 2011-02-16 23:37:40 +0100 | [diff] [blame] | 213 | dst->nr, dst->name); |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 214 | 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 Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 232 | app->route_is_set = 1; |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 233 | |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static 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 Freyther | a33b23f | 2011-02-16 23:37:40 +0100 | [diff] [blame] | 271 | mtp->nr, mtp->name); |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 272 | 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 Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 307 | app->route_is_set = 1; |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 308 | |
Holger Hans Peter Freyther | a7bc3aa | 2011-02-16 16:12:07 +0100 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int 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 Freyther | ab7c601 | 2011-02-16 22:23:52 +0100 | [diff] [blame] | 331 | |
Holger Hans Peter Freyther | ab7c601 | 2011-02-16 22:23:52 +0100 | [diff] [blame] | 332 | static void start_mtp(struct mtp_link_set *set) |
| 333 | { |
| 334 | struct mtp_link *link; |
| 335 | |
| 336 | llist_for_each_entry(link, &set->links, entry) |
| 337 | link->reset(link); |
| 338 | } |
| 339 | |
| 340 | static void start_msc(struct msc_connection *msc) |
| 341 | { |
| 342 | msc_connection_start(msc); |
| 343 | } |
| 344 | |
Holger Hans Peter Freyther | cfe9d71 | 2011-02-17 20:32:09 +0100 | [diff] [blame] | 345 | static void start_set(struct ss7_application *app, struct mtp_link_set *set) |
| 346 | { |
| 347 | if (!set) |
| 348 | return; |
| 349 | |
| 350 | set->isup_opc = set->isup_opc >= 0 ? set->isup_opc : set->opc; |
| 351 | set->sccp_opc = set->sccp_opc >= 0 ? set->sccp_opc : set->opc; |
| 352 | set->pass_all_isup = app->isup_pass; |
| 353 | start_mtp(set); |
| 354 | } |
| 355 | |
Holger Hans Peter Freyther | ab7c601 | 2011-02-16 22:23:52 +0100 | [diff] [blame] | 356 | int ss7_application_start(struct ss7_application *app) |
| 357 | { |
Holger Hans Peter Freyther | 7176030 | 2011-02-22 20:57:08 +0100 | [diff] [blame] | 358 | if (!app->route_is_set) { |
| 359 | LOGP(DINP, LOGL_ERROR, |
| 360 | "The routes are not configured on app %d.\n", app->nr); |
| 361 | return -1; |
| 362 | } |
| 363 | |
Holger Hans Peter Freyther | cfe9d71 | 2011-02-17 20:32:09 +0100 | [diff] [blame] | 364 | start_set(app, app->route_src.set); |
| 365 | start_set(app, app->route_dst.set); |
Holger Hans Peter Freyther | ab7c601 | 2011-02-16 22:23:52 +0100 | [diff] [blame] | 366 | |
| 367 | if (app->route_src.msc) |
| 368 | start_msc(app->route_src.msc); |
| 369 | if (app->route_dst.msc) |
| 370 | start_msc(app->route_dst.msc); |
| 371 | |
| 372 | LOGP(DINP, LOGL_NOTICE, "SS7 Application %d/%s is now running.\n", |
| 373 | app->nr, app->name); |
| 374 | return 0; |
| 375 | } |
Holger Hans Peter Freyther | cfe9d71 | 2011-02-17 20:32:09 +0100 | [diff] [blame] | 376 | |
| 377 | void ss7_application_pass_isup(struct ss7_application *app, int pass) |
| 378 | { |
Holger Hans Peter Freyther | 2068e8c | 2011-03-05 09:48:52 +0100 | [diff] [blame] | 379 | app->isup_pass = pass; |
| 380 | |
Holger Hans Peter Freyther | cfe9d71 | 2011-02-17 20:32:09 +0100 | [diff] [blame] | 381 | if (app->route_src.set) |
| 382 | app->route_src.set->pass_all_isup = pass; |
| 383 | if (app->route_dst.set) |
| 384 | app->route_dst.set->pass_all_isup = pass; |
| 385 | } |
Holger Hans Peter Freyther | 899198e | 2011-03-03 01:11:52 +0100 | [diff] [blame] | 386 | |
| 387 | void mtp_link_submit(struct mtp_link *link, struct msgb *msg) |
| 388 | { |
| 389 | if (link->set->app && link->set->app->type == APP_STP) { |
| 390 | if (!link->set->app->route_src.up || !link->set->app->route_dst.up) { |
| 391 | LOGP(DINP, LOGL_NOTICE, "Not sending data as application is down %d/%s.\n", |
| 392 | link->set->app->nr, link->set->app->name); |
| 393 | msgb_free(msg); |
| 394 | return; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_OUT]); |
| 399 | rate_ctr_inc(&link->set->ctrg->ctr[MTP_LSET_TOTA_OUT_MSG]); |
| 400 | link->write(link, msg); |
| 401 | } |
Holger Hans Peter Freyther | 56cba9a | 2011-03-03 01:16:53 +0100 | [diff] [blame] | 402 | |
| 403 | int mtp_link_set_data(struct mtp_link *link, struct msgb *msg) |
| 404 | { |
| 405 | if (link->set->app && link->set->app->type == APP_STP) { |
| 406 | if (!link->set->app->route_src.up || !link->set->app->route_dst.up) { |
| 407 | LOGP(DINP, LOGL_NOTICE, "Not handling data as application is down %d/%s.\n", |
| 408 | link->set->app->nr, link->set->app->name); |
| 409 | return -1; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return mtp_link_handle_data(link, msg); |
| 414 | } |
Holger Hans Peter Freyther | fc98ce2 | 2011-09-13 20:08:57 +0200 | [diff] [blame^] | 415 | |
| 416 | int ss7_application_mgcp_domain_name(struct ss7_application *app, |
| 417 | const char *name) |
| 418 | { |
| 419 | talloc_free(app->mgcp_domain_name); |
| 420 | app->mgcp_domain_name = talloc_strdup(app, name); |
| 421 | |
| 422 | return app->mgcp_domain_name == NULL; |
| 423 | } |