blob: f77cbefa7c0b9f9b4b1bc4ae2b44f80fa0e10fb1 [file] [log] [blame]
Holger Hans Peter Freyther0ab6bab2010-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
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/debug.h>
24#include <openbsc/bsc_nat.h>
25
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +020026#include <sccp/sccp.h>
27
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +080028#include <osmocore/talloc.h>
29
30#include <string.h>
Holger Hans Peter Freytherc14e09b2010-06-15 18:51:49 +080031#include <time.h>
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +080032
Holger Hans Peter Freyther64622e42010-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 Freyther0ab6bab2010-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{
58 static u_int32_t last_ref = 0x50000;
59 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
85int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
86{
87 struct sccp_connections *conn;
88
Holger Hans Peter Freytherd885b982010-04-21 10:47:25 +080089 /* Some commercial BSCs like to reassign there SRC ref */
90 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
91 if (conn->bsc != bsc)
92 continue;
93 if (memcmp(&conn->real_ref, parsed->src_local_ref, sizeof(conn->real_ref)) != 0)
94 continue;
95
96 /* the BSC has reassigned the SRC ref and we failed to keep track */
97 memset(&conn->remote_ref, 0, sizeof(conn->remote_ref));
98 if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) {
99 LOGP(DNAT, LOGL_ERROR, "BSC %d reused src ref: %d and we failed to generate a new id.\n",
100 bsc->cfg->nr, sccp_src_ref_to_int(parsed->src_local_ref));
Holger Hans Peter Freyther886d3822010-05-12 00:35:07 +0800101 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freytherd885b982010-04-21 10:47:25 +0800102 llist_del(&conn->list_entry);
103 talloc_free(conn);
104 return -1;
105 } else {
Holger Hans Peter Freytherc14e09b2010-06-15 18:51:49 +0800106 clock_gettime(CLOCK_MONOTONIC, &conn->creation_time);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800107 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freytherd885b982010-04-21 10:47:25 +0800108 return 0;
109 }
110 }
111
112
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800113 conn = talloc_zero(bsc->nat, struct sccp_connections);
114 if (!conn) {
115 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
116 return -1;
117 }
118
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +0200119 conn->bsc = bsc;
Holger Hans Peter Freytherc14e09b2010-06-15 18:51:49 +0800120 clock_gettime(CLOCK_MONOTONIC, &conn->creation_time);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800121 conn->real_ref = *parsed->src_local_ref;
122 if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) {
123 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
124 talloc_free(conn);
125 return -1;
126 }
127
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800128 bsc_mgcp_init(conn);
Holger Hans Peter Freyther8b510062010-04-18 01:12:03 +0800129 llist_add_tail(&conn->list_entry, &bsc->nat->sccp_connections);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +0200130 counter_inc(bsc->cfg->stats.sccp.conn);
131 counter_inc(bsc->cfg->nat->stats.sccp.conn);
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +0200132
Holger Hans Peter Freyther72ba1622010-06-15 18:48:44 +0800133 LOGP(DNAT, LOGL_DEBUG, "Created 0x%x <-> 0x%x mapping for con %p\n",
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +0200134 sccp_src_ref_to_int(&conn->real_ref),
135 sccp_src_ref_to_int(&conn->patched_ref), bsc);
136
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800137 return 0;
138}
139
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800140int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200141{
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200142 if (!parsed->dest_local_ref || !parsed->src_local_ref) {
143 LOGP(DNAT, LOGL_ERROR, "CC MSG should contain both local and dest address.\n");
144 return -1;
145 }
146
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800147 sccp->remote_ref = *parsed->src_local_ref;
Holger Hans Peter Freyther4c683d12010-04-23 14:13:27 +0800148 sccp->has_remote_ref = 1;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800149 LOGP(DNAT, LOGL_DEBUG, "Updating 0x%x to remote 0x%x on %p\n",
150 sccp_src_ref_to_int(&sccp->patched_ref),
151 sccp_src_ref_to_int(&sccp->remote_ref), sccp->bsc);
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200152
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800153 return 0;
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200154}
155
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800156void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
157{
158 struct sccp_connections *conn;
159
160 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
161 if (memcmp(parsed->src_local_ref,
Holger Hans Peter Freyther9d518552010-04-05 21:44:51 +0200162 &conn->patched_ref, sizeof(conn->patched_ref)) == 0) {
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800163
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200164 sccp_connection_destroy(conn);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800165 return;
166 }
167 }
168
Holger Hans Peter Freytherb9ac37d2010-04-05 17:58:52 +0200169 LOGP(DNAT, LOGL_ERROR, "Can not remove connection: 0x%x\n",
170 sccp_src_ref_to_int(parsed->src_local_ref));
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800171}
172
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200173/*
174 * We have a message from the MSC to the BSC. The MSC is using
175 * an address that was assigned by the MUX, we need to update the
176 * dest reference to the real network.
177 */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800178struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *msg,
179 struct bsc_nat_parsed *parsed,
180 struct bsc_nat *nat)
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800181{
182 struct sccp_connections *conn;
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200183
184 if (!parsed->dest_local_ref) {
185 LOGP(DNAT, LOGL_ERROR, "MSG should contain dest_local_ref.\n");
186 return NULL;
187 }
188
189
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800190 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200191 if (!equal(parsed->dest_local_ref, &conn->patched_ref))
192 continue;
193
194 /* Change the dest address to the real one */
195 *parsed->dest_local_ref = conn->real_ref;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800196 return conn;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800197 }
198
199 return NULL;
200}
201
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200202/*
203 * These are message to the MSC. We will need to find the BSC
204 * Connection by either the SRC or the DST local reference.
205 *
206 * In case of a CR we need to work by the SRC local reference
207 * in all other cases we need to work by the destination local
208 * reference..
209 */
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800210struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *msg,
211 struct bsc_nat_parsed *parsed,
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800212 struct bsc_connection *bsc)
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800213{
214 struct sccp_connections *conn;
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200215
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800216 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
217 if (conn->bsc != bsc)
218 continue;
219
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200220 if (parsed->src_local_ref) {
221 if (equal(parsed->src_local_ref, &conn->real_ref)) {
222 *parsed->src_local_ref = conn->patched_ref;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800223 return conn;
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200224 }
225 } else if (parsed->dest_local_ref) {
226 if (equal(parsed->dest_local_ref, &conn->remote_ref))
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800227 return conn;
Holger Hans Peter Freyther64622e42010-03-29 17:41:01 +0200228 } else {
229 LOGP(DNAT, LOGL_ERROR, "Header has neither loc/dst ref.\n");
230 return NULL;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800231 }
232 }
233
234 return NULL;
235}