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> |
| 25 | |
| 26 | #include <osmocore/talloc.h> |
| 27 | |
| 28 | #include <string.h> |
| 29 | |
| 30 | /* |
| 31 | * SCCP patching below |
| 32 | */ |
| 33 | |
| 34 | /* check if we are using this ref for patched already */ |
| 35 | static int sccp_ref_is_free(struct sccp_source_reference *ref, struct bsc_nat *nat) |
| 36 | { |
| 37 | struct sccp_connections *conn; |
| 38 | |
| 39 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
| 40 | if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0) |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | /* copied from sccp.c */ |
| 48 | static int assign_src_local_reference(struct sccp_source_reference *ref, struct bsc_nat *nat) |
| 49 | { |
| 50 | static u_int32_t last_ref = 0x50000; |
| 51 | int wrapped = 0; |
| 52 | |
| 53 | do { |
| 54 | struct sccp_source_reference reference; |
| 55 | reference.octet1 = (last_ref >> 0) & 0xff; |
| 56 | reference.octet2 = (last_ref >> 8) & 0xff; |
| 57 | reference.octet3 = (last_ref >> 16) & 0xff; |
| 58 | |
| 59 | ++last_ref; |
| 60 | /* do not use the reversed word and wrap around */ |
| 61 | if ((last_ref & 0x00FFFFFF) == 0x00FFFFFF) { |
| 62 | LOGP(DNAT, LOGL_NOTICE, "Wrapped searching for a free code\n"); |
| 63 | last_ref = 0; |
| 64 | ++wrapped; |
| 65 | } |
| 66 | |
| 67 | if (sccp_ref_is_free(&reference, nat) == 0) { |
| 68 | *ref = reference; |
| 69 | return 0; |
| 70 | } |
| 71 | } while (wrapped != 2); |
| 72 | |
| 73 | LOGP(DNAT, LOGL_ERROR, "Finding a free reference failed\n"); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed) |
| 78 | { |
| 79 | struct sccp_connections *conn; |
| 80 | |
| 81 | conn = talloc_zero(bsc->nat, struct sccp_connections); |
| 82 | if (!conn) { |
| 83 | LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n"); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | conn->real_ref = *parsed->src_local_ref; |
| 88 | if (assign_src_local_reference(&conn->patched_ref, bsc->nat) != 0) { |
| 89 | LOGP(DNAT, LOGL_ERROR, "Failed to assign a ref.\n"); |
| 90 | talloc_free(conn); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed) |
| 98 | { |
| 99 | struct sccp_connections *conn; |
| 100 | |
| 101 | llist_for_each_entry(conn, &bsc->nat->sccp_connections, list_entry) { |
| 102 | if (memcmp(parsed->src_local_ref, |
| 103 | &conn->real_ref, sizeof(conn->real_ref)) == 0) { |
| 104 | if (bsc != conn->bsc) { |
| 105 | LOGP(DNAT, LOGL_ERROR, "Someone else...\n"); |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | llist_del(&conn->list_entry); |
| 111 | talloc_free(conn); |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | LOGP(DNAT, LOGL_ERROR, "Unknown connection.\n"); |
| 117 | } |
| 118 | |
| 119 | struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg, |
| 120 | struct bsc_nat_parsed *parsed, |
| 121 | struct bsc_nat *nat) |
| 122 | { |
| 123 | struct sccp_connections *conn; |
| 124 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
| 125 | if (memcmp(parsed->dest_local_ref, |
| 126 | &conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) { |
| 127 | memcpy(parsed->dest_local_ref, |
| 128 | &conn->patched_ref, sizeof(*parsed->dest_local_ref)); |
| 129 | return conn->bsc; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg, |
| 137 | struct bsc_nat_parsed *parsed, |
| 138 | struct bsc_nat *nat) |
| 139 | { |
| 140 | struct sccp_connections *conn; |
| 141 | llist_for_each_entry(conn, &nat->sccp_connections, list_entry) { |
| 142 | if (memcmp(parsed->src_local_ref, |
| 143 | &conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) { |
| 144 | memcpy(parsed->src_local_ref, |
| 145 | &conn->patched_ref, sizeof(*parsed->src_local_ref)); |
| 146 | return conn->bsc; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return NULL; |
| 151 | } |