blob: 72de11201f847fa0ae7901ffc18a91732a4d78d4 [file] [log] [blame]
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +08001/* SCCP patching and handling routines */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080010 * (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
Harald Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080016 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080019 *
20 */
21
22#include <openbsc/debug.h>
23#include <openbsc/bsc_nat.h>
Holger Hans Peter Freyther4a9dd3b2010-07-31 05:17:17 +080024#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080025
Harald Welte90e5eae2010-08-03 15:11:51 +020026#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther021266e2010-03-29 15:03:54 +020027
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080028#include <osmocore/talloc.h>
29
30#include <string.h>
Holger Hans Peter Freytherb5280e22010-06-15 18:51:49 +080031#include <time.h>
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080032
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +020033static int equal(struct sccp_source_reference *ref1, struct sccp_source_reference *ref2)
34{
35 return memcmp(ref1, ref2, sizeof(*ref1)) == 0;
36}
37
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080038/*
39 * SCCP patching below
40 */
41
42/* check if we are using this ref for patched already */
43static int sccp_ref_is_free(struct sccp_source_reference *ref, struct bsc_nat *nat)
44{
45 struct sccp_connections *conn;
46
47 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
48 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
49 return -1;
50 }
51
52 return 0;
53}
54
55/* copied from sccp.c */
56static int assign_src_local_reference(struct sccp_source_reference *ref, struct bsc_nat *nat)
57{
Holger Hans Peter Freytherd08fe9e2010-07-23 19:09:51 +080058 static uint32_t last_ref = 0x50000;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080059 int wrapped = 0;
60
61 do {
62 struct sccp_source_reference reference;
63 reference.octet1 = (last_ref >> 0) & 0xff;
64 reference.octet2 = (last_ref >> 8) & 0xff;
65 reference.octet3 = (last_ref >> 16) & 0xff;
66
67 ++last_ref;
68 /* do not use the reversed word and wrap around */
69 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
70 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
71 last_ref = 0;
72 ++wrapped;
73 }
74
75 if (sccp_ref_is_free(&reference, nat) == 0) {
76 *ref = reference;
77 return 0;
78 }
79 } while (wrapped != 2);
80
81 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
82 return -1;
83}
84
Holger Hans Peter Freythere1a16482010-05-16 16:51:31 +080085struct sccp_connections *create_sccp_src_ref(struct bsc_connection *bsc,
86 struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080087{
88 struct sccp_connections *conn;
89
Holger Hans Peter Freytherb9bc2542010-04-21 10:47:25 +080090 /* Some commercial BSCs like to reassign there SRC ref */
91 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
92 if (conn->bsc != bsc)
93 continue;
94 if (memcmp(&conn->real_ref, parsed->src_local_ref, sizeof(conn->real_ref)) != 0)
95 continue;
96
97 /* the BSC has reassigned the SRC ref and we failed to keep track */
98 memset(&conn->remote_ref, 0, sizeof(conn->remote_ref));
99 if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) {
100 LOGP(DNAT, LOGL_ERROR, "BSC %d reused src ref: %d and we failed to generate a new id.\n",
101 bsc->cfg->nr, sccp_src_ref_to_int(parsed->src_local_ref));
Holger Hans Peter Freyther22a4eb22010-05-12 00:35:07 +0800102 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freytherb9bc2542010-04-21 10:47:25 +0800103 llist_del(&conn->list_entry);
104 talloc_free(conn);
Holger Hans Peter Freythere1a16482010-05-16 16:51:31 +0800105 return NULL;
Holger Hans Peter Freytherb9bc2542010-04-21 10:47:25 +0800106 } else {
Holger Hans Peter Freytherb5280e22010-06-15 18:51:49 +0800107 clock_gettime(CLOCK_MONOTONIC, &conn->creation_time);
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800108 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freythere1a16482010-05-16 16:51:31 +0800109 return conn;
Holger Hans Peter Freytherb9bc2542010-04-21 10:47:25 +0800110 }
111 }
112
113
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800114 conn = talloc_zero(bsc->nat, struct sccp_connections);
115 if (!conn) {
116 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
Holger Hans Peter Freythere1a16482010-05-16 16:51:31 +0800117 return NULL;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800118 }
119
Holger Hans Peter Freyther021266e2010-03-29 15:03:54 +0200120 conn->bsc = bsc;
Holger Hans Peter Freytherb5280e22010-06-15 18:51:49 +0800121 clock_gettime(CLOCK_MONOTONIC, &conn->creation_time);
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800122 conn->real_ref = *parsed->src_local_ref;
123 if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) {
124 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
125 talloc_free(conn);
Holger Hans Peter Freythere1a16482010-05-16 16:51:31 +0800126 return NULL;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800127 }
128
Holger Hans Peter Freyther36330952010-04-22 12:08:17 +0800129 bsc_mgcp_init(conn);
Holger Hans Peter Freyther7aef1712010-04-18 01:12:03 +0800130 llist_add_tail(&conn->list_entry, &bsc->nat->sccp_connections);
Holger Hans Peter Freytherccefba62010-06-17 18:16:00 +0800131 rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_SCCP_CONN]);
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200132 counter_inc(bsc->cfg->nat->stats.sccp.conn);
Holger Hans Peter Freyther021266e2010-03-29 15:03:54 +0200133
Holger Hans Peter Freytherd062c522010-06-15 18:48:44 +0800134 LOGP(DNAT, LOGL_DEBUG, "Created 0x%x <-> 0x%x mapping for con %p\n",
Holger Hans Peter Freyther021266e2010-03-29 15:03:54 +0200135 sccp_src_ref_to_int(&conn->real_ref),
136 sccp_src_ref_to_int(&conn->patched_ref), bsc);
137
Holger Hans Peter Freythere1a16482010-05-16 16:51:31 +0800138 return conn;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800139}
140
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800141int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200142{
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200143 if (!parsed->dest_local_ref || !parsed->src_local_ref) {
144 LOGP(DNAT, LOGL_ERROR, "CC MSG should contain both local and dest address.\n");
145 return -1;
146 }
147
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800148 sccp->remote_ref = *parsed->src_local_ref;
Holger Hans Peter Freyther5975c682010-04-23 14:13:27 +0800149 sccp->has_remote_ref = 1;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800150 LOGP(DNAT, LOGL_DEBUG, "Updating 0x%x to remote 0x%x on %p\n",
151 sccp_src_ref_to_int(&sccp->patched_ref),
152 sccp_src_ref_to_int(&sccp->remote_ref), sccp->bsc);
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200153
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800154 return 0;
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200155}
156
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800157void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
158{
159 struct sccp_connections *conn;
160
161 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
162 if (memcmp(parsed->src_local_ref,
Holger Hans Peter Freytherbedecf32010-04-05 21:44:51 +0200163 &conn->patched_ref, sizeof(conn->patched_ref)) == 0) {
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800164
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +0200165 sccp_connection_destroy(conn);
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800166 return;
167 }
168 }
169
Holger Hans Peter Freytherc12e1932010-04-05 17:58:52 +0200170 LOGP(DNAT, LOGL_ERROR, "Can not remove connection: 0x%x\n",
171 sccp_src_ref_to_int(parsed->src_local_ref));
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800172}
173
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200174/*
175 * We have a message from the MSC to the BSC. The MSC is using
176 * an address that was assigned by the MUX, we need to update the
177 * dest reference to the real network.
178 */
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800179struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *msg,
180 struct bsc_nat_parsed *parsed,
181 struct bsc_nat *nat)
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800182{
183 struct sccp_connections *conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200184
185 if (!parsed->dest_local_ref) {
186 LOGP(DNAT, LOGL_ERROR, "MSG should contain dest_local_ref.\n");
187 return NULL;
188 }
189
190
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800191 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200192 if (!equal(parsed->dest_local_ref, &conn->patched_ref))
193 continue;
194
195 /* Change the dest address to the real one */
196 *parsed->dest_local_ref = conn->real_ref;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800197 return conn;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800198 }
199
200 return NULL;
201}
202
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200203/*
204 * These are message to the MSC. We will need to find the BSC
205 * Connection by either the SRC or the DST local reference.
206 *
207 * In case of a CR we need to work by the SRC local reference
208 * in all other cases we need to work by the destination local
209 * reference..
210 */
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800211struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *msg,
212 struct bsc_nat_parsed *parsed,
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800213 struct bsc_connection *bsc)
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800214{
215 struct sccp_connections *conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200216
Holger Hans Peter Freytherda30c4b2010-04-21 18:56:12 +0800217 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
218 if (conn->bsc != bsc)
219 continue;
220
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200221 if (parsed->src_local_ref) {
222 if (equal(parsed->src_local_ref, &conn->real_ref)) {
223 *parsed->src_local_ref = conn->patched_ref;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800224 return conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200225 }
226 } else if (parsed->dest_local_ref) {
227 if (equal(parsed->dest_local_ref, &conn->remote_ref))
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800228 return conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200229 } else {
230 LOGP(DNAT, LOGL_ERROR, "Header has neither loc/dst ref.\n");
231 return NULL;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800232 }
233 }
234
235 return NULL;
236}
Holger Hans Peter Freytheraee68252010-10-16 16:35:00 +0200237
238struct sccp_connections *bsc_nat_find_con_by_bsc(struct bsc_nat *nat,
239 struct sccp_source_reference *ref)
240{
241 struct sccp_connections *conn;
242
243 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
244 if (memcmp(ref, &conn->real_ref, sizeof(*ref)) == 0)
245 return conn;
246 }
247
248 return NULL;
249}