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