Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2010 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 15 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | c2b31ed | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 22 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 23 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 24 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 19c530c | 2010-10-13 23:52:01 +0200 | [diff] [blame] | 25 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 26 | #include <openbsc/mgcp.h> |
| 27 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 28 | |
Harald Welte | d5db12c | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 29 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 30 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 31 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 1395948 | 2010-06-15 18:50:57 +0800 | [diff] [blame] | 32 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 69d801e | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 33 | #include <osmocore/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 35 | #include <netinet/in.h> |
| 36 | #include <arpa/inet.h> |
| 37 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 38 | #include <errno.h> |
| 39 | #include <unistd.h> |
| 40 | |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 41 | static int bsc_assign_endpoint(struct bsc_connection *bsc, struct sccp_connections *con) |
| 42 | { |
| 43 | const int number_endpoints = ARRAY_SIZE(bsc->endpoint_status); |
| 44 | int i; |
| 45 | |
| 46 | for (i = 1; i < number_endpoints; ++i) { |
| 47 | int endpoint = (bsc->last_endpoint + i) % number_endpoints; |
| 48 | if (endpoint == 0) |
| 49 | endpoint = 1; |
| 50 | |
| 51 | if (bsc->endpoint_status[endpoint] == 0) { |
| 52 | bsc->endpoint_status[endpoint] = 1; |
| 53 | con->bsc_endp = endpoint; |
| 54 | bsc->last_endpoint = endpoint; |
| 55 | return 0; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | static uint16_t create_cic(int endpoint) |
| 63 | { |
| 64 | int timeslot, multiplex; |
| 65 | |
| 66 | mgcp_endpoint_to_timeslot(endpoint, &multiplex, ×lot); |
| 67 | return (multiplex << 5) | (timeslot & 0x1f); |
| 68 | } |
| 69 | |
| 70 | int bsc_mgcp_assign_patch(struct sccp_connections *con, struct msgb *msg) |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 71 | { |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 72 | struct sccp_connections *mcon; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 73 | struct tlv_parsed tp; |
Holger Hans Peter Freyther | e2c1520 | 2010-07-23 19:09:21 +0800 | [diff] [blame] | 74 | uint16_t cic; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 75 | uint8_t timeslot; |
| 76 | uint8_t multiplex; |
Holger Hans Peter Freyther | 1406977 | 2010-11-04 17:14:41 +0100 | [diff] [blame] | 77 | unsigned int endp; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 78 | |
| 79 | if (!msg->l3h) { |
| 80 | LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | if (msgb_l3len(msg) < 3) { |
| 85 | LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n"); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 90 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) { |
| 91 | LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n"); |
| 92 | return -1; |
| 93 | } |
| 94 | |
Holger Hans Peter Freyther | e2c1520 | 2010-07-23 19:09:21 +0800 | [diff] [blame] | 95 | cic = ntohs(*(uint16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 96 | timeslot = cic & 0x1f; |
| 97 | multiplex = (cic & ~0x1f) >> 5; |
| 98 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 99 | |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 100 | endp = mgcp_timeslot_to_endpoint(multiplex, timeslot); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 101 | |
Holger Hans Peter Freyther | 1406977 | 2010-11-04 17:14:41 +0100 | [diff] [blame] | 102 | if (endp >= con->bsc->nat->mgcp_cfg->number_endpoints) { |
| 103 | LOGP(DNAT, LOGL_ERROR, |
| 104 | "MSC attempted to assign bad endpoint 0x%x\n", |
| 105 | endp); |
| 106 | return -1; |
| 107 | } |
| 108 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 109 | /* find stale connections using that endpoint */ |
| 110 | llist_for_each_entry(mcon, &con->bsc->nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 111 | if (mcon->msc_endp == endp) { |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 112 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 113 | "Endpoint %d was assigned to 0x%x and now 0x%x\n", |
| 114 | endp, |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 115 | sccp_src_ref_to_int(&mcon->patched_ref), |
| 116 | sccp_src_ref_to_int(&con->patched_ref)); |
| 117 | bsc_mgcp_dlcx(mcon); |
| 118 | } |
| 119 | } |
| 120 | |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 121 | con->msc_endp = endp; |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 122 | if (bsc_assign_endpoint(con->bsc, con) != 0) |
| 123 | return -1; |
| 124 | |
| 125 | /* |
| 126 | * now patch the message for the new CIC... |
| 127 | * still assumed to be one multiplex only |
| 128 | */ |
| 129 | cic = htons(create_cic(con->bsc_endp)); |
| 130 | memcpy((uint8_t *) TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE), |
| 131 | &cic, sizeof(cic)); |
| 132 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 136 | static void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i) |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 137 | { |
| 138 | if (nat->bsc_endpoints[i].transaction_id) { |
| 139 | talloc_free(nat->bsc_endpoints[i].transaction_id); |
| 140 | nat->bsc_endpoints[i].transaction_id = NULL; |
| 141 | } |
| 142 | |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 143 | nat->bsc_endpoints[i].transaction_state = 0; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 144 | nat->bsc_endpoints[i].bsc = NULL; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 147 | void bsc_mgcp_free_endpoints(struct bsc_nat *nat) |
| 148 | { |
| 149 | int i; |
| 150 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 151 | for (i = 1; i < nat->mgcp_cfg->number_endpoints; ++i){ |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 152 | bsc_mgcp_free_endpoint(nat, i); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 153 | mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]); |
| 154 | } |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 155 | } |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 156 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 157 | /* send a MDCX where we do not want a response */ |
Holger Hans Peter Freyther | 601180f | 2010-08-29 23:40:33 +0800 | [diff] [blame] | 158 | static void bsc_mgcp_send_mdcx(struct bsc_connection *bsc, int port, struct mgcp_endpoint *endp) |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 159 | { |
| 160 | char buf[2096]; |
| 161 | int len; |
| 162 | |
| 163 | len = snprintf(buf, sizeof(buf), |
Holger Hans Peter Freyther | f42f45b | 2010-04-22 13:06:24 +0800 | [diff] [blame] | 164 | "MDCX 23 %x@mgw MGCP 1.0\r\n" |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 165 | "Z: noanswer\r\n" |
| 166 | "\r\n" |
| 167 | "c=IN IP4 %s\r\n" |
| 168 | "m=audio %d RTP/AVP 255\r\n", |
Holger Hans Peter Freyther | 601180f | 2010-08-29 23:40:33 +0800 | [diff] [blame] | 169 | port, |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 170 | bsc->nat->mgcp_cfg->source_addr, |
Holger Hans Peter Freyther | 58ff219 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 171 | endp->bts_end.local_port); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 172 | if (len < 0) { |
| 173 | LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n"); |
| 174 | return; |
| 175 | } |
Holger Hans Peter Freyther | d38aa45 | 2010-08-30 11:58:49 +0800 | [diff] [blame] | 176 | |
| 177 | #warning "The MDCX is not send to the BSC. It should" |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void bsc_mgcp_send_dlcx(struct bsc_connection *bsc, int endpoint) |
| 181 | { |
| 182 | char buf[2096]; |
| 183 | int len; |
| 184 | |
| 185 | len = snprintf(buf, sizeof(buf), |
Holger Hans Peter Freyther | bf812fa | 2010-08-30 12:01:36 +0800 | [diff] [blame] | 186 | "DLCX 26 %x@mgw MGCP 1.0\r\n" |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 187 | "Z: noanswer\r\n", endpoint); |
| 188 | if (len < 0) { |
| 189 | LOGP(DMGCP, LOGL_ERROR, "snprintf for DLCX failed.\n"); |
| 190 | return; |
| 191 | } |
| 192 | |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 193 | bsc_write_mgcp(bsc, (uint8_t *) buf, len); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void bsc_mgcp_init(struct sccp_connections *con) |
| 197 | { |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 198 | con->msc_endp = -1; |
| 199 | con->bsc_endp = -1; |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void bsc_mgcp_dlcx(struct sccp_connections *con) |
| 203 | { |
| 204 | /* send a DLCX down the stream */ |
Holger Hans Peter Freyther | 39cd32e | 2010-08-28 18:11:07 +0800 | [diff] [blame] | 205 | if (con->bsc_endp != -1) { |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 206 | if (con->bsc->endpoint_status[con->bsc_endp] != 1) |
| 207 | LOGP(DNAT, LOGL_ERROR, "Endpoint 0x%x was not in use\n", con->bsc_endp); |
| 208 | con->bsc->endpoint_status[con->bsc_endp] = 0; |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 209 | bsc_mgcp_send_dlcx(con->bsc, con->bsc_endp); |
| 210 | bsc_mgcp_free_endpoint(con->bsc->nat, con->msc_endp); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bsc_mgcp_init(con); |
| 214 | } |
| 215 | |
| 216 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 217 | struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint) |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 218 | { |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 219 | struct sccp_connections *con = NULL; |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 220 | struct sccp_connections *sccp; |
| 221 | |
| 222 | llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 223 | if (sccp->msc_endp == -1) |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 224 | continue; |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 225 | if (sccp->msc_endp != endpoint) |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 226 | continue; |
| 227 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 228 | con = sccp; |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 231 | if (con) |
| 232 | return con; |
Holger Hans Peter Freyther | eb52e89 | 2010-04-18 02:14:45 +0800 | [diff] [blame] | 233 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 234 | LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 238 | int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const char *transaction_id) |
| 239 | { |
| 240 | struct bsc_nat *nat; |
| 241 | struct bsc_endpoint *bsc_endp; |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 242 | struct sccp_connections *sccp; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 243 | struct mgcp_endpoint *mgcp_endp; |
| 244 | struct msgb *bsc_msg; |
| 245 | |
| 246 | nat = cfg->data; |
| 247 | bsc_endp = &nat->bsc_endpoints[endpoint]; |
| 248 | mgcp_endp = &nat->mgcp_cfg->endpoints[endpoint]; |
| 249 | |
Holger Hans Peter Freyther | fef7612 | 2010-04-24 21:05:18 +0800 | [diff] [blame] | 250 | if (bsc_endp->transaction_id) { |
| 251 | LOGP(DMGCP, LOGL_ERROR, "Endpoint 0x%x had pending transaction: '%s'\n", |
| 252 | endpoint, bsc_endp->transaction_id); |
| 253 | talloc_free(bsc_endp->transaction_id); |
| 254 | bsc_endp->transaction_id = NULL; |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 255 | bsc_endp->transaction_state = 0; |
Holger Hans Peter Freyther | fef7612 | 2010-04-24 21:05:18 +0800 | [diff] [blame] | 256 | } |
| 257 | bsc_endp->bsc = NULL; |
| 258 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 259 | sccp = bsc_mgcp_find_con(nat, endpoint); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 260 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 261 | if (!sccp) { |
Holger Hans Peter Freyther | 3d194d9 | 2010-04-24 21:02:01 +0800 | [diff] [blame] | 262 | LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for change on endpoint: 0x%x state: %d\n", endpoint, state); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 263 | |
| 264 | switch (state) { |
| 265 | case MGCP_ENDP_CRCX: |
| 266 | return MGCP_POLICY_REJECT; |
| 267 | break; |
| 268 | case MGCP_ENDP_DLCX: |
| 269 | return MGCP_POLICY_CONT; |
| 270 | break; |
| 271 | case MGCP_ENDP_MDCX: |
| 272 | return MGCP_POLICY_CONT; |
| 273 | break; |
| 274 | default: |
| 275 | LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state); |
| 276 | return MGCP_POLICY_CONT; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 281 | /* we need to generate a new and patched message */ |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 282 | bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, sccp->bsc_endp, |
Holger Hans Peter Freyther | 58ff219 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 283 | nat->mgcp_cfg->source_addr, mgcp_endp->bts_end.local_port); |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 284 | if (!bsc_msg) { |
| 285 | LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n"); |
| 286 | return MGCP_POLICY_CONT; |
| 287 | } |
| 288 | |
| 289 | |
Holger Hans Peter Freyther | b3e0a03 | 2010-04-04 18:11:49 +0200 | [diff] [blame] | 290 | bsc_endp->transaction_id = talloc_strdup(nat, transaction_id); |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 291 | bsc_endp->transaction_state = state; |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 292 | bsc_endp->bsc = sccp->bsc; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 293 | |
| 294 | /* we need to update some bits */ |
| 295 | if (state == MGCP_ENDP_CRCX) { |
| 296 | struct sockaddr_in sock; |
| 297 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 298 | if (getpeername(sccp->bsc->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) { |
Holger Hans Peter Freyther | 92febd3 | 2010-04-06 11:17:07 +0200 | [diff] [blame] | 299 | LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n", |
| 300 | errno, strerror(errno)); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 301 | } else { |
Holger Hans Peter Freyther | a17d701 | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 302 | mgcp_endp->bts_end.addr = sock.sin_addr; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 303 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 304 | |
Holger Hans Peter Freyther | e66cac3 | 2010-08-05 01:50:44 +0800 | [diff] [blame] | 305 | /* send the message and a fake MDCX to force sending of a dummy packet */ |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 306 | bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | 601180f | 2010-08-29 23:40:33 +0800 | [diff] [blame] | 307 | bsc_mgcp_send_mdcx(sccp->bsc, sccp->bsc_endp, mgcp_endp); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 308 | return MGCP_POLICY_DEFER; |
| 309 | } else if (state == MGCP_ENDP_DLCX) { |
| 310 | /* we will free the endpoint now and send a DLCX to the BSC */ |
Holger Hans Peter Freyther | 3a347f0 | 2010-05-01 10:31:53 +0800 | [diff] [blame] | 311 | msgb_free(bsc_msg); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 312 | bsc_mgcp_dlcx(sccp); |
| 313 | return MGCP_POLICY_CONT; |
| 314 | } else { |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 315 | bsc_write(sccp->bsc, bsc_msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 316 | return MGCP_POLICY_DEFER; |
| 317 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 320 | /* |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 321 | * We do have a failure, free data downstream.. |
| 322 | */ |
| 323 | static void free_chan_downstream(struct mgcp_endpoint *endp, struct bsc_endpoint *bsc_endp, |
| 324 | struct bsc_connection *bsc) |
| 325 | { |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 326 | LOGP(DMGCP, LOGL_ERROR, "No CI, freeing endpoint 0x%x in state %d\n", |
| 327 | ENDPOINT_NUMBER(endp), bsc_endp->transaction_state); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 328 | |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 329 | /* if a CRCX failed... send a DLCX down the stream */ |
| 330 | if (bsc_endp->transaction_state == MGCP_ENDP_CRCX) { |
| 331 | struct sccp_connections *con; |
| 332 | con = bsc_mgcp_find_con(bsc->nat, ENDPOINT_NUMBER(endp)); |
| 333 | if (!con) { |
| 334 | LOGP(DMGCP, LOGL_ERROR, |
| 335 | "No SCCP connection for endp 0x%x\n", |
| 336 | ENDPOINT_NUMBER(endp)); |
| 337 | } else { |
| 338 | if (con->bsc == bsc) { |
Holger Hans Peter Freyther | 8574dcf | 2010-08-29 22:39:07 +0800 | [diff] [blame] | 339 | bsc_mgcp_send_dlcx(bsc, con->bsc_endp); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 340 | } else { |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 341 | LOGP(DMGCP, LOGL_ERROR, |
| 342 | "Endpoint belongs to a different BSC\n"); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 343 | } |
| 344 | } |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 345 | } |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 346 | |
Holger Hans Peter Freyther | c021fbe | 2010-08-28 16:46:27 +0800 | [diff] [blame] | 347 | bsc_mgcp_free_endpoint(bsc->nat, ENDPOINT_NUMBER(endp)); |
| 348 | mgcp_free_endp(endp); |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 352 | * We have received a msg from the BSC. We will see if we know |
| 353 | * this transaction and if it belongs to the BSC. Then we will |
| 354 | * need to patch the content to point to the local network and we |
| 355 | * need to update the I: that was assigned by the BSS. |
| 356 | */ |
| 357 | void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg) |
| 358 | { |
| 359 | struct msgb *output; |
| 360 | struct bsc_endpoint *bsc_endp = NULL; |
| 361 | struct mgcp_endpoint *endp = NULL; |
Holger Hans Peter Freyther | d2dd6e8 | 2010-04-06 11:06:11 +0200 | [diff] [blame] | 362 | int i, code; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 363 | char transaction_id[60]; |
| 364 | |
| 365 | /* Some assumption that our buffer is big enough.. and null terminate */ |
| 366 | if (msgb_l2len(msg) > 2000) { |
| 367 | LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n"); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | msg->l2h[msgb_l2len(msg)] = '\0'; |
| 372 | |
| 373 | if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) { |
| 374 | LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n"); |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) { |
| 379 | if (bsc->nat->bsc_endpoints[i].bsc != bsc) |
| 380 | continue; |
Holger Hans Peter Freyther | 3f7c7d0 | 2010-04-05 18:00:05 +0200 | [diff] [blame] | 381 | /* no one listening? a bug? */ |
| 382 | if (!bsc->nat->bsc_endpoints[i].transaction_id) |
| 383 | continue; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 384 | if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0) |
| 385 | continue; |
| 386 | |
| 387 | endp = &bsc->nat->mgcp_cfg->endpoints[i]; |
| 388 | bsc_endp = &bsc->nat->bsc_endpoints[i]; |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | if (!bsc_endp) { |
Holger Hans Peter Freyther | b9ac37d | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 393 | LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n", |
| 394 | transaction_id, (const char *) msg->l2h); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 398 | endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h); |
Holger Hans Peter Freyther | b84b5f6 | 2010-08-06 08:34:46 +0800 | [diff] [blame] | 399 | if (endp->ci == CI_UNUSED) { |
Holger Hans Peter Freyther | 7d47601 | 2010-08-06 19:16:34 +0800 | [diff] [blame] | 400 | free_chan_downstream(endp, bsc_endp, bsc); |
Holger Hans Peter Freyther | b84b5f6 | 2010-08-06 08:34:46 +0800 | [diff] [blame] | 401 | return; |
| 402 | } |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 403 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 404 | /* free some stuff */ |
| 405 | talloc_free(bsc_endp->transaction_id); |
| 406 | bsc_endp->transaction_id = NULL; |
Holger Hans Peter Freyther | 5b2726e | 2010-08-06 09:05:05 +0800 | [diff] [blame] | 407 | bsc_endp->transaction_state = 0; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 408 | |
Holger Hans Peter Freyther | dd16b42 | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 409 | /* |
| 410 | * rewrite the information. In case the endpoint was deleted |
| 411 | * there should be nothing for us to rewrite so putting endp->rtp_port |
| 412 | * with the value of 0 should be no problem. |
| 413 | */ |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 414 | output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), -1, |
Holger Hans Peter Freyther | 58ff219 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 415 | bsc->nat->mgcp_cfg->source_addr, endp->net_end.local_port); |
Holger Hans Peter Freyther | 5cc94fb | 2010-04-05 22:56:49 +0200 | [diff] [blame] | 416 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 417 | if (!output) { |
| 418 | LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n"); |
| 419 | return; |
| 420 | } |
| 421 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 422 | if (write_queue_enqueue(&bsc->nat->mgcp_cfg->gw_fd, output) != 0) { |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 423 | LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n"); |
| 424 | msgb_free(output); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]) |
| 429 | { |
| 430 | /* we want to parse two strings */ |
| 431 | return sscanf(str, "%3d %59s\n", code, transaction) != 2; |
| 432 | } |
| 433 | |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 434 | uint32_t bsc_mgcp_extract_ci(const char *str) |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 435 | { |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 436 | unsigned int ci; |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 437 | char *res = strstr(str, "I: "); |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 438 | if (!res) { |
| 439 | LOGP(DMGCP, LOGL_ERROR, "No CI in msg '%s'\n", str); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 440 | return CI_UNUSED; |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 441 | } |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 442 | |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 443 | if (sscanf(res, "I: %u", &ci) != 1) { |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 444 | LOGP(DMGCP, LOGL_ERROR, "Failed to parse CI in msg '%s'\n", str); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 445 | return CI_UNUSED; |
Holger Hans Peter Freyther | 9c31cfc | 2010-08-06 08:19:05 +0800 | [diff] [blame] | 446 | } |
| 447 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 448 | return ci; |
| 449 | } |
| 450 | |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 451 | static void patch_mgcp(struct msgb *output, const char *op, const char *tok, |
| 452 | int endp, int len, int cr) |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 453 | { |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 454 | int slen; |
| 455 | int ret; |
| 456 | char buf[40]; |
| 457 | |
| 458 | buf[0] = buf[39] = '\0'; |
| 459 | ret = sscanf(tok, "%*s %s", buf); |
| 460 | |
| 461 | slen = sprintf((char *) output->l3h, "%s %s %x@mgw MGCP 1.0%s", |
| 462 | op, buf, endp, cr ? "\r\n" : "\n"); |
| 463 | output->l3h = msgb_put(output, slen); |
| 464 | } |
| 465 | |
| 466 | /* we need to replace some strings... */ |
| 467 | struct msgb *bsc_mgcp_rewrite(char *input, int length, int endpoint, const char *ip, int port) |
| 468 | { |
| 469 | static const char *crcx_str = "CRCX "; |
| 470 | static const char *dlcx_str = "DLCX "; |
| 471 | static const char *mdcx_str = "MDCX "; |
| 472 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 473 | static const char *ip_str = "c=IN IP4 "; |
| 474 | static const char *aud_str = "m=audio "; |
| 475 | |
| 476 | char buf[128]; |
| 477 | char *running, *token; |
| 478 | struct msgb *output; |
| 479 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 480 | if (length > 4096 - 128) { |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 481 | LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n"); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | output = msgb_alloc_headroom(4096, 128, "MGCP rewritten"); |
| 486 | if (!output) { |
| 487 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n"); |
| 488 | return NULL; |
| 489 | } |
| 490 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 491 | running = input; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 492 | output->l2h = output->data; |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 493 | output->l3h = output->l2h; |
Holger Hans Peter Freyther | 9e5300a | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 494 | for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) { |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 495 | int len = strlen(token); |
Holger Hans Peter Freyther | 9e5300a | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 496 | int cr = len > 0 && token[len - 1] == '\r'; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 497 | |
Holger Hans Peter Freyther | f7c86c5 | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 498 | if (strncmp(crcx_str, token, (sizeof crcx_str) - 1) == 0) { |
| 499 | patch_mgcp(output, "CRCX", token, endpoint, len, cr); |
| 500 | } else if (strncmp(dlcx_str, token, (sizeof dlcx_str) - 1) == 0) { |
| 501 | patch_mgcp(output, "DLCX", token, endpoint, len, cr); |
| 502 | } else if (strncmp(mdcx_str, token, (sizeof mdcx_str) - 1) == 0) { |
| 503 | patch_mgcp(output, "MDCX", token, endpoint, len, cr); |
| 504 | } else if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) { |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 505 | output->l3h = msgb_put(output, strlen(ip_str)); |
| 506 | memcpy(output->l3h, ip_str, strlen(ip_str)); |
| 507 | output->l3h = msgb_put(output, strlen(ip)); |
| 508 | memcpy(output->l3h, ip, strlen(ip)); |
Holger Hans Peter Freyther | 9e5300a | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 509 | |
| 510 | if (cr) { |
| 511 | output->l3h = msgb_put(output, 2); |
| 512 | output->l3h[0] = '\r'; |
| 513 | output->l3h[1] = '\n'; |
| 514 | } else { |
| 515 | output->l3h = msgb_put(output, 1); |
| 516 | output->l3h[0] = '\n'; |
| 517 | } |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 518 | } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) { |
| 519 | int payload; |
| 520 | if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) { |
| 521 | LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n"); |
| 522 | msgb_free(output); |
| 523 | return NULL; |
| 524 | } |
| 525 | |
Holger Hans Peter Freyther | 9e5300a | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 526 | snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s", |
| 527 | port, payload, cr ? "\r\n" : "\n"); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 528 | buf[sizeof(buf)-1] = '\0'; |
| 529 | |
| 530 | output->l3h = msgb_put(output, strlen(buf)); |
| 531 | memcpy(output->l3h, buf, strlen(buf)); |
| 532 | } else { |
| 533 | output->l3h = msgb_put(output, len + 1); |
| 534 | memcpy(output->l3h, token, len); |
| 535 | output->l3h[len] = '\n'; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | return output; |
| 540 | } |
| 541 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 542 | static int mgcp_do_read(struct bsc_fd *fd) |
| 543 | { |
| 544 | struct bsc_nat *nat; |
| 545 | struct msgb *msg, *resp; |
| 546 | int rc; |
| 547 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 548 | nat = fd->data; |
| 549 | |
| 550 | rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1); |
| 551 | if (rc <= 0) { |
| 552 | LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno); |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | nat->mgcp_msg[rc] = '\0'; |
| 557 | nat->mgcp_length = rc; |
| 558 | |
| 559 | msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read"); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 560 | if (!msg) { |
| 561 | LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n"); |
| 562 | return -1; |
| 563 | } |
| 564 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 565 | msg->l2h = msgb_put(msg, rc); |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 566 | memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg)); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 567 | resp = mgcp_handle_message(nat->mgcp_cfg, msg); |
| 568 | msgb_free(msg); |
| 569 | |
| 570 | /* we do have a direct answer... e.g. AUEP */ |
| 571 | if (resp) { |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 572 | if (write_queue_enqueue(&nat->mgcp_cfg->gw_fd, resp) != 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 573 | LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n"); |
| 574 | msgb_free(resp); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg) |
| 582 | { |
| 583 | int rc; |
| 584 | |
| 585 | rc = write(bfd->fd, msg->data, msg->len); |
| 586 | |
| 587 | if (rc != msg->len) { |
| 588 | LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n"); |
| 589 | return -1; |
| 590 | } |
| 591 | |
| 592 | return rc; |
| 593 | } |
| 594 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 595 | int bsc_mgcp_nat_init(struct bsc_nat *nat) |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 596 | { |
| 597 | int on; |
| 598 | struct sockaddr_in addr; |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 599 | struct mgcp_config *cfg = nat->mgcp_cfg; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 600 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 601 | if (!cfg->call_agent_addr) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 602 | LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n"); |
| 603 | return -1; |
| 604 | } |
| 605 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 606 | if (cfg->bts_ip) { |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 607 | LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n"); |
| 608 | return -1; |
| 609 | } |
| 610 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 611 | cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 612 | if (cfg->gw_fd.bfd.fd < 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 613 | LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno); |
| 614 | return -1; |
| 615 | } |
| 616 | |
| 617 | on = 1; |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 618 | setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 619 | |
| 620 | addr.sin_family = AF_INET; |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 621 | addr.sin_port = htons(cfg->source_port); |
| 622 | inet_aton(cfg->source_addr, &addr.sin_addr); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 623 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 624 | if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 625 | LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 626 | close(cfg->gw_fd.bfd.fd); |
| 627 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 628 | return -1; |
| 629 | } |
| 630 | |
| 631 | addr.sin_port = htons(2727); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 632 | inet_aton(cfg->call_agent_addr, &addr.sin_addr); |
| 633 | if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 634 | LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n", |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 635 | cfg->call_agent_addr, errno); |
| 636 | close(cfg->gw_fd.bfd.fd); |
| 637 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 638 | return -1; |
| 639 | } |
| 640 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 641 | write_queue_init(&cfg->gw_fd, 10); |
| 642 | cfg->gw_fd.bfd.when = BSC_FD_READ; |
| 643 | cfg->gw_fd.bfd.data = nat; |
| 644 | cfg->gw_fd.read_cb = mgcp_do_read; |
| 645 | cfg->gw_fd.write_cb = mgcp_do_write; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 646 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 647 | if (bsc_register_fd(&cfg->gw_fd.bfd) != 0) { |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 648 | LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n"); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 649 | close(cfg->gw_fd.bfd.fd); |
| 650 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 651 | return -1; |
| 652 | } |
| 653 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 654 | /* some more MGCP config handling */ |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 655 | cfg->data = nat; |
| 656 | cfg->policy_cb = bsc_mgcp_policy_cb; |
| 657 | cfg->force_realloc = 1; |
Holger Hans Peter Freyther | d5e6c23 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 658 | |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 659 | if (cfg->bts_ip) |
| 660 | talloc_free(cfg->bts_ip); |
| 661 | cfg->bts_ip = ""; |
Holger Hans Peter Freyther | d5e6c23 | 2010-08-05 10:08:36 +0000 | [diff] [blame] | 662 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 663 | nat->bsc_endpoints = talloc_zero_array(nat, |
| 664 | struct bsc_endpoint, |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 665 | cfg->number_endpoints + 1); |
Holger Hans Peter Freyther | 3c79214 | 2010-09-19 04:34:04 +0800 | [diff] [blame] | 666 | if (!nat->bsc_endpoints) { |
| 667 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate nat endpoints\n"); |
Holger Hans Peter Freyther | 249d69a | 2010-09-18 21:13:54 +0800 | [diff] [blame] | 668 | close(cfg->gw_fd.bfd.fd); |
| 669 | cfg->gw_fd.bfd.fd = -1; |
Holger Hans Peter Freyther | 3c79214 | 2010-09-19 04:34:04 +0800 | [diff] [blame] | 670 | return -1; |
| 671 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 672 | |
Holger Hans Peter Freyther | f2eedff | 2010-09-19 04:36:07 +0800 | [diff] [blame] | 673 | if (mgcp_reset_transcoder(cfg) < 0) { |
| 674 | LOGP(DMGCP, LOGL_ERROR, "Failed to send packet to the transcoder.\n"); |
| 675 | talloc_free(nat->bsc_endpoints); |
| 676 | nat->bsc_endpoints = NULL; |
| 677 | close(cfg->gw_fd.bfd.fd); |
| 678 | cfg->gw_fd.bfd.fd = -1; |
| 679 | return -1; |
| 680 | } |
| 681 | |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 682 | return 0; |
| 683 | } |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 684 | |
| 685 | void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc) |
| 686 | { |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 687 | struct rate_ctr *ctr = NULL; |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 688 | int i; |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 689 | |
| 690 | if (bsc->cfg) |
| 691 | ctr = &bsc->cfg->stats.ctrg->ctr[BCFG_CTR_DROPPED_CALLS]; |
| 692 | |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 693 | for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) { |
| 694 | struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i]; |
| 695 | |
| 696 | if (bsc_endp->bsc != bsc) |
| 697 | continue; |
| 698 | |
Holger Hans Peter Freyther | 8330c1c | 2010-06-17 18:29:42 +0800 | [diff] [blame] | 699 | if (ctr) |
| 700 | rate_ctr_inc(ctr); |
| 701 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 702 | bsc_mgcp_free_endpoint(bsc->nat, i); |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 703 | mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]); |
| 704 | } |
| 705 | } |