blob: e29dc9b84ce1750e64f2038f7c03ae59488e2429 [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>
31
32/*
33 * SCCP patching below
34 */
35
36/* check if we are using this ref for patched already */
37static int sccp_ref_is_free(struct sccp_source_reference *ref, struct bsc_nat *nat)
38{
39 struct sccp_connections *conn;
40
41 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
42 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
43 return -1;
44 }
45
46 return 0;
47}
48
49/* copied from sccp.c */
50static int assign_src_local_reference(struct sccp_source_reference *ref, struct bsc_nat *nat)
51{
52 static u_int32_t last_ref = 0x50000;
53 int wrapped = 0;
54
55 do {
56 struct sccp_source_reference reference;
57 reference.octet1 = (last_ref >> 0) & 0xff;
58 reference.octet2 = (last_ref >> 8) & 0xff;
59 reference.octet3 = (last_ref >> 16) & 0xff;
60
61 ++last_ref;
62 /* do not use the reversed word and wrap around */
63 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
64 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
65 last_ref = 0;
66 ++wrapped;
67 }
68
69 if (sccp_ref_is_free(&reference, nat) == 0) {
70 *ref = reference;
71 return 0;
72 }
73 } while (wrapped != 2);
74
75 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
76 return -1;
77}
78
79int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
80{
81 struct sccp_connections *conn;
82
83 conn = talloc_zero(bsc->nat, struct sccp_connections);
84 if (!conn) {
85 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
86 return -1;
87 }
88
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +020089 conn->bsc = bsc;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +080090 conn->real_ref = *parsed->src_local_ref;
91 if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) {
92 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
93 talloc_free(conn);
94 return -1;
95 }
96
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +020097 llist_add(&conn->list_entry, &bsc->nat->sccp_connections);
98
99 LOGP(DNAT, LOGL_DEBUG, "Created 0x%x <-> 0x%x mapping for con 0x%p\n",
100 sccp_src_ref_to_int(&conn->real_ref),
101 sccp_src_ref_to_int(&conn->patched_ref), bsc);
102
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800103 return 0;
104}
105
Holger Hans Peter Freyther16a6f702010-03-29 17:18:42 +0200106int update_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
107{
108 struct sccp_connections *conn;
109
110 if (!parsed->dest_local_ref || !parsed->src_local_ref) {
111 LOGP(DNAT, LOGL_ERROR, "CC MSG should contain both local and dest address.\n");
112 return -1;
113 }
114
115 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
116 if (conn->bsc != bsc)
117 continue;
118
119 if (memcmp(parsed->dest_local_ref,
120 &conn->patched_ref, sizeof(conn->patched_ref)) != 0)
121 continue;
122
123 conn->remote_ref = *parsed->src_local_ref;
124 LOGP(DNAT, LOGL_DEBUG, "Updating 0x%x to remote 0x%x on 0x%p\n",
125 sccp_src_ref_to_int(&conn->patched_ref),
126 sccp_src_ref_to_int(&conn->remote_ref), bsc);
127 return 0;
128 }
129
130 LOGP(DNAT, LOGL_ERROR, "Referenced connection not found on BSC: 0x%p\n", bsc);
131 return -1;
132}
133
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800134void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
135{
136 struct sccp_connections *conn;
137
138 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
139 if (memcmp(parsed->src_local_ref,
140 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +0200141
142 /* two BSCs have used the same real ref... this is why we rewrite it */
143 if (bsc != conn->bsc)
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800144 continue;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800145
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +0200146 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con 0x%p\n",
147 sccp_src_ref_to_int(&conn->real_ref),
148 sccp_src_ref_to_int(&conn->patched_ref), bsc);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800149 llist_del(&conn->list_entry);
150 talloc_free(conn);
151 return;
152 }
153 }
154
155 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
156}
157
158struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg,
159 struct bsc_nat_parsed *parsed,
160 struct bsc_nat *nat)
161{
162 struct sccp_connections *conn;
163 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
164 if (memcmp(parsed->dest_local_ref,
165 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
166 memcpy(parsed->dest_local_ref,
167 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
168 return conn->bsc;
169 }
170 }
171
172 return NULL;
173}
174
175struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg,
176 struct bsc_nat_parsed *parsed,
177 struct bsc_nat *nat)
178{
179 struct sccp_connections *conn;
180 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
181 if (memcmp(parsed->src_local_ref,
182 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
183 memcpy(parsed->src_local_ref,
184 &conn->patched_ref, sizeof(*parsed->src_local_ref));
185 return conn->bsc;
186 }
187 }
188
189 return NULL;
190}