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