blob: 33142a9101fd488bde67f6d75ec3a29b6b322d2b [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
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 Freyther021266e2010-03-29 15:03:54 +020026#include <sccp/sccp.h>
27
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080028#include <osmocore/talloc.h>
29
30#include <string.h>
31
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +020032static int equal(struct sccp_source_reference *ref1, struct sccp_source_reference *ref2)
33{
34 return memcmp(ref1, ref2, sizeof(*ref1)) == 0;
35}
36
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080037/*
38 * SCCP patching below
39 */
40
41/* check if we are using this ref for patched already */
42static int sccp_ref_is_free(struct sccp_source_reference *ref, struct bsc_nat *nat)
43{
44 struct sccp_connections *conn;
45
46 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
47 if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
48 return -1;
49 }
50
51 return 0;
52}
53
54/* copied from sccp.c */
55static int assign_src_local_reference(struct sccp_source_reference *ref, struct bsc_nat *nat)
56{
57 static u_int32_t last_ref = 0x50000;
58 int wrapped = 0;
59
60 do {
61 struct sccp_source_reference reference;
62 reference.octet1 = (last_ref >> 0) & 0xff;
63 reference.octet2 = (last_ref >> 8) & 0xff;
64 reference.octet3 = (last_ref >> 16) & 0xff;
65
66 ++last_ref;
67 /* do not use the reversed word and wrap around */
68 if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) {
69 LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n");
70 last_ref = 0;
71 ++wrapped;
72 }
73
74 if (sccp_ref_is_free(&reference, nat) == 0) {
75 *ref = reference;
76 return 0;
77 }
78 } while (wrapped != 2);
79
80 LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n");
81 return -1;
82}
83
84int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
85{
86 struct sccp_connections *conn;
87
88 conn = talloc_zero(bsc->nat, struct sccp_connections);
89 if (!conn) {
90 LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
91 return -1;
92 }
93
Holger Hans Peter Freyther021266e2010-03-29 15:03:54 +020094 conn->bsc = bsc;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +080095 conn->real_ref = *parsed->src_local_ref;
96 if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) {
97 LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n");
98 talloc_free(conn);
99 return -1;
100 }
101
Holger Hans Peter Freytherddfb5422010-04-18 03:07:22 +0800102 bsc_mgcp_clear(conn);
Holger Hans Peter Freyther7aef1712010-04-18 01:12:03 +0800103 llist_add_tail(&conn->list_entry, &bsc->nat->sccp_connections);
Holger Hans Peter Freyther5fa42dd2010-04-12 12:17:09 +0200104 counter_inc(bsc->cfg->stats.sccp.conn);
105 counter_inc(bsc->cfg->nat->stats.sccp.conn);
Holger Hans Peter Freyther021266e2010-03-29 15:03:54 +0200106
Holger Hans Peter Freytherd062c522010-06-15 18:48:44 +0800107 LOGP(DNAT, LOGL_DEBUG, "Created 0x%x <-> 0x%x mapping for con %p\n",
Holger Hans Peter Freyther021266e2010-03-29 15:03:54 +0200108 sccp_src_ref_to_int(&conn->real_ref),
109 sccp_src_ref_to_int(&conn->patched_ref), bsc);
110
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800111 return 0;
112}
113
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800114int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200115{
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200116 if (!parsed->dest_local_ref || !parsed->src_local_ref) {
117 LOGP(DNAT, LOGL_ERROR, "CC MSG should contain both local and dest address.\n");
118 return -1;
119 }
120
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800121 sccp->remote_ref = *parsed->src_local_ref;
122 LOGP(DNAT, LOGL_DEBUG, "Updating 0x%x to remote 0x%x on %p\n",
123 sccp_src_ref_to_int(&sccp->patched_ref),
124 sccp_src_ref_to_int(&sccp->remote_ref), sccp->bsc);
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200125
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800126 return 0;
Holger Hans Peter Freythera25e3ec2010-03-29 17:18:42 +0200127}
128
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800129void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
130{
131 struct sccp_connections *conn;
132
133 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
134 if (memcmp(parsed->src_local_ref,
Holger Hans Peter Freytherbedecf32010-04-05 21:44:51 +0200135 &conn->patched_ref, sizeof(conn->patched_ref)) == 0) {
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800136
Holger Hans Peter Freyther8be49b32010-03-30 10:45:48 +0200137 sccp_connection_destroy(conn);
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800138 return;
139 }
140 }
141
Holger Hans Peter Freytherc12e1932010-04-05 17:58:52 +0200142 LOGP(DNAT, LOGL_ERROR, "Can not remove connection: 0x%x\n",
143 sccp_src_ref_to_int(parsed->src_local_ref));
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800144}
145
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200146/*
147 * We have a message from the MSC to the BSC. The MSC is using
148 * an address that was assigned by the MUX, we need to update the
149 * dest reference to the real network.
150 */
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800151struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *msg,
152 struct bsc_nat_parsed *parsed,
153 struct bsc_nat *nat)
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800154{
155 struct sccp_connections *conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200156
157 if (!parsed->dest_local_ref) {
158 LOGP(DNAT, LOGL_ERROR, "MSG should contain dest_local_ref.\n");
159 return NULL;
160 }
161
162
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800163 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200164 if (!equal(parsed->dest_local_ref, &conn->patched_ref))
165 continue;
166
167 /* Change the dest address to the real one */
168 *parsed->dest_local_ref = conn->real_ref;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800169 return conn;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800170 }
171
172 return NULL;
173}
174
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200175/*
176 * These are message to the MSC. We will need to find the BSC
177 * Connection by either the SRC or the DST local reference.
178 *
179 * In case of a CR we need to work by the SRC local reference
180 * in all other cases we need to work by the destination local
181 * reference..
182 */
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800183struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *msg,
184 struct bsc_nat_parsed *parsed,
185 struct bsc_nat *nat)
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800186{
187 struct sccp_connections *conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200188
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800189 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200190 if (parsed->src_local_ref) {
191 if (equal(parsed->src_local_ref, &conn->real_ref)) {
192 *parsed->src_local_ref = conn->patched_ref;
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800193 return conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200194 }
195 } else if (parsed->dest_local_ref) {
196 if (equal(parsed->dest_local_ref, &conn->remote_ref))
Holger Hans Peter Freyther75ee8412010-06-15 18:48:55 +0800197 return conn;
Holger Hans Peter Freyther773fba72010-03-29 17:41:01 +0200198 } else {
199 LOGP(DNAT, LOGL_ERROR, "Header has neither loc/dst ref.\n");
200 return NULL;
Holger Hans Peter Freyther2f1f55d2010-06-15 18:47:49 +0800201 }
202 }
203
204 return NULL;
205}