blob: 226e2e0d8ce9cf21248566be5798ee8e54ae3802 [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
106void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
107{
108 struct sccp_connections *conn;
109
110 llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) {
111 if (memcmp(parsed->src_local_ref,
112 &conn->real_ref, sizeof(conn->real_ref)) == 0) {
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +0200113
114 /* two BSCs have used the same real ref... this is why we rewrite it */
115 if (bsc != conn->bsc)
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800116 continue;
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800117
Holger Hans Peter Freyther58a56792010-03-29 15:03:54 +0200118 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con 0x%p\n",
119 sccp_src_ref_to_int(&conn->real_ref),
120 sccp_src_ref_to_int(&conn->patched_ref), bsc);
Holger Hans Peter Freyther0ab6bab2010-06-15 18:47:49 +0800121 llist_del(&conn->list_entry);
122 talloc_free(conn);
123 return;
124 }
125 }
126
127 LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n");
128}
129
130struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg,
131 struct bsc_nat_parsed *parsed,
132 struct bsc_nat *nat)
133{
134 struct sccp_connections *conn;
135 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
136 if (memcmp(parsed->dest_local_ref,
137 &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
138 memcpy(parsed->dest_local_ref,
139 &conn->patched_ref, sizeof(*parsed->dest_local_ref));
140 return conn->bsc;
141 }
142 }
143
144 return NULL;
145}
146
147struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg,
148 struct bsc_nat_parsed *parsed,
149 struct bsc_nat *nat)
150{
151 struct sccp_connections *conn;
152 llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
153 if (memcmp(parsed->src_local_ref,
154 &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
155 memcpy(parsed->src_local_ref,
156 &conn->patched_ref, sizeof(*parsed->src_local_ref));
157 return conn->bsc;
158 }
159 }
160
161 return NULL;
162}