Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1 | /* OpenBSC Abis/IP proxy ip.access nanoBTS */ |
| 2 | |
| 3 | /* (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 5 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <signal.h> |
| 30 | #include <time.h> |
| 31 | #include <sys/fcntl.h> |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <netinet/in.h> |
| 36 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 37 | #define _GNU_SOURCE |
| 38 | #include <getopt.h> |
| 39 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 40 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 41 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 42 | #include <osmocom/core/select.h> |
| 43 | #include <osmocom/gsm/tlv.h> |
| 44 | #include <osmocom/core/msgb.h> |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 45 | #include <openbsc/debug.h> |
| 46 | #include <openbsc/ipaccess.h> |
Pablo Neira Ayuso | da2f769 | 2011-04-05 18:33:28 +0200 | [diff] [blame] | 47 | #include <openbsc/socket.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 48 | #include <osmocom/core/talloc.h> |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 49 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 50 | /* one instance of an ip.access protocol proxy */ |
| 51 | struct ipa_proxy { |
| 52 | /* socket where we listen for incoming OML from BTS */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 53 | struct osmo_fd oml_listen_fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 54 | /* socket where we listen for incoming RSL from BTS */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 55 | struct osmo_fd rsl_listen_fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 56 | /* list of BTS's (struct ipa_bts_conn */ |
| 57 | struct llist_head bts_list; |
| 58 | /* the BSC reconnect timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 59 | struct osmo_timer_list reconn_timer; |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 60 | /* global GPRS NS data */ |
| 61 | struct in_addr gprs_addr; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 62 | struct in_addr listen_addr; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /* global pointer to the proxy structure */ |
| 66 | static struct ipa_proxy *ipp; |
| 67 | |
| 68 | struct ipa_proxy_conn { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 69 | struct osmo_fd fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 70 | struct llist_head tx_queue; |
| 71 | struct ipa_bts_conn *bts_conn; |
| 72 | }; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 73 | #define MAX_TRX 4 |
| 74 | |
| 75 | /* represents a particular BTS in our proxy */ |
| 76 | struct ipa_bts_conn { |
| 77 | /* list of BTS's (ipa_proxy->bts_list) */ |
| 78 | struct llist_head list; |
| 79 | /* back pointer to the proxy which we belong to */ |
| 80 | struct ipa_proxy *ipp; |
| 81 | /* the unit ID as determined by CCM */ |
| 82 | struct { |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 83 | uint16_t site_id; |
| 84 | uint16_t bts_id; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 85 | } unit_id; |
| 86 | |
| 87 | /* incoming connections from BTS */ |
| 88 | struct ipa_proxy_conn *oml_conn; |
| 89 | struct ipa_proxy_conn *rsl_conn[MAX_TRX]; |
| 90 | |
| 91 | /* outgoing connections to BSC */ |
| 92 | struct ipa_proxy_conn *bsc_oml_conn; |
| 93 | struct ipa_proxy_conn *bsc_rsl_conn[MAX_TRX]; |
| 94 | |
| 95 | /* UDP sockets for BTS and BSC injection */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 96 | struct osmo_fd udp_bts_fd; |
| 97 | struct osmo_fd udp_bsc_fd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 98 | |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 99 | /* NS data */ |
| 100 | struct in_addr bts_addr; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 101 | struct osmo_fd gprs_ns_fd; |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 102 | int gprs_local_port; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 103 | uint16_t gprs_orig_port; |
| 104 | uint32_t gprs_orig_ip; |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 105 | |
Harald Welte | 36ac775 | 2011-07-16 13:38:48 +0200 | [diff] [blame] | 106 | char *id_tags[256]; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 107 | uint8_t *id_resp; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 108 | unsigned int id_resp_len; |
| 109 | }; |
| 110 | |
| 111 | enum ipp_fd_type { |
| 112 | OML_FROM_BTS = 1, |
| 113 | RSL_FROM_BTS = 2, |
| 114 | OML_TO_BSC = 3, |
| 115 | RSL_TO_BSC = 4, |
| 116 | UDP_TO_BTS = 5, |
| 117 | UDP_TO_BSC = 6, |
| 118 | }; |
| 119 | |
| 120 | /* some of the code against we link from OpenBSC needs this */ |
| 121 | void *tall_bsc_ctx; |
| 122 | |
| 123 | static char *listen_ipaddr; |
| 124 | static char *bsc_ipaddr; |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 125 | static char *gprs_ns_ipaddr; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 126 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 127 | static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 128 | |
Holger Hans Peter Freyther | 3dac881 | 2010-06-09 14:39:22 +0800 | [diff] [blame] | 129 | #define PROXY_ALLOC_SIZE 1200 |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 130 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 131 | static struct ipa_bts_conn *find_bts_by_unitid(struct ipa_proxy *ipp, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 132 | uint16_t site_id, |
| 133 | uint16_t bts_id) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 134 | { |
| 135 | struct ipa_bts_conn *ipbc; |
| 136 | |
| 137 | llist_for_each_entry(ipbc, &ipp->bts_list, list) { |
| 138 | if (ipbc->unit_id.site_id == site_id && |
| 139 | ipbc->unit_id.bts_id == bts_id) |
| 140 | return ipbc; |
| 141 | } |
| 142 | |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | struct ipa_proxy_conn *alloc_conn(void) |
| 147 | { |
| 148 | struct ipa_proxy_conn *ipc; |
| 149 | |
| 150 | ipc = talloc_zero(tall_bsc_ctx, struct ipa_proxy_conn); |
| 151 | if (!ipc) |
| 152 | return NULL; |
| 153 | |
| 154 | INIT_LLIST_HEAD(&ipc->tx_queue); |
| 155 | |
| 156 | return ipc; |
| 157 | } |
| 158 | |
| 159 | static int store_idtags(struct ipa_bts_conn *ipbc, struct tlv_parsed *tlvp) |
| 160 | { |
| 161 | unsigned int i, len; |
| 162 | |
| 163 | for (i = 0; i <= 0xff; i++) { |
| 164 | if (!TLVP_PRESENT(tlvp, i)) |
| 165 | continue; |
| 166 | |
| 167 | len = TLVP_LEN(tlvp, i); |
| 168 | #if 0 |
| 169 | if (!ipbc->id_tags[i]) |
| 170 | ipbc->id_tags[i] = talloc_size(tall_bsc_ctx, len); |
| 171 | else |
| 172 | #endif |
Harald Welte | a16ef3d | 2009-12-23 23:35:51 +0100 | [diff] [blame] | 173 | ipbc->id_tags[i] = talloc_realloc_size(ipbc, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 174 | ipbc->id_tags[i], len); |
| 175 | if (!ipbc->id_tags[i]) |
| 176 | return -ENOMEM; |
| 177 | |
| 178 | memset(ipbc->id_tags[i], 0, len); |
| 179 | //memcpy(ipbc->id_tags[i], TLVP_VAL(tlvp, i), len); |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data); |
| 186 | |
| 187 | #define logp_ipbc_uid(ss, lvl, ipbc, trx_id) _logp_ipbc_uid(ss, lvl, __FILE__, __LINE__, ipbc, trx_id) |
| 188 | |
| 189 | static void _logp_ipbc_uid(unsigned int ss, unsigned int lvl, char *file, int line, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 190 | struct ipa_bts_conn *ipbc, uint8_t trx_id) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 191 | { |
| 192 | if (ipbc) |
Harald Welte | dc5062b | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 193 | logp2(ss, lvl, file, line, 0, "(%u/%u/%u) ", ipbc->unit_id.site_id, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 194 | ipbc->unit_id.bts_id, trx_id); |
| 195 | else |
Harald Welte | dc5062b | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 196 | logp2(ss, lvl, file, line, 0, "unknown "); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 199 | static int handle_udp_read(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 200 | { |
| 201 | struct ipa_bts_conn *ipbc = bfd->data; |
| 202 | struct ipa_proxy_conn *other_conn = NULL; |
| 203 | struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP UDP"); |
| 204 | struct ipaccess_head *hh; |
| 205 | int ret; |
| 206 | |
| 207 | /* with UDP sockets, we cannot read partial packets but have to read |
| 208 | * all of it in one go */ |
| 209 | hh = (struct ipaccess_head *) msg->data; |
| 210 | ret = recv(bfd->fd, msg->data, msg->data_len, 0); |
| 211 | if (ret < 0) { |
Harald Welte | fb33957 | 2009-12-24 13:35:18 +0100 | [diff] [blame] | 212 | if (errno != EAGAIN) |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 213 | LOGP(DLINP, LOGL_ERROR, "recv error %s\n", strerror(errno)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 214 | msgb_free(msg); |
| 215 | return ret; |
| 216 | } |
| 217 | if (ret == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 218 | DEBUGP(DLINP, "UDP peer disappeared, dead socket\n"); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 219 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 220 | close(bfd->fd); |
| 221 | bfd->fd = -1; |
| 222 | msgb_free(msg); |
| 223 | return -EIO; |
| 224 | } |
| 225 | if (ret < sizeof(*hh)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 226 | DEBUGP(DLINP, "could not even read header!?!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 227 | msgb_free(msg); |
| 228 | return -EIO; |
| 229 | } |
| 230 | msgb_put(msg, ret); |
| 231 | msg->l2h = msg->data + sizeof(*hh); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 232 | DEBUGP(DLMI, "UDP RX: %s\n", osmo_hexdump(msg->data, msg->len)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 233 | |
| 234 | if (hh->len != msg->len - sizeof(*hh)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 235 | DEBUGP(DLINP, "length (%u/%u) disagrees with header(%u)\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 236 | msg->len, msg->len - 3, hh->len); |
| 237 | msgb_free(msg); |
| 238 | return -EIO; |
| 239 | } |
| 240 | |
| 241 | switch (bfd->priv_nr & 0xff) { |
| 242 | case UDP_TO_BTS: |
| 243 | /* injection towards BTS */ |
| 244 | switch (hh->proto) { |
| 245 | case IPAC_PROTO_RSL: |
| 246 | /* FIXME: what to do about TRX > 0 */ |
| 247 | other_conn = ipbc->rsl_conn[0]; |
| 248 | break; |
| 249 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 250 | DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 251 | "OML FD\n", hh->proto); |
| 252 | /* fall through */ |
| 253 | case IPAC_PROTO_IPACCESS: |
| 254 | case IPAC_PROTO_OML: |
| 255 | other_conn = ipbc->oml_conn; |
| 256 | break; |
| 257 | } |
| 258 | break; |
| 259 | case UDP_TO_BSC: |
| 260 | /* injection towards BSC */ |
| 261 | switch (hh->proto) { |
| 262 | case IPAC_PROTO_RSL: |
| 263 | /* FIXME: what to do about TRX > 0 */ |
| 264 | other_conn = ipbc->bsc_rsl_conn[0]; |
| 265 | break; |
| 266 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 267 | DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 268 | "OML FD\n", hh->proto); |
| 269 | case IPAC_PROTO_IPACCESS: |
| 270 | case IPAC_PROTO_OML: |
| 271 | other_conn = ipbc->bsc_oml_conn; |
| 272 | break; |
| 273 | } |
| 274 | break; |
| 275 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 276 | DEBUGP(DLINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 277 | break; |
| 278 | } |
| 279 | |
| 280 | if (other_conn) { |
| 281 | /* enqueue the message for TX on the respective FD */ |
| 282 | msgb_enqueue(&other_conn->tx_queue, msg); |
| 283 | other_conn->fd.when |= BSC_FD_WRITE; |
| 284 | } else |
| 285 | msgb_free(msg); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 290 | static int handle_udp_write(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 291 | { |
| 292 | /* not implemented yet */ |
| 293 | bfd->when &= ~BSC_FD_WRITE; |
| 294 | |
| 295 | return -EIO; |
| 296 | } |
| 297 | |
| 298 | /* callback from select.c in case one of the fd's can be read/written */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 299 | static int udp_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 300 | { |
| 301 | int rc = 0; |
| 302 | |
| 303 | if (what & BSC_FD_READ) |
| 304 | rc = handle_udp_read(bfd); |
| 305 | if (what & BSC_FD_WRITE) |
| 306 | rc = handle_udp_write(bfd); |
| 307 | |
| 308 | return rc; |
| 309 | } |
| 310 | |
| 311 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 312 | static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 313 | uint16_t site_id, uint16_t bts_id, |
| 314 | uint16_t trx_id, struct tlv_parsed *tlvp, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 315 | struct msgb *msg) |
| 316 | { |
| 317 | struct ipa_bts_conn *ipbc; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 318 | uint16_t udp_port; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 319 | int ret = 0; |
| 320 | struct sockaddr_in sin; |
| 321 | |
| 322 | memset(&sin, 0, sizeof(sin)); |
| 323 | sin.sin_family = AF_INET; |
| 324 | inet_aton(bsc_ipaddr, &sin.sin_addr); |
| 325 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 326 | DEBUGP(DLINP, "(%u/%u/%u) New BTS connection: ", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 327 | site_id, bts_id, trx_id); |
| 328 | |
| 329 | /* OML needs to be established before RSL */ |
| 330 | if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 331 | DEBUGPC(DLINP, "Not a OML connection ?!?\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 332 | return -EIO; |
| 333 | } |
| 334 | |
| 335 | /* allocate new BTS connection data structure */ |
| 336 | ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn); |
| 337 | if (!ipbc) { |
| 338 | ret = -ENOMEM; |
| 339 | goto err_out; |
| 340 | } |
| 341 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 342 | DEBUGPC(DLINP, "Created BTS Conn data structure\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 343 | ipbc->ipp = ipp; |
| 344 | ipbc->unit_id.site_id = site_id; |
| 345 | ipbc->unit_id.bts_id = bts_id; |
| 346 | ipbc->oml_conn = ipc; |
| 347 | ipc->bts_conn = ipbc; |
| 348 | |
| 349 | /* store the content of the ID TAGS for later reference */ |
| 350 | store_idtags(ipbc, tlvp); |
| 351 | ipbc->id_resp_len = msg->len; |
| 352 | ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len); |
| 353 | memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len); |
| 354 | |
| 355 | /* Create OML TCP connection towards BSC */ |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 356 | sin.sin_port = htons(IPA_TCP_PORT_OML); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 357 | ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc); |
| 358 | if (!ipbc->bsc_oml_conn) { |
| 359 | ret = -EIO; |
| 360 | goto err_bsc_conn; |
| 361 | } |
| 362 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 363 | DEBUGP(DLINP, "(%u/%u/%u) OML Connected to BSC\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 364 | site_id, bts_id, trx_id); |
| 365 | |
| 366 | /* Create UDP socket for BTS packet injection */ |
| 367 | udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100); |
Pablo Neira Ayuso | da2f769 | 2011-04-05 18:33:28 +0200 | [diff] [blame] | 368 | ret = make_sock(&ipbc->udp_bts_fd, IPPROTO_UDP, INADDR_ANY, udp_port, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 369 | UDP_TO_BTS, udp_fd_cb, ipbc); |
| 370 | if (ret < 0) |
| 371 | goto err_udp_bts; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 372 | DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 373 | "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port); |
| 374 | |
| 375 | /* Create UDP socket for BSC packet injection */ |
| 376 | udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100); |
Pablo Neira Ayuso | da2f769 | 2011-04-05 18:33:28 +0200 | [diff] [blame] | 377 | ret = make_sock(&ipbc->udp_bsc_fd, IPPROTO_UDP, INADDR_ANY, udp_port, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 378 | UDP_TO_BSC, udp_fd_cb, ipbc); |
| 379 | if (ret < 0) |
| 380 | goto err_udp_bsc; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 381 | DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 382 | "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 383 | |
| 384 | |
| 385 | /* GPRS NS related code */ |
| 386 | if (gprs_ns_ipaddr) { |
| 387 | struct sockaddr_in sock; |
| 388 | socklen_t len = sizeof(sock); |
Pablo Neira Ayuso | 7e73700 | 2011-04-11 16:33:17 +0200 | [diff] [blame] | 389 | struct in_addr addr; |
| 390 | uint32_t ip; |
| 391 | |
| 392 | inet_aton(listen_ipaddr, &addr); |
| 393 | ip = ntohl(addr.s_addr); /* make_sock() needs host byte order */ |
| 394 | ret = make_sock(&ipbc->gprs_ns_fd, IPPROTO_UDP, ip, 0, 0, |
| 395 | gprs_ns_cb, ipbc); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 396 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 397 | LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 398 | goto err_udp_bsc; |
| 399 | } |
| 400 | |
| 401 | ret = getsockname(ipbc->gprs_ns_fd.fd, (struct sockaddr* ) &sock, &len); |
| 402 | ipbc->gprs_local_port = ntohs(sock.sin_port); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 403 | LOGP(DLINP, LOGL_NOTICE, |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 404 | "Created GPRS NS Socket. Listening on: %s:%d\n", |
| 405 | inet_ntoa(sock.sin_addr), ipbc->gprs_local_port); |
| 406 | |
| 407 | ret = getpeername(bfd->fd, (struct sockaddr* ) &sock, &len); |
| 408 | ipbc->bts_addr = sock.sin_addr; |
| 409 | } |
| 410 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 411 | llist_add(&ipbc->list, &ipp->bts_list); |
| 412 | |
| 413 | return 0; |
| 414 | |
| 415 | err_udp_bsc: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 416 | osmo_fd_unregister(&ipbc->udp_bts_fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 417 | err_udp_bts: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 418 | osmo_fd_unregister(&ipbc->bsc_oml_conn->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 419 | close(ipbc->bsc_oml_conn->fd.fd); |
| 420 | talloc_free(ipbc->bsc_oml_conn); |
| 421 | ipbc->bsc_oml_conn = NULL; |
| 422 | err_bsc_conn: |
| 423 | talloc_free(ipbc->id_resp); |
| 424 | talloc_free(ipbc); |
| 425 | #if 0 |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 426 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 427 | close(bfd->fd); |
| 428 | talloc_free(bfd); |
| 429 | #endif |
| 430 | err_out: |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg, |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 435 | struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 436 | { |
| 437 | struct tlv_parsed tlvp; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 438 | uint8_t msg_type = *(msg->l2h); |
| 439 | uint16_t site_id, bts_id, trx_id; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 440 | struct ipa_bts_conn *ipbc; |
| 441 | int ret = 0; |
| 442 | |
| 443 | switch (msg_type) { |
| 444 | case IPAC_MSGT_PING: |
Pablo Neira Ayuso | 22f58a9 | 2011-04-07 14:15:06 +0200 | [diff] [blame] | 445 | ret = ipaccess_send_pong(bfd->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 446 | break; |
| 447 | case IPAC_MSGT_PONG: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 448 | DEBUGP(DLMI, "PONG!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 449 | break; |
| 450 | case IPAC_MSGT_ID_RESP: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 451 | DEBUGP(DLMI, "ID_RESP "); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 452 | /* parse tags, search for Unit ID */ |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 453 | ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2, |
Pablo Neira Ayuso | 625295b | 2011-04-07 14:15:16 +0200 | [diff] [blame] | 454 | msgb_l2len(msg)-2); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 455 | DEBUGP(DLMI, "\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 456 | |
| 457 | if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 458 | LOGP(DLINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 459 | return -EIO; |
| 460 | } |
| 461 | |
| 462 | /* lookup BTS, create sign_link, ... */ |
Holger Hans Peter Freyther | 82ae716 | 2010-03-30 15:20:46 +0200 | [diff] [blame] | 463 | site_id = bts_id = trx_id = 0; |
Pablo Neira Ayuso | c281b4e | 2011-04-07 14:15:20 +0200 | [diff] [blame] | 464 | ipaccess_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT), |
| 465 | &site_id, &bts_id, &trx_id); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 466 | ipbc = find_bts_by_unitid(ipp, site_id, bts_id); |
| 467 | if (!ipbc) { |
| 468 | /* We have not found an ipbc (per-bts proxy instance) |
| 469 | * for this BTS yet. The first connection of a new BTS must |
| 470 | * be a OML connection. We allocate the associated data structures, |
| 471 | * and try to connect to the remote end */ |
| 472 | |
| 473 | return ipbc_alloc_connect(ipc, bfd, site_id, bts_id, |
| 474 | trx_id, &tlvp, msg); |
| 475 | /* if this fails, the caller will clean up bfd */ |
| 476 | } else { |
| 477 | struct sockaddr_in sin; |
| 478 | memset(&sin, 0, sizeof(sin)); |
| 479 | sin.sin_family = AF_INET; |
| 480 | inet_aton(bsc_ipaddr, &sin.sin_addr); |
| 481 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 482 | DEBUGP(DLINP, "Identified BTS %u/%u/%u\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 483 | site_id, bts_id, trx_id); |
| 484 | |
| 485 | if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 486 | LOGP(DLINP, LOGL_ERROR, "Second OML connection from " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 487 | "same BTS ?!?\n"); |
| 488 | return 0; |
| 489 | } |
| 490 | |
Harald Welte | 36ac775 | 2011-07-16 13:38:48 +0200 | [diff] [blame] | 491 | if (trx_id >= MAX_TRX) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 492 | LOGP(DLINP, LOGL_ERROR, "We don't support more " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 493 | "than %u TRX\n", MAX_TRX); |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | |
| 497 | ipc->bts_conn = ipbc; |
| 498 | /* store TRX number in higher 8 bit of the bfd private number */ |
| 499 | bfd->priv_nr |= trx_id << 8; |
| 500 | ipbc->rsl_conn[trx_id] = ipc; |
| 501 | |
| 502 | /* Create RSL TCP connection towards BSC */ |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 503 | sin.sin_port = htons(IPA_TCP_PORT_RSL); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 504 | ipbc->bsc_rsl_conn[trx_id] = |
| 505 | connect_bsc(&sin, RSL_TO_BSC | (trx_id << 8), ipbc); |
| 506 | if (!ipbc->bsc_oml_conn) |
| 507 | return -EIO; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 508 | DEBUGP(DLINP, "(%u/%u/%u) Connected RSL to BSC\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 509 | site_id, bts_id, trx_id); |
| 510 | } |
| 511 | break; |
| 512 | case IPAC_MSGT_ID_GET: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 513 | DEBUGP(DLMI, "ID_GET\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 514 | if ((bfd->priv_nr & 0xff) != OML_TO_BSC && |
| 515 | (bfd->priv_nr & 0xff) != RSL_TO_BSC) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 516 | DEBUGP(DLINP, "IDentity REQuest from BTS ?!?\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 517 | return -EIO; |
| 518 | } |
| 519 | ipbc = ipc->bts_conn; |
| 520 | if (!ipbc) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 521 | DEBUGP(DLINP, "ID_GET from BSC before we have ID_RESP from BTS\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 522 | return -EIO; |
| 523 | } |
| 524 | ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len); |
| 525 | break; |
| 526 | case IPAC_MSGT_ID_ACK: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 527 | DEBUGP(DLMI, "ID_ACK? -> ACK!\n"); |
Pablo Neira Ayuso | 22f58a9 | 2011-04-07 14:15:06 +0200 | [diff] [blame] | 528 | ret = ipaccess_send_id_ack(bfd->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 529 | break; |
Holger Hans Peter Freyther | 0897dad | 2010-06-09 17:38:59 +0800 | [diff] [blame] | 530 | default: |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 531 | LOGP(DLMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type); |
Holger Hans Peter Freyther | 0897dad | 2010-06-09 17:38:59 +0800 | [diff] [blame] | 532 | return 1; |
| 533 | break; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 534 | } |
| 535 | return 0; |
| 536 | } |
| 537 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 538 | struct msgb *ipaccess_proxy_read_msg(struct osmo_fd *bfd, int *error) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 539 | { |
| 540 | struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP"); |
| 541 | struct ipaccess_head *hh; |
| 542 | int len, ret = 0; |
| 543 | |
| 544 | if (!msg) { |
| 545 | *error = -ENOMEM; |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | /* first read our 3-byte header */ |
| 550 | hh = (struct ipaccess_head *) msg->data; |
| 551 | ret = recv(bfd->fd, msg->data, 3, 0); |
| 552 | if (ret < 0) { |
Harald Welte | fb33957 | 2009-12-24 13:35:18 +0100 | [diff] [blame] | 553 | if (errno != EAGAIN) |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 554 | LOGP(DLINP, LOGL_ERROR, "recv error: %s\n", strerror(errno)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 555 | msgb_free(msg); |
| 556 | *error = ret; |
| 557 | return NULL; |
| 558 | } else if (ret == 0) { |
| 559 | msgb_free(msg); |
| 560 | *error = ret; |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | msgb_put(msg, ret); |
| 565 | |
| 566 | /* then read te length as specified in header */ |
| 567 | msg->l2h = msg->data + sizeof(*hh); |
| 568 | len = ntohs(hh->len); |
| 569 | ret = recv(bfd->fd, msg->l2h, len, 0); |
| 570 | if (ret < len) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 571 | LOGP(DLINP, LOGL_ERROR, "short read!\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 572 | msgb_free(msg); |
| 573 | *error = -EIO; |
| 574 | return NULL; |
| 575 | } |
| 576 | msgb_put(msg, ret); |
| 577 | |
| 578 | return msg; |
| 579 | } |
| 580 | |
| 581 | static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc, |
| 582 | unsigned int priv_nr) |
| 583 | { |
| 584 | struct ipa_proxy_conn *bsc_conn; |
| 585 | unsigned int trx_id = priv_nr >> 8; |
| 586 | |
| 587 | switch (priv_nr & 0xff) { |
| 588 | case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */ |
| 589 | bsc_conn = ipbc->bsc_oml_conn; |
| 590 | break; |
| 591 | case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */ |
| 592 | bsc_conn = ipbc->bsc_rsl_conn[trx_id]; |
| 593 | break; |
| 594 | case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */ |
| 595 | bsc_conn = ipbc->oml_conn; |
| 596 | break; |
| 597 | case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */ |
| 598 | bsc_conn = ipbc->rsl_conn[trx_id]; |
| 599 | break; |
| 600 | default: |
| 601 | bsc_conn = NULL; |
| 602 | break; |
| 603 | } |
| 604 | return bsc_conn; |
| 605 | } |
| 606 | |
| 607 | static void reconn_tmr_cb(void *data) |
| 608 | { |
| 609 | struct ipa_proxy *ipp = data; |
| 610 | struct ipa_bts_conn *ipbc; |
| 611 | struct sockaddr_in sin; |
| 612 | int i; |
| 613 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 614 | DEBUGP(DLINP, "Running reconnect timer\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 615 | |
| 616 | memset(&sin, 0, sizeof(sin)); |
| 617 | sin.sin_family = AF_INET; |
| 618 | inet_aton(bsc_ipaddr, &sin.sin_addr); |
| 619 | |
| 620 | llist_for_each_entry(ipbc, &ipp->bts_list, list) { |
| 621 | /* if OML to BSC is dead, try to restore it */ |
| 622 | if (ipbc->oml_conn && !ipbc->bsc_oml_conn) { |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 623 | sin.sin_port = htons(IPA_TCP_PORT_OML); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 624 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0); |
| 625 | LOGPC(DLINP, LOGL_NOTICE, "OML Trying to reconnect\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 626 | ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc); |
| 627 | if (!ipbc->bsc_oml_conn) |
| 628 | goto reschedule; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 629 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0); |
| 630 | LOGPC(DLINP, LOGL_NOTICE, "OML Reconnected\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 631 | } |
| 632 | /* if we (still) don't have a OML connection, skip RSL */ |
| 633 | if (!ipbc->oml_conn || !ipbc->bsc_oml_conn) |
| 634 | continue; |
| 635 | |
| 636 | for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) { |
| 637 | unsigned int priv_nr; |
| 638 | /* don't establish RSL links which we don't have */ |
| 639 | if (!ipbc->rsl_conn[i]) |
| 640 | continue; |
| 641 | if (ipbc->bsc_rsl_conn[i]) |
| 642 | continue; |
| 643 | priv_nr = ipbc->rsl_conn[i]->fd.priv_nr; |
| 644 | priv_nr &= ~0xff; |
| 645 | priv_nr |= RSL_TO_BSC; |
Harald Welte | 87ed5cd | 2009-12-23 22:47:53 +0100 | [diff] [blame] | 646 | sin.sin_port = htons(IPA_TCP_PORT_RSL); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 647 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8); |
| 648 | LOGPC(DLINP, LOGL_NOTICE, "RSL Trying to reconnect\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 649 | ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc); |
| 650 | if (!ipbc->bsc_rsl_conn) |
| 651 | goto reschedule; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 652 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8); |
| 653 | LOGPC(DLINP, LOGL_NOTICE, "RSL Reconnected\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | return; |
| 657 | |
| 658 | reschedule: |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 659 | osmo_timer_schedule(&ipp->reconn_timer, 5, 0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 660 | } |
| 661 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 662 | static void handle_dead_socket(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 663 | { |
| 664 | struct ipa_proxy_conn *ipc = bfd->data; /* local conn */ |
| 665 | struct ipa_proxy_conn *bsc_conn; /* remote conn */ |
| 666 | struct ipa_bts_conn *ipbc = ipc->bts_conn; |
| 667 | unsigned int trx_id = bfd->priv_nr >> 8; |
| 668 | struct msgb *msg, *msg2; |
| 669 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 670 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 671 | close(bfd->fd); |
| 672 | bfd->fd = -1; |
| 673 | |
| 674 | /* FIXME: clear tx_queue, remove all references, etc. */ |
| 675 | llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list) |
| 676 | msgb_free(msg); |
| 677 | |
| 678 | switch (bfd->priv_nr & 0xff) { |
| 679 | case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */ |
Pablo Neira Ayuso | 3c409c2 | 2011-03-27 21:31:48 +0200 | [diff] [blame] | 680 | /* The BTS started a connection with us but we got no |
| 681 | * IPAC_MSGT_ID_RESP message yet, in that scenario we did not |
| 682 | * allocate the ipa_bts_conn structure. */ |
| 683 | if (ipbc == NULL) |
| 684 | break; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 685 | ipbc->oml_conn = NULL; |
| 686 | bsc_conn = ipbc->bsc_oml_conn; |
| 687 | /* close the connection to the BSC */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 688 | osmo_fd_unregister(&bsc_conn->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 689 | close(bsc_conn->fd.fd); |
| 690 | llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list) |
| 691 | msgb_free(msg); |
| 692 | talloc_free(bsc_conn); |
| 693 | ipbc->bsc_oml_conn = NULL; |
| 694 | /* FIXME: do we need to delete the entire ipbc ? */ |
| 695 | break; |
| 696 | case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */ |
| 697 | ipbc->rsl_conn[trx_id] = NULL; |
| 698 | bsc_conn = ipbc->bsc_rsl_conn[trx_id]; |
| 699 | /* close the connection to the BSC */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 700 | osmo_fd_unregister(&bsc_conn->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 701 | close(bsc_conn->fd.fd); |
| 702 | llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list) |
| 703 | msgb_free(msg); |
| 704 | talloc_free(bsc_conn); |
| 705 | ipbc->bsc_rsl_conn[trx_id] = NULL; |
| 706 | break; |
| 707 | case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */ |
| 708 | ipbc->bsc_oml_conn = NULL; |
| 709 | bsc_conn = ipbc->oml_conn; |
| 710 | /* start reconnect timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 711 | osmo_timer_schedule(&ipp->reconn_timer, 5, 0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 712 | break; |
| 713 | case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */ |
| 714 | ipbc->bsc_rsl_conn[trx_id] = NULL; |
| 715 | bsc_conn = ipbc->rsl_conn[trx_id]; |
| 716 | /* start reconnect timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 717 | osmo_timer_schedule(&ipp->reconn_timer, 5, 0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 718 | break; |
| 719 | default: |
| 720 | bsc_conn = NULL; |
| 721 | break; |
| 722 | } |
| 723 | |
| 724 | talloc_free(ipc); |
| 725 | } |
| 726 | |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 727 | static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg) |
| 728 | { |
| 729 | uint8_t *nsvci; |
| 730 | |
| 731 | if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC) |
| 732 | return; |
| 733 | |
| 734 | if (msgb_l2len(msg) != 39) |
| 735 | return; |
| 736 | |
| 737 | /* |
| 738 | * Check if this is a IPA Set Attribute or IPA Set Attribute ACK |
| 739 | * and if the FOM Class is GPRS NSVC0 and then we will patch it. |
| 740 | * |
| 741 | * The patch assumes the message looks like the one from the trace |
| 742 | * but we only match messages with a specific size anyway... So |
| 743 | * this hack should work just fine. |
| 744 | */ |
| 745 | |
| 746 | if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 && |
| 747 | msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 && |
| 748 | msg->l2h[18] == 0xf5 && msg->l2h[19] == 0xf2) { |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 749 | nsvci = &msg->l2h[23]; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 750 | ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8); |
| 751 | ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10); |
| 752 | *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port); |
| 753 | *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 754 | } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 && |
| 755 | msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 && |
| 756 | msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) { |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 757 | nsvci = &msg->l2h[23]; |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 758 | *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port; |
| 759 | *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip; |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 763 | static int handle_tcp_read(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 764 | { |
| 765 | struct ipa_proxy_conn *ipc = bfd->data; |
| 766 | struct ipa_bts_conn *ipbc = ipc->bts_conn; |
| 767 | struct ipa_proxy_conn *bsc_conn; |
Harald Welte | a16ef3d | 2009-12-23 23:35:51 +0100 | [diff] [blame] | 768 | struct msgb *msg; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 769 | struct ipaccess_head *hh; |
| 770 | int ret = 0; |
| 771 | char *btsbsc; |
| 772 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 773 | if ((bfd->priv_nr & 0xff) <= 2) |
| 774 | btsbsc = "BTS"; |
| 775 | else |
| 776 | btsbsc = "BSC"; |
| 777 | |
Pablo Neira Ayuso | 22f58a9 | 2011-04-07 14:15:06 +0200 | [diff] [blame] | 778 | msg = ipaccess_proxy_read_msg(bfd, &ret); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 779 | if (!msg) { |
| 780 | if (ret == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 781 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8); |
| 782 | LOGPC(DLINP, LOGL_NOTICE, "%s disappeared, " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 783 | "dead socket\n", btsbsc); |
| 784 | handle_dead_socket(bfd); |
| 785 | } |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | msgb_put(msg, ret); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 790 | logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8); |
| 791 | DEBUGPC(DLMI, "RX<-%s: %s\n", btsbsc, osmo_hexdump(msg->data, msg->len)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 792 | |
| 793 | hh = (struct ipaccess_head *) msg->data; |
| 794 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 795 | ret = ipaccess_rcvmsg(ipc, msg, bfd); |
| 796 | if (ret < 0) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 797 | osmo_fd_unregister(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 798 | close(bfd->fd); |
| 799 | bfd->fd = -1; |
| 800 | talloc_free(bfd); |
Holger Hans Peter Freyther | 0897dad | 2010-06-09 17:38:59 +0800 | [diff] [blame] | 801 | msgb_free(msg); |
| 802 | return ret; |
| 803 | } else if (ret == 0) { |
| 804 | /* we do not forward parts of the CCM protocol |
| 805 | * through the proxy but rather terminate it ourselves. */ |
| 806 | msgb_free(msg); |
| 807 | return ret; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 808 | } |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | if (!ipbc) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 812 | LOGP(DLINP, LOGL_ERROR, |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 813 | "received %s packet but no ipc->bts_conn?!?\n", btsbsc); |
| 814 | msgb_free(msg); |
| 815 | return -EIO; |
| 816 | } |
| 817 | |
| 818 | bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr); |
| 819 | if (bsc_conn) { |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 820 | if (gprs_ns_ipaddr) |
| 821 | patch_gprs_msg(ipbc, bfd->priv_nr, msg); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 822 | /* enqueue packet towards BSC */ |
| 823 | msgb_enqueue(&bsc_conn->tx_queue, msg); |
| 824 | /* mark respective filedescriptor as 'we want to write' */ |
| 825 | bsc_conn->fd.when |= BSC_FD_WRITE; |
| 826 | } else { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 827 | logp_ipbc_uid(DLINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8); |
| 828 | LOGPC(DLINP, LOGL_INFO, "Dropping packet from %s, " |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 829 | "since remote connection is dead\n", btsbsc); |
| 830 | msgb_free(msg); |
| 831 | } |
| 832 | |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | /* a TCP socket is ready to be written to */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 837 | static int handle_tcp_write(struct osmo_fd *bfd) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 838 | { |
| 839 | struct ipa_proxy_conn *ipc = bfd->data; |
| 840 | struct ipa_bts_conn *ipbc = ipc->bts_conn; |
| 841 | struct llist_head *lh; |
| 842 | struct msgb *msg; |
| 843 | char *btsbsc; |
| 844 | int ret; |
| 845 | |
| 846 | if ((bfd->priv_nr & 0xff) <= 2) |
| 847 | btsbsc = "BTS"; |
| 848 | else |
| 849 | btsbsc = "BSC"; |
| 850 | |
| 851 | |
| 852 | /* get the next msg for this timeslot */ |
| 853 | if (llist_empty(&ipc->tx_queue)) { |
| 854 | bfd->when &= ~BSC_FD_WRITE; |
| 855 | return 0; |
| 856 | } |
| 857 | lh = ipc->tx_queue.next; |
| 858 | llist_del(lh); |
| 859 | msg = llist_entry(lh, struct msgb, list); |
| 860 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 861 | logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8); |
| 862 | DEBUGPC(DLMI, "TX %04x: %s\n", bfd->priv_nr, |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 863 | osmo_hexdump(msg->data, msg->len)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 864 | |
| 865 | ret = send(bfd->fd, msg->data, msg->len, 0); |
| 866 | msgb_free(msg); |
| 867 | |
| 868 | if (ret == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 869 | logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8); |
| 870 | LOGP(DLINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 871 | handle_dead_socket(bfd); |
| 872 | } |
| 873 | |
| 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | /* callback from select.c in case one of the fd's can be read/written */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 878 | static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 879 | { |
| 880 | int rc = 0; |
| 881 | |
| 882 | if (what & BSC_FD_READ) { |
| 883 | rc = handle_tcp_read(bfd); |
| 884 | if (rc < 0) |
| 885 | return rc; |
| 886 | } |
| 887 | if (what & BSC_FD_WRITE) |
| 888 | rc = handle_tcp_write(bfd); |
| 889 | |
| 890 | return rc; |
| 891 | } |
| 892 | |
| 893 | /* callback of the listening filedescriptor */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 894 | static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what) |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 895 | { |
| 896 | int ret; |
| 897 | struct ipa_proxy_conn *ipc; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 898 | struct osmo_fd *bfd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 899 | struct sockaddr_in sa; |
| 900 | socklen_t sa_len = sizeof(sa); |
| 901 | |
| 902 | if (!(what & BSC_FD_READ)) |
| 903 | return 0; |
| 904 | |
| 905 | ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 906 | if (ret < 0) { |
| 907 | perror("accept"); |
| 908 | return ret; |
| 909 | } |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 910 | DEBUGP(DLINP, "accept()ed new %s link from %s\n", |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 911 | (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL", |
| 912 | inet_ntoa(sa.sin_addr)); |
| 913 | |
| 914 | ipc = alloc_conn(); |
| 915 | if (!ipc) { |
| 916 | close(ret); |
| 917 | return -ENOMEM; |
| 918 | } |
| 919 | |
| 920 | bfd = &ipc->fd; |
| 921 | bfd->fd = ret; |
| 922 | bfd->data = ipc; |
| 923 | bfd->priv_nr = listen_bfd->priv_nr; |
| 924 | bfd->cb = ipaccess_fd_cb; |
| 925 | bfd->when = BSC_FD_READ; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 926 | ret = osmo_fd_register(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 927 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 928 | LOGP(DLINP, LOGL_ERROR, "could not register FD\n"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 929 | close(bfd->fd); |
| 930 | talloc_free(ipc); |
| 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */ |
Pablo Neira Ayuso | 22f58a9 | 2011-04-07 14:15:06 +0200 | [diff] [blame] | 935 | ret = ipaccess_send_id_req(bfd->fd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 940 | static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port) |
| 941 | { |
| 942 | int ret; |
| 943 | struct sockaddr_in addr; |
| 944 | socklen_t len = sizeof(addr); |
| 945 | memset(&addr, 0, sizeof(addr)); |
| 946 | |
| 947 | addr.sin_family = AF_INET; |
Holger Hans Peter Freyther | 21e1c0d | 2010-06-10 15:56:26 +0800 | [diff] [blame] | 948 | addr.sin_port = htons(port); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 949 | addr.sin_addr = ip; |
| 950 | |
| 951 | ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len); |
| 952 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 953 | LOGP(DLINP, LOGL_ERROR, "Failed to forward GPRS message.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 957 | static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what) |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 958 | { |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 959 | struct ipa_bts_conn *bts; |
| 960 | char buf[4096]; |
| 961 | int ret; |
| 962 | struct sockaddr_in sock; |
| 963 | socklen_t len = sizeof(sock); |
| 964 | |
| 965 | /* 1. get the data... */ |
| 966 | ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len); |
| 967 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 968 | LOGP(DLINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno)); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 969 | return -1; |
| 970 | } |
| 971 | |
| 972 | bts = bfd->data; |
| 973 | |
| 974 | /* 2. figure out where to send it to */ |
| 975 | if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 976 | LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from network.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 977 | send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000); |
| 978 | } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 979 | LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n"); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 980 | send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000); |
Holger Hans Peter Freyther | 21e1c0d | 2010-06-10 15:56:26 +0800 | [diff] [blame] | 981 | } else { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 982 | LOGP(DLINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr)); |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 983 | } |
| 984 | |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 985 | return 0; |
| 986 | } |
| 987 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 988 | /* Actively connect to a BSC. */ |
| 989 | static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data) |
| 990 | { |
| 991 | struct ipa_proxy_conn *ipc; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 992 | struct osmo_fd *bfd; |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 993 | int ret, on = 1; |
| 994 | |
| 995 | ipc = alloc_conn(); |
| 996 | if (!ipc) |
| 997 | return NULL; |
| 998 | |
| 999 | ipc->bts_conn = data; |
| 1000 | |
| 1001 | bfd = &ipc->fd; |
| 1002 | bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1003 | bfd->cb = ipaccess_fd_cb; |
| 1004 | bfd->when = BSC_FD_READ | BSC_FD_WRITE; |
| 1005 | bfd->data = ipc; |
| 1006 | bfd->priv_nr = priv_nr; |
| 1007 | |
| 1008 | setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 1009 | |
| 1010 | ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa)); |
| 1011 | if (ret < 0) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1012 | LOGP(DLINP, LOGL_ERROR, "Could not connect socket: %s\n", |
Holger Hans Peter Freyther | 6cb9b23 | 2010-06-09 15:15:11 +0800 | [diff] [blame] | 1013 | inet_ntoa(sa->sin_addr)); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1014 | close(bfd->fd); |
| 1015 | talloc_free(ipc); |
| 1016 | return NULL; |
| 1017 | } |
| 1018 | |
| 1019 | /* pre-fill tx_queue with identity request */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1020 | ret = osmo_fd_register(bfd); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1021 | if (ret < 0) { |
| 1022 | close(bfd->fd); |
| 1023 | talloc_free(ipc); |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | return ipc; |
| 1028 | } |
| 1029 | |
| 1030 | static int ipaccess_proxy_setup(void) |
| 1031 | { |
| 1032 | int ret; |
| 1033 | |
| 1034 | ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy); |
| 1035 | if (!ipp) |
| 1036 | return -ENOMEM; |
| 1037 | INIT_LLIST_HEAD(&ipp->bts_list); |
| 1038 | ipp->reconn_timer.cb = reconn_tmr_cb; |
| 1039 | ipp->reconn_timer.data = ipp; |
| 1040 | |
| 1041 | /* Listen for OML connections */ |
Pablo Neira Ayuso | 3ab864a | 2011-04-07 14:15:02 +0200 | [diff] [blame] | 1042 | ret = make_sock(&ipp->oml_listen_fd, IPPROTO_TCP, INADDR_ANY, |
| 1043 | IPA_TCP_PORT_OML, OML_FROM_BTS, listen_fd_cb, NULL); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1044 | if (ret < 0) |
| 1045 | return ret; |
| 1046 | |
| 1047 | /* Listen for RSL connections */ |
Pablo Neira Ayuso | 3ab864a | 2011-04-07 14:15:02 +0200 | [diff] [blame] | 1048 | ret = make_sock(&ipp->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY, |
| 1049 | IPA_TCP_PORT_RSL, RSL_FROM_BTS, listen_fd_cb, NULL); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1050 | |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1051 | if (ret < 0) |
| 1052 | return ret; |
| 1053 | |
| 1054 | /* Connect the GPRS NS Socket */ |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 1055 | if (gprs_ns_ipaddr) { |
Holger Hans Peter Freyther | 952aba7 | 2010-06-09 17:07:43 +0800 | [diff] [blame] | 1056 | inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr); |
Holger Hans Peter Freyther | 4fe22cc | 2010-06-10 15:57:08 +0800 | [diff] [blame] | 1057 | inet_aton(listen_ipaddr, &ipp->listen_addr); |
| 1058 | } |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1059 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | static void signal_handler(int signal) |
| 1064 | { |
| 1065 | fprintf(stdout, "signal %u received\n", signal); |
| 1066 | |
| 1067 | switch (signal) { |
| 1068 | case SIGABRT: |
| 1069 | /* in case of abort, we want to obtain a talloc report |
| 1070 | * and then return to the caller, who will abort the process */ |
| 1071 | case SIGUSR1: |
| 1072 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1073 | break; |
| 1074 | default: |
| 1075 | break; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Harald Welte | d4ab13b | 2011-07-16 13:39:44 +0200 | [diff] [blame] | 1079 | static void print_help(void) |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1080 | { |
| 1081 | printf(" ipaccess-proxy is a proxy BTS.\n"); |
| 1082 | printf(" -h --help. This help text.\n"); |
| 1083 | printf(" -l --listen IP. The ip to listen to.\n"); |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1084 | printf(" -b --bsc IP. The BSC IP address.\n"); |
| 1085 | printf(" -g --gprs IP. Take GPRS NS from that IP.\n"); |
| 1086 | printf("\n"); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1087 | printf(" -s --disable-color. Disable the color inside the logging message.\n"); |
| 1088 | printf(" -e --log-level number. Set the global loglevel.\n"); |
| 1089 | printf(" -T --timestamp. Prefix every log message with a timestamp.\n"); |
| 1090 | printf(" -V --version. Print the version of OpenBSC.\n"); |
| 1091 | } |
| 1092 | |
Harald Welte | d4ab13b | 2011-07-16 13:39:44 +0200 | [diff] [blame] | 1093 | static void print_usage(void) |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1094 | { |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1095 | printf("Usage: ipaccess-proxy [options]\n"); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1098 | enum { |
| 1099 | IPA_PROXY_OPT_LISTEN_NONE = 0, |
| 1100 | IPA_PROXY_OPT_LISTEN_IP = (1 << 0), |
| 1101 | IPA_PROXY_OPT_BSC_IP = (1 << 1), |
| 1102 | }; |
| 1103 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1104 | static void handle_options(int argc, char** argv) |
| 1105 | { |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1106 | int options_mask = 0; |
| 1107 | |
Pablo Neira Ayuso | 25ffe54 | 2011-04-11 16:33:03 +0200 | [diff] [blame] | 1108 | /* disable explicit missing arguments error output from getopt_long */ |
| 1109 | opterr = 0; |
| 1110 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1111 | while (1) { |
| 1112 | int option_index = 0, c; |
| 1113 | static struct option long_options[] = { |
| 1114 | {"help", 0, 0, 'h'}, |
| 1115 | {"disable-color", 0, 0, 's'}, |
| 1116 | {"timestamp", 0, 0, 'T'}, |
| 1117 | {"log-level", 1, 0, 'e'}, |
| 1118 | {"listen", 1, 0, 'l'}, |
| 1119 | {"bsc", 1, 0, 'b'}, |
| 1120 | {0, 0, 0, 0} |
| 1121 | }; |
| 1122 | |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1123 | c = getopt_long(argc, argv, "hsTe:l:b:g:", |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1124 | long_options, &option_index); |
| 1125 | if (c == -1) |
| 1126 | break; |
| 1127 | |
| 1128 | switch (c) { |
| 1129 | case 'h': |
| 1130 | print_usage(); |
| 1131 | print_help(); |
| 1132 | exit(0); |
| 1133 | case 'l': |
| 1134 | listen_ipaddr = optarg; |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1135 | options_mask |= IPA_PROXY_OPT_LISTEN_IP; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1136 | break; |
| 1137 | case 'b': |
| 1138 | bsc_ipaddr = optarg; |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1139 | options_mask |= IPA_PROXY_OPT_BSC_IP; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1140 | break; |
Holger Hans Peter Freyther | 6147c5d | 2010-06-09 16:20:39 +0800 | [diff] [blame] | 1141 | case 'g': |
| 1142 | gprs_ns_ipaddr = optarg; |
| 1143 | break; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1144 | case 's': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1145 | log_set_use_color(osmo_stderr_target, 0); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1146 | break; |
| 1147 | case 'T': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1148 | log_set_print_timestamp(osmo_stderr_target, 1); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1149 | break; |
| 1150 | case 'e': |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1151 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1152 | break; |
Pablo Neira Ayuso | 25ffe54 | 2011-04-11 16:33:03 +0200 | [diff] [blame] | 1153 | case '?': |
| 1154 | if (optopt) { |
| 1155 | printf("ERROR: missing mandatory argument " |
| 1156 | "for `%s' option\n", argv[optind-1]); |
| 1157 | } else { |
| 1158 | printf("ERROR: unknown option `%s'\n", |
| 1159 | argv[optind-1]); |
| 1160 | } |
| 1161 | print_usage(); |
| 1162 | print_help(); |
| 1163 | exit(EXIT_FAILURE); |
| 1164 | break; |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1165 | default: |
| 1166 | /* ignore */ |
| 1167 | break; |
| 1168 | } |
| 1169 | } |
Pablo Neira Ayuso | 9f1294d | 2011-04-07 18:47:39 +0200 | [diff] [blame] | 1170 | if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) |
| 1171 | != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) { |
| 1172 | printf("ERROR: You have to specify `--listen' and `--bsc' " |
| 1173 | "options at least.\n"); |
| 1174 | print_usage(); |
| 1175 | print_help(); |
| 1176 | exit(EXIT_FAILURE); |
| 1177 | } |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1180 | int main(int argc, char **argv) |
| 1181 | { |
| 1182 | int rc; |
| 1183 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1184 | tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy"); |
| 1185 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1186 | osmo_init_logging(&log_info); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 1187 | log_parse_category_mask(osmo_stderr_target, "DLINP:DLMI"); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1188 | |
Holger Hans Peter Freyther | 1a0c2b7 | 2010-06-09 14:59:09 +0800 | [diff] [blame] | 1189 | handle_options(argc, argv); |
| 1190 | |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1191 | rc = ipaccess_proxy_setup(); |
| 1192 | if (rc < 0) |
| 1193 | exit(1); |
| 1194 | |
| 1195 | signal(SIGUSR1, &signal_handler); |
| 1196 | signal(SIGABRT, &signal_handler); |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1197 | osmo_init_ignore_signals(); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1198 | |
| 1199 | while (1) { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1200 | osmo_select_main(0); |
Harald Welte | 2ca7c31 | 2009-12-23 22:44:04 +0100 | [diff] [blame] | 1201 | } |
| 1202 | } |