blob: 00b6340529546d0473c1658025877b21209409bb [file] [log] [blame]
Harald Welte2ca7c312009-12-23 22:44:04 +01001/* 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>
38#include <openbsc/select.h>
39#include <openbsc/tlv.h>
40#include <openbsc/msgb.h>
41#include <openbsc/debug.h>
42#include <openbsc/ipaccess.h>
43#include <openbsc/talloc.h>
44
45static struct debug_target *stderr_target;
46
47/* one instance of an ip.access protocol proxy */
48struct 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 */
60static struct ipa_proxy *ipp;
61
62struct 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 */
71struct 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
99enum 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 */
109void *tall_bsc_ctx;
110
111static char *listen_ipaddr;
112static char *bsc_ipaddr;
113
114#define PROXY_ALLOC_SIZE 300
115
116static const u_int8_t pong[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_PONG };
117static const u_int8_t id_ack[] = { 0, 1, IPAC_PROTO_IPACCESS, IPAC_MSGT_ID_ACK };
118static 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
129static 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
141static 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
149static 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
169static 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
206static 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
221struct 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
234static 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
248 ipbc->id_tags[i] = talloc_realloc_size(tall_bsc_ctx,
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
260static 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
264static 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)
268 debugp2(ss, lvl, file, line, 0, "(%u/%u/%u) ", ipbc->unit_id.site_id,
269 ipbc->unit_id.bts_id, trx_id);
270 else
271 debugp2(ss, lvl, file, line, 0, "unknown ");
272}
273
274/* UDP socket handling */
275
276static 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
311static 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) {
324 DEBUGP(DINP, "recv error %s\n", strerror(errno));
325 msgb_free(msg);
326 return ret;
327 }
328 if (ret == 0) {
329 DEBUGP(DINP, "UDP peer disappeared, dead socket\n");
330 bsc_unregister_fd(bfd);
331 close(bfd->fd);
332 bfd->fd = -1;
333 msgb_free(msg);
334 return -EIO;
335 }
336 if (ret < sizeof(*hh)) {
337 DEBUGP(DINP, "could not even read header!?!\n");
338 msgb_free(msg);
339 return -EIO;
340 }
341 msgb_put(msg, ret);
342 msg->l2h = msg->data + sizeof(*hh);
343 DEBUGP(DMI, "UDP RX: %s\n", hexdump(msg->data, msg->len));
344
345 if (hh->len != msg->len - sizeof(*hh)) {
346 DEBUGP(DINP, "length (%u/%u) disagrees with header(%u)\n",
347 msg->len, msg->len - 3, hh->len);
348 msgb_free(msg);
349 return -EIO;
350 }
351
352 switch (bfd->priv_nr & 0xff) {
353 case UDP_TO_BTS:
354 /* injection towards BTS */
355 switch (hh->proto) {
356 case IPAC_PROTO_RSL:
357 /* FIXME: what to do about TRX > 0 */
358 other_conn = ipbc->rsl_conn[0];
359 break;
360 default:
361 DEBUGP(DINP, "Unknown protocol 0x%02x, sending to "
362 "OML FD\n", hh->proto);
363 /* fall through */
364 case IPAC_PROTO_IPACCESS:
365 case IPAC_PROTO_OML:
366 other_conn = ipbc->oml_conn;
367 break;
368 }
369 break;
370 case UDP_TO_BSC:
371 /* injection towards BSC */
372 switch (hh->proto) {
373 case IPAC_PROTO_RSL:
374 /* FIXME: what to do about TRX > 0 */
375 other_conn = ipbc->bsc_rsl_conn[0];
376 break;
377 default:
378 DEBUGP(DINP, "Unknown protocol 0x%02x, sending to "
379 "OML FD\n", hh->proto);
380 case IPAC_PROTO_IPACCESS:
381 case IPAC_PROTO_OML:
382 other_conn = ipbc->bsc_oml_conn;
383 break;
384 }
385 break;
386 default:
387 DEBUGP(DINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr);
388 break;
389 }
390
391 if (other_conn) {
392 /* enqueue the message for TX on the respective FD */
393 msgb_enqueue(&other_conn->tx_queue, msg);
394 other_conn->fd.when |= BSC_FD_WRITE;
395 } else
396 msgb_free(msg);
397
398 return 0;
399}
400
401static int handle_udp_write(struct bsc_fd *bfd)
402{
403 /* not implemented yet */
404 bfd->when &= ~BSC_FD_WRITE;
405
406 return -EIO;
407}
408
409/* callback from select.c in case one of the fd's can be read/written */
410static int udp_fd_cb(struct bsc_fd *bfd, unsigned int what)
411{
412 int rc = 0;
413
414 if (what & BSC_FD_READ)
415 rc = handle_udp_read(bfd);
416 if (what & BSC_FD_WRITE)
417 rc = handle_udp_write(bfd);
418
419 return rc;
420}
421
422
423static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct bsc_fd *bfd,
424 u_int16_t site_id, u_int16_t bts_id,
425 u_int16_t trx_id, struct tlv_parsed *tlvp,
426 struct msgb *msg)
427{
428 struct ipa_bts_conn *ipbc;
429 u_int16_t udp_port;
430 int ret = 0;
431 struct sockaddr_in sin;
432
433 memset(&sin, 0, sizeof(sin));
434 sin.sin_family = AF_INET;
435 inet_aton(bsc_ipaddr, &sin.sin_addr);
436
437 DEBUGP(DINP, "(%u/%u/%u) New BTS connection: ",
438 site_id, bts_id, trx_id);
439
440 /* OML needs to be established before RSL */
441 if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) {
442 DEBUGPC(DINP, "Not a OML connection ?!?\n");
443 return -EIO;
444 }
445
446 /* allocate new BTS connection data structure */
447 ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn);
448 if (!ipbc) {
449 ret = -ENOMEM;
450 goto err_out;
451 }
452
453 DEBUGPC(DINP, "Created BTS Conn data structure\n");
454 ipbc->ipp = ipp;
455 ipbc->unit_id.site_id = site_id;
456 ipbc->unit_id.bts_id = bts_id;
457 ipbc->oml_conn = ipc;
458 ipc->bts_conn = ipbc;
459
460 /* store the content of the ID TAGS for later reference */
461 store_idtags(ipbc, tlvp);
462 ipbc->id_resp_len = msg->len;
463 ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len);
464 memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len);
465
466 /* Create OML TCP connection towards BSC */
467 sin.sin_port = htons(3002);
468 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
469 if (!ipbc->bsc_oml_conn) {
470 ret = -EIO;
471 goto err_bsc_conn;
472 }
473
474 DEBUGP(DINP, "(%u/%u/%u) OML Connected to BSC\n",
475 site_id, bts_id, trx_id);
476
477 /* Create UDP socket for BTS packet injection */
478 udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100);
479 ret = make_sock(&ipbc->udp_bts_fd, udp_port, IPPROTO_UDP,
480 UDP_TO_BTS, udp_fd_cb, ipbc);
481 if (ret < 0)
482 goto err_udp_bts;
483 DEBUGP(DINP, "(%u/%u/%u) Created UDP socket for injection "
484 "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port);
485
486 /* Create UDP socket for BSC packet injection */
487 udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100);
488 ret = make_sock(&ipbc->udp_bsc_fd, udp_port, IPPROTO_UDP,
489 UDP_TO_BSC, udp_fd_cb, ipbc);
490 if (ret < 0)
491 goto err_udp_bsc;
492 DEBUGP(DINP, "(%u/%u/%u) Created UDP socket for injection "
493 "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port);
494 llist_add(&ipbc->list, &ipp->bts_list);
495
496 return 0;
497
498err_udp_bsc:
499 bsc_unregister_fd(&ipbc->udp_bts_fd);
500err_udp_bts:
501 bsc_unregister_fd(&ipbc->bsc_oml_conn->fd);
502 close(ipbc->bsc_oml_conn->fd.fd);
503 talloc_free(ipbc->bsc_oml_conn);
504 ipbc->bsc_oml_conn = NULL;
505err_bsc_conn:
506 talloc_free(ipbc->id_resp);
507 talloc_free(ipbc);
508#if 0
509 bsc_unregister_fd(bfd);
510 close(bfd->fd);
511 talloc_free(bfd);
512#endif
513err_out:
514 return ret;
515}
516
517static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg,
518 struct bsc_fd *bfd)
519{
520 struct tlv_parsed tlvp;
521 u_int8_t msg_type = *(msg->l2h);
522 u_int16_t site_id, bts_id, trx_id;
523 struct ipa_bts_conn *ipbc;
524 int ret = 0;
525
526 switch (msg_type) {
527 case IPAC_MSGT_PING:
528 ret = write(bfd->fd, pong, sizeof(pong));
529 if (ret < 0)
530 return ret;
531 if (ret < sizeof(pong)) {
532 DEBUGP(DINP, "short write\n");
533 return -EIO;
534 }
535 break;
536 case IPAC_MSGT_PONG:
537 DEBUGP(DMI, "PONG!\n");
538 break;
539 case IPAC_MSGT_ID_RESP:
540 DEBUGP(DMI, "ID_RESP ");
541 /* parse tags, search for Unit ID */
542 ipac_idtag_parse(&tlvp, (u_int8_t *)msg->l2h + 2,
543 msgb_l2len(msg)-2);
544 DEBUGP(DMI, "\n");
545
546 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
547 LOGP(DINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n");
548 return -EIO;
549 }
550
551 /* lookup BTS, create sign_link, ... */
552 parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
553 &site_id, &bts_id, &trx_id);
554 ipbc = find_bts_by_unitid(ipp, site_id, bts_id);
555 if (!ipbc) {
556 /* We have not found an ipbc (per-bts proxy instance)
557 * for this BTS yet. The first connection of a new BTS must
558 * be a OML connection. We allocate the associated data structures,
559 * and try to connect to the remote end */
560
561 return ipbc_alloc_connect(ipc, bfd, site_id, bts_id,
562 trx_id, &tlvp, msg);
563 /* if this fails, the caller will clean up bfd */
564 } else {
565 struct sockaddr_in sin;
566 memset(&sin, 0, sizeof(sin));
567 sin.sin_family = AF_INET;
568 inet_aton(bsc_ipaddr, &sin.sin_addr);
569
570 DEBUGP(DINP, "Identified BTS %u/%u/%u\n",
571 site_id, bts_id, trx_id);
572
573 if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) {
574 LOGP(DINP, LOGL_ERROR, "Second OML connection from "
575 "same BTS ?!?\n");
576 return 0;
577 }
578
579 if (trx_id > MAX_TRX) {
580 LOGP(DINP, LOGL_ERROR, "We don't support more "
581 "than %u TRX\n", MAX_TRX);
582 return -EINVAL;
583 }
584
585 ipc->bts_conn = ipbc;
586 /* store TRX number in higher 8 bit of the bfd private number */
587 bfd->priv_nr |= trx_id << 8;
588 ipbc->rsl_conn[trx_id] = ipc;
589
590 /* Create RSL TCP connection towards BSC */
591 sin.sin_port = htons(3003);
592 ipbc->bsc_rsl_conn[trx_id] =
593 connect_bsc(&sin, RSL_TO_BSC | (trx_id << 8), ipbc);
594 if (!ipbc->bsc_oml_conn)
595 return -EIO;
596 DEBUGP(DINP, "(%u/%u/%u) Connected RSL to BSC\n",
597 site_id, bts_id, trx_id);
598 }
599 break;
600 case IPAC_MSGT_ID_GET:
601 DEBUGP(DMI, "ID_GET\n");
602 if ((bfd->priv_nr & 0xff) != OML_TO_BSC &&
603 (bfd->priv_nr & 0xff) != RSL_TO_BSC) {
604 DEBUGP(DINP, "IDentity REQuest from BTS ?!?\n");
605 return -EIO;
606 }
607 ipbc = ipc->bts_conn;
608 if (!ipbc) {
609 DEBUGP(DINP, "ID_GET from BSC before we have ID_RESP from BTS\n");
610 return -EIO;
611 }
612 ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len);
613 break;
614 case IPAC_MSGT_ID_ACK:
615 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
616 ret = write(bfd->fd, id_ack, sizeof(id_ack));
617 break;
618 }
619 return 0;
620}
621
622struct msgb *ipaccess_read_msg(struct bsc_fd *bfd, int *error)
623{
624 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
625 struct ipaccess_head *hh;
626 int len, ret = 0;
627
628 if (!msg) {
629 *error = -ENOMEM;
630 return NULL;
631 }
632
633 /* first read our 3-byte header */
634 hh = (struct ipaccess_head *) msg->data;
635 ret = recv(bfd->fd, msg->data, 3, 0);
636 if (ret < 0) {
637 LOGP(DINP, LOGL_ERROR, "recv error: %s\n", strerror(errno));
638 msgb_free(msg);
639 *error = ret;
640 return NULL;
641 } else if (ret == 0) {
642 msgb_free(msg);
643 *error = ret;
644 return NULL;
645 }
646
647 msgb_put(msg, ret);
648
649 /* then read te length as specified in header */
650 msg->l2h = msg->data + sizeof(*hh);
651 len = ntohs(hh->len);
652 ret = recv(bfd->fd, msg->l2h, len, 0);
653 if (ret < len) {
654 LOGP(DINP, LOGL_ERROR, "short read!\n");
655 msgb_free(msg);
656 *error = -EIO;
657 return NULL;
658 }
659 msgb_put(msg, ret);
660
661 return msg;
662}
663
664static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc,
665 unsigned int priv_nr)
666{
667 struct ipa_proxy_conn *bsc_conn;
668 unsigned int trx_id = priv_nr >> 8;
669
670 switch (priv_nr & 0xff) {
671 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
672 bsc_conn = ipbc->bsc_oml_conn;
673 break;
674 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
675 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
676 break;
677 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
678 bsc_conn = ipbc->oml_conn;
679 break;
680 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
681 bsc_conn = ipbc->rsl_conn[trx_id];
682 break;
683 default:
684 bsc_conn = NULL;
685 break;
686 }
687 return bsc_conn;
688}
689
690static void reconn_tmr_cb(void *data)
691{
692 struct ipa_proxy *ipp = data;
693 struct ipa_bts_conn *ipbc;
694 struct sockaddr_in sin;
695 int i;
696
697 DEBUGP(DINP, "Running reconnect timer\n");
698
699 memset(&sin, 0, sizeof(sin));
700 sin.sin_family = AF_INET;
701 inet_aton(bsc_ipaddr, &sin.sin_addr);
702
703 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
704 /* if OML to BSC is dead, try to restore it */
705 if (ipbc->oml_conn && !ipbc->bsc_oml_conn) {
706 sin.sin_port = htons(3002);
707 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, 0);
708 LOGPC(DINP, LOGL_NOTICE, "OML Trying to reconnect\n");
709 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
710 if (!ipbc->bsc_oml_conn)
711 goto reschedule;
712 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, 0);
713 LOGPC(DINP, LOGL_NOTICE, "OML Reconnected\n");
714 }
715 /* if we (still) don't have a OML connection, skip RSL */
716 if (!ipbc->oml_conn || !ipbc->bsc_oml_conn)
717 continue;
718
719 for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) {
720 unsigned int priv_nr;
721 /* don't establish RSL links which we don't have */
722 if (!ipbc->rsl_conn[i])
723 continue;
724 if (ipbc->bsc_rsl_conn[i])
725 continue;
726 priv_nr = ipbc->rsl_conn[i]->fd.priv_nr;
727 priv_nr &= ~0xff;
728 priv_nr |= RSL_TO_BSC;
729 sin.sin_port = htons(3003);
730 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
731 LOGPC(DINP, LOGL_NOTICE, "RSL Trying to reconnect\n");
732 ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc);
733 if (!ipbc->bsc_rsl_conn)
734 goto reschedule;
735 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
736 LOGPC(DINP, LOGL_NOTICE, "RSL Reconnected\n");
737 }
738 }
739 return;
740
741reschedule:
742 bsc_schedule_timer(&ipp->reconn_timer, 5, 0);
743}
744
745static void handle_dead_socket(struct bsc_fd *bfd)
746{
747 struct ipa_proxy_conn *ipc = bfd->data; /* local conn */
748 struct ipa_proxy_conn *bsc_conn; /* remote conn */
749 struct ipa_bts_conn *ipbc = ipc->bts_conn;
750 unsigned int trx_id = bfd->priv_nr >> 8;
751 struct msgb *msg, *msg2;
752
753 bsc_unregister_fd(bfd);
754 close(bfd->fd);
755 bfd->fd = -1;
756
757 /* FIXME: clear tx_queue, remove all references, etc. */
758 llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list)
759 msgb_free(msg);
760
761 switch (bfd->priv_nr & 0xff) {
762 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
763 ipbc->oml_conn = NULL;
764 bsc_conn = ipbc->bsc_oml_conn;
765 /* close the connection to the BSC */
766 bsc_unregister_fd(&bsc_conn->fd);
767 close(bsc_conn->fd.fd);
768 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
769 msgb_free(msg);
770 talloc_free(bsc_conn);
771 ipbc->bsc_oml_conn = NULL;
772 /* FIXME: do we need to delete the entire ipbc ? */
773 break;
774 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
775 ipbc->rsl_conn[trx_id] = NULL;
776 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
777 /* close the connection to the BSC */
778 bsc_unregister_fd(&bsc_conn->fd);
779 close(bsc_conn->fd.fd);
780 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
781 msgb_free(msg);
782 talloc_free(bsc_conn);
783 ipbc->bsc_rsl_conn[trx_id] = NULL;
784 break;
785 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
786 ipbc->bsc_oml_conn = NULL;
787 bsc_conn = ipbc->oml_conn;
788 /* start reconnect timer */
789 bsc_schedule_timer(&ipp->reconn_timer, 5, 0);
790 break;
791 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
792 ipbc->bsc_rsl_conn[trx_id] = NULL;
793 bsc_conn = ipbc->rsl_conn[trx_id];
794 /* start reconnect timer */
795 bsc_schedule_timer(&ipp->reconn_timer, 5, 0);
796 break;
797 default:
798 bsc_conn = NULL;
799 break;
800 }
801
802 talloc_free(ipc);
803}
804
805static int handle_tcp_read(struct bsc_fd *bfd)
806{
807 struct ipa_proxy_conn *ipc = bfd->data;
808 struct ipa_bts_conn *ipbc = ipc->bts_conn;
809 struct ipa_proxy_conn *bsc_conn;
810 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
811 struct ipaccess_head *hh;
812 int ret = 0;
813 char *btsbsc;
814
815 if (!msg)
816 return -ENOMEM;
817
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 */
877static 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 */
918static 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 */
934static 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 }
950 DEBUGP(DINP, "accept()ed new %s link from %s\n",
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
980static 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. */
1023static 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
1063static 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 */
1075 ret = make_listen_sock(&ipp->oml_listen_fd, 3002, OML_FROM_BTS, listen_fd_cb);
1076 if (ret < 0)
1077 return ret;
1078
1079 /* Listen for RSL connections */
1080 ret = make_listen_sock(&ipp->rsl_listen_fd, 3003, RSL_FROM_BTS, listen_fd_cb);
1081
1082 return ret;
1083}
1084
1085static void signal_handler(int signal)
1086{
1087 fprintf(stdout, "signal %u received\n", signal);
1088
1089 switch (signal) {
1090 case SIGABRT:
1091 /* in case of abort, we want to obtain a talloc report
1092 * and then return to the caller, who will abort the process */
1093 case SIGUSR1:
1094 talloc_report_full(tall_bsc_ctx, stderr);
1095 break;
1096 default:
1097 break;
1098 }
1099}
1100
1101int main(int argc, char **argv)
1102{
1103 int rc;
1104
1105 listen_ipaddr = "192.168.100.11";
1106 bsc_ipaddr = "192.168.100.239";
1107
1108 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
1109
1110 debug_init();
1111 stderr_target = debug_target_create_stderr();
1112 debug_add_target(stderr_target);
1113 debug_set_all_filter(stderr_target, 1);
1114 debug_parse_category_mask(stderr_target, "DINP:DMI");
1115
1116 rc = ipaccess_proxy_setup();
1117 if (rc < 0)
1118 exit(1);
1119
1120 signal(SIGUSR1, &signal_handler);
1121 signal(SIGABRT, &signal_handler);
1122
1123 while (1) {
1124 bsc_select_main(0);
1125 }
1126}