Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 1 | /* 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 Freyther | 58a5679 | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 26 | #include <sccp/sccp.h> |
| 27 | |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 28 | #include <osmocore/talloc.h> |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 32 | static 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 Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 37 | /* |
| 38 | * SCCP patching below |
| 39 | */ |
| 40 | |
| 41 | /* check if we are using this ref for patched already */ |
| 42 | static 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 */ |
| 55 | static 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 | |
| 84 | int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed) |
| 85 | { |
| 86 | struct sccp_connections *conn; |
| 87 | |
Holger Hans Peter Freyther | d885b98 | 2010-04-21 10:47:25 +0800 | [diff] [blame] | 88 | /* Some commercial BSCs like to reassign there SRC ref */ |
| 89 | llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) { |
| 90 | if (conn->bsc != bsc) |
| 91 | continue; |
| 92 | if (memcmp(&conn->real_ref, parsed->src_local_ref, sizeof(conn->real_ref)) != 0) |
| 93 | continue; |
| 94 | |
| 95 | /* the BSC has reassigned the SRC ref and we failed to keep track */ |
| 96 | memset(&conn->remote_ref, 0, sizeof(conn->remote_ref)); |
| 97 | if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) { |
| 98 | LOGP(DNAT, LOGL_ERROR, "BSC %d reused src ref: %d and we failed to generate a new id.\n", |
| 99 | bsc->cfg->nr, sccp_src_ref_to_int(parsed->src_local_ref)); |
| 100 | llist_del(&conn->list_entry); |
| 101 | talloc_free(conn); |
| 102 | return -1; |
| 103 | } else { |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 104 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | d885b98 | 2010-04-21 10:47:25 +0800 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 110 | conn = talloc_zero(bsc->nat, struct sccp_connections); |
| 111 | if (!conn) { |
| 112 | LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
Holger Hans Peter Freyther | 58a5679 | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 116 | conn->bsc = bsc; |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 117 | conn->real_ref = *parsed->src_local_ref; |
| 118 | if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) { |
| 119 | LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n"); |
| 120 | talloc_free(conn); |
| 121 | return -1; |
| 122 | } |
| 123 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 124 | bsc_mgcp_init(conn); |
Holger Hans Peter Freyther | 8b51006 | 2010-04-18 01:12:03 +0800 | [diff] [blame] | 125 | llist_add_tail(&conn->list_entry, &bsc->nat->sccp_connections); |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 126 | counter_inc(bsc->cfg->stats.sccp.conn); |
| 127 | counter_inc(bsc->cfg->nat->stats.sccp.conn); |
Holger Hans Peter Freyther | 58a5679 | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 128 | |
Holger Hans Peter Freyther | 72ba162 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 129 | LOGP(DNAT, LOGL_DEBUG, "Created 0x%x <-> 0x%x mapping for con %p\n", |
Holger Hans Peter Freyther | 58a5679 | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 130 | sccp_src_ref_to_int(&conn->real_ref), |
| 131 | sccp_src_ref_to_int(&conn->patched_ref), bsc); |
| 132 | |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 136 | int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 137 | { |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 138 | if (!parsed->dest_local_ref || !parsed->src_local_ref) { |
| 139 | LOGP(DNAT, LOGL_ERROR, "CC MSG should contain both local and dest address.\n"); |
| 140 | return -1; |
| 141 | } |
| 142 | |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 143 | sccp->remote_ref = *parsed->src_local_ref; |
| 144 | LOGP(DNAT, LOGL_DEBUG, "Updating 0x%x to remote 0x%x on %p\n", |
| 145 | sccp_src_ref_to_int(&sccp->patched_ref), |
| 146 | sccp_src_ref_to_int(&sccp->remote_ref), sccp->bsc); |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 147 | |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 148 | return 0; |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 151 | void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed) |
| 152 | { |
| 153 | struct sccp_connections *conn; |
| 154 | |
| 155 | llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) { |
| 156 | if (memcmp(parsed->src_local_ref, |
Holger Hans Peter Freyther | 9d51855 | 2010-04-05 21:44:51 +0200 | [diff] [blame] | 157 | &conn->patched_ref, sizeof(conn->patched_ref)) == 0) { |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 158 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 159 | sccp_connection_destroy(conn); |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | } |
| 163 | |
Holger Hans Peter Freyther | b9ac37d | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 164 | LOGP(DNAT, LOGL_ERROR, "Can not remove connection: 0x%x\n", |
| 165 | sccp_src_ref_to_int(parsed->src_local_ref)); |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 168 | /* |
| 169 | * We have a message from the MSC to the BSC. The MSC is using |
| 170 | * an address that was assigned by the MUX, we need to update the |
| 171 | * dest reference to the real network. |
| 172 | */ |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 173 | struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *msg, |
| 174 | struct bsc_nat_parsed *parsed, |
| 175 | struct bsc_nat *nat) |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 176 | { |
| 177 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 178 | |
| 179 | if (!parsed->dest_local_ref) { |
| 180 | LOGP(DNAT, LOGL_ERROR, "MSG should contain dest_local_ref.\n"); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 185 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 186 | if (!equal(parsed->dest_local_ref, &conn->patched_ref)) |
| 187 | continue; |
| 188 | |
| 189 | /* Change the dest address to the real one */ |
| 190 | *parsed->dest_local_ref = conn->real_ref; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 191 | return conn; |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return NULL; |
| 195 | } |
| 196 | |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 197 | /* |
| 198 | * These are message to the MSC. We will need to find the BSC |
| 199 | * Connection by either the SRC or the DST local reference. |
| 200 | * |
| 201 | * In case of a CR we need to work by the SRC local reference |
| 202 | * in all other cases we need to work by the destination local |
| 203 | * reference.. |
| 204 | */ |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 205 | struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *msg, |
| 206 | struct bsc_nat_parsed *parsed, |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 207 | struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 208 | { |
| 209 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 210 | |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 211 | llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) { |
| 212 | if (conn->bsc != bsc) |
| 213 | continue; |
| 214 | |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 215 | if (parsed->src_local_ref) { |
| 216 | if (equal(parsed->src_local_ref, &conn->real_ref)) { |
| 217 | *parsed->src_local_ref = conn->patched_ref; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 218 | return conn; |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 219 | } |
| 220 | } else if (parsed->dest_local_ref) { |
| 221 | if (equal(parsed->dest_local_ref, &conn->remote_ref)) |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 222 | return conn; |
Holger Hans Peter Freyther | 64622e4 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 223 | } else { |
| 224 | LOGP(DNAT, LOGL_ERROR, "Header has neither loc/dst ref.\n"); |
| 225 | return NULL; |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | return NULL; |
| 230 | } |