Holger Hans Peter Freyther | 2f1f55d | 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> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 25 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 26 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 27 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 021266e | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 28 | |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 29 | #include <osmocore/talloc.h> |
| 30 | |
| 31 | #include <string.h> |
Holger Hans Peter Freyther | b5280e2 | 2010-06-15 18:51:49 +0800 | [diff] [blame] | 32 | #include <time.h> |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 34 | static int equal(struct sccp_source_reference *ref1, struct sccp_source_reference *ref2) |
| 35 | { |
| 36 | return memcmp(ref1, ref2, sizeof(*ref1)) == 0; |
| 37 | } |
| 38 | |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 39 | /* |
| 40 | * SCCP patching below |
| 41 | */ |
| 42 | |
| 43 | /* check if we are using this ref for patched already */ |
| 44 | static int sccp_ref_is_free(struct sccp_source_reference *ref, struct bsc_nat *nat) |
| 45 | { |
| 46 | struct sccp_connections *conn; |
| 47 | |
| 48 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
| 49 | if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0) |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* copied from sccp.c */ |
| 57 | static int assign_src_local_reference(struct sccp_source_reference *ref, struct bsc_nat *nat) |
| 58 | { |
Holger Hans Peter Freyther | d08fe9e | 2010-07-23 19:09:51 +0800 | [diff] [blame] | 59 | static uint32_t last_ref = 0x50000; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 60 | int wrapped = 0; |
| 61 | |
| 62 | do { |
| 63 | struct sccp_source_reference reference; |
| 64 | reference.octet1 = (last_ref >> 0) & 0xff; |
| 65 | reference.octet2 = (last_ref >> 8) & 0xff; |
| 66 | reference.octet3 = (last_ref >> 16) & 0xff; |
| 67 | |
| 68 | ++last_ref; |
| 69 | /* do not use the reversed word and wrap around */ |
| 70 | if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) { |
| 71 | LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n"); |
| 72 | last_ref = 0; |
| 73 | ++wrapped; |
| 74 | } |
| 75 | |
| 76 | if (sccp_ref_is_free(&reference, nat) == 0) { |
| 77 | *ref = reference; |
| 78 | return 0; |
| 79 | } |
| 80 | } while (wrapped != 2); |
| 81 | |
| 82 | LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 86 | struct sccp_connections *create_sccp_src_ref(struct bsc_connection *bsc, |
| 87 | struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 88 | { |
| 89 | struct sccp_connections *conn; |
| 90 | |
Holger Hans Peter Freyther | b9bc254 | 2010-04-21 10:47:25 +0800 | [diff] [blame] | 91 | /* Some commercial BSCs like to reassign there SRC ref */ |
| 92 | llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) { |
| 93 | if (conn->bsc != bsc) |
| 94 | continue; |
| 95 | if (memcmp(&conn->real_ref, parsed->src_local_ref, sizeof(conn->real_ref)) != 0) |
| 96 | continue; |
| 97 | |
| 98 | /* the BSC has reassigned the SRC ref and we failed to keep track */ |
| 99 | memset(&conn->remote_ref, 0, sizeof(conn->remote_ref)); |
| 100 | if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) { |
| 101 | LOGP(DNAT, LOGL_ERROR, "BSC %d reused src ref: %d and we failed to generate a new id.\n", |
| 102 | bsc->cfg->nr, sccp_src_ref_to_int(parsed->src_local_ref)); |
Holger Hans Peter Freyther | 22a4eb2 | 2010-05-12 00:35:07 +0800 | [diff] [blame] | 103 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | b9bc254 | 2010-04-21 10:47:25 +0800 | [diff] [blame] | 104 | llist_del(&conn->list_entry); |
| 105 | talloc_free(conn); |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 106 | return NULL; |
Holger Hans Peter Freyther | b9bc254 | 2010-04-21 10:47:25 +0800 | [diff] [blame] | 107 | } else { |
Holger Hans Peter Freyther | b5280e2 | 2010-06-15 18:51:49 +0800 | [diff] [blame] | 108 | clock_gettime(CLOCK_MONOTONIC, &conn->creation_time); |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 109 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 110 | return conn; |
Holger Hans Peter Freyther | b9bc254 | 2010-04-21 10:47:25 +0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 115 | conn = talloc_zero(bsc->nat, struct sccp_connections); |
| 116 | if (!conn) { |
| 117 | LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n"); |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 118 | return NULL; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 119 | } |
| 120 | |
Holger Hans Peter Freyther | 021266e | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 121 | conn->bsc = bsc; |
Holger Hans Peter Freyther | b5280e2 | 2010-06-15 18:51:49 +0800 | [diff] [blame] | 122 | clock_gettime(CLOCK_MONOTONIC, &conn->creation_time); |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 123 | conn->real_ref = *parsed->src_local_ref; |
| 124 | if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) { |
| 125 | LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n"); |
| 126 | talloc_free(conn); |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 127 | return NULL; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 130 | bsc_mgcp_init(conn); |
Holger Hans Peter Freyther | 7aef171 | 2010-04-18 01:12:03 +0800 | [diff] [blame] | 131 | llist_add_tail(&conn->list_entry, &bsc->nat->sccp_connections); |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 132 | rate_ctr_inc(&bsc->cfg->stats.ctrg->ctr[BCFG_CTR_SCCP_CONN]); |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 133 | counter_inc(bsc->cfg->nat->stats.sccp.conn); |
Holger Hans Peter Freyther | 021266e | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 134 | |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 135 | LOGP(DNAT, LOGL_DEBUG, "Created 0x%x <-> 0x%x mapping for con %p\n", |
Holger Hans Peter Freyther | 021266e | 2010-03-29 15:03:54 +0200 | [diff] [blame] | 136 | sccp_src_ref_to_int(&conn->real_ref), |
| 137 | sccp_src_ref_to_int(&conn->patched_ref), bsc); |
| 138 | |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 139 | return conn; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 142 | int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 143 | { |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 144 | if (!parsed->dest_local_ref || !parsed->src_local_ref) { |
| 145 | LOGP(DNAT, LOGL_ERROR, "CC MSG should contain both local and dest address.\n"); |
| 146 | return -1; |
| 147 | } |
| 148 | |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 149 | sccp->remote_ref = *parsed->src_local_ref; |
Holger Hans Peter Freyther | 5975c68 | 2010-04-23 14:13:27 +0800 | [diff] [blame] | 150 | sccp->has_remote_ref = 1; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 151 | LOGP(DNAT, LOGL_DEBUG, "Updating 0x%x to remote 0x%x on %p\n", |
| 152 | sccp_src_ref_to_int(&sccp->patched_ref), |
| 153 | sccp_src_ref_to_int(&sccp->remote_ref), sccp->bsc); |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 154 | |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 155 | return 0; |
Holger Hans Peter Freyther | a25e3ec | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 158 | void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed) |
| 159 | { |
| 160 | struct sccp_connections *conn; |
| 161 | |
| 162 | llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) { |
| 163 | if (memcmp(parsed->src_local_ref, |
Holger Hans Peter Freyther | bedecf3 | 2010-04-05 21:44:51 +0200 | [diff] [blame] | 164 | &conn->patched_ref, sizeof(conn->patched_ref)) == 0) { |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 165 | |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 166 | sccp_connection_destroy(conn); |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | } |
| 170 | |
Holger Hans Peter Freyther | c12e193 | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 171 | LOGP(DNAT, LOGL_ERROR, "Can not remove connection: 0x%x\n", |
| 172 | sccp_src_ref_to_int(parsed->src_local_ref)); |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 175 | /* |
| 176 | * We have a message from the MSC to the BSC. The MSC is using |
| 177 | * an address that was assigned by the MUX, we need to update the |
| 178 | * dest reference to the real network. |
| 179 | */ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 180 | struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *msg, |
| 181 | struct bsc_nat_parsed *parsed, |
| 182 | struct bsc_nat *nat) |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 183 | { |
| 184 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 185 | |
| 186 | if (!parsed->dest_local_ref) { |
| 187 | LOGP(DNAT, LOGL_ERROR, "MSG should contain dest_local_ref.\n"); |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 192 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 193 | if (!equal(parsed->dest_local_ref, &conn->patched_ref)) |
| 194 | continue; |
| 195 | |
| 196 | /* Change the dest address to the real one */ |
| 197 | *parsed->dest_local_ref = conn->real_ref; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 198 | return conn; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return NULL; |
| 202 | } |
| 203 | |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 204 | /* |
| 205 | * These are message to the MSC. We will need to find the BSC |
| 206 | * Connection by either the SRC or the DST local reference. |
| 207 | * |
| 208 | * In case of a CR we need to work by the SRC local reference |
| 209 | * in all other cases we need to work by the destination local |
| 210 | * reference.. |
| 211 | */ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 212 | struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *msg, |
| 213 | struct bsc_nat_parsed *parsed, |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 214 | struct bsc_connection *bsc) |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 215 | { |
| 216 | struct sccp_connections *conn; |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 217 | |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 218 | llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) { |
| 219 | if (conn->bsc != bsc) |
| 220 | continue; |
| 221 | |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 222 | if (parsed->src_local_ref) { |
| 223 | if (equal(parsed->src_local_ref, &conn->real_ref)) { |
| 224 | *parsed->src_local_ref = conn->patched_ref; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 225 | return conn; |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 226 | } |
| 227 | } else if (parsed->dest_local_ref) { |
| 228 | if (equal(parsed->dest_local_ref, &conn->remote_ref)) |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 229 | return conn; |
Holger Hans Peter Freyther | 773fba7 | 2010-03-29 17:41:01 +0200 | [diff] [blame] | 230 | } else { |
| 231 | LOGP(DNAT, LOGL_ERROR, "Header has neither loc/dst ref.\n"); |
| 232 | return NULL; |
Holger Hans Peter Freyther | 2f1f55d | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | return NULL; |
| 237 | } |
Holger Hans Peter Freyther | aee6825 | 2010-10-16 16:35:00 +0200 | [diff] [blame] | 238 | |
| 239 | struct sccp_connections *bsc_nat_find_con_by_bsc(struct bsc_nat *nat, |
| 240 | struct sccp_source_reference *ref) |
| 241 | { |
| 242 | struct sccp_connections *conn; |
| 243 | |
| 244 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
| 245 | if (memcmp(ref, &conn->real_ref, sizeof(*ref)) == 0) |
| 246 | return conn; |
| 247 | } |
| 248 | |
| 249 | return NULL; |
| 250 | } |