blob: dd9d6b1b593409481ed597d9970ae97bc98211ad [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
Harald Weltea16ef3d2009-12-23 23:35:51 +0100248 ipbc->id_tags[i] = talloc_realloc_size(ipbc,
Harald Welte2ca7c312009-12-23 22:44:04 +0100249 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 */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100467 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100468 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 */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100591 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welte2ca7c312009-12-23 22:44:04 +0100592 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) {
Harald Welte87ed5cd2009-12-23 22:47:53 +0100706 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100707 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;
Harald Welte87ed5cd2009-12-23 22:47:53 +0100729 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welte2ca7c312009-12-23 22:44:04 +0100730 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;
Harald Weltea16ef3d2009-12-23 23:35:51 +0100810 struct msgb *msg;
Harald Welte2ca7c312009-12-23 22:44:04 +0100811 struct ipaccess_head *hh;
812 int ret = 0;
813 char *btsbsc;
814
Harald Welte2ca7c312009-12-23 22:44:04 +0100815 if ((bfd->priv_nr & 0xff) <= 2)
816 btsbsc = "BTS";
817 else
818 btsbsc = "BSC";
819
820 msg = ipaccess_read_msg(bfd, &ret);
821 if (!msg) {
822 if (ret == 0) {
823 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
824 LOGPC(DINP, LOGL_NOTICE, "%s disappeared, "
825 "dead socket\n", btsbsc);
826 handle_dead_socket(bfd);
827 }
828 return ret;
829 }
830
831 msgb_put(msg, ret);
832 logp_ipbc_uid(DMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
833 DEBUGPC(DMI, "RX<-%s: %s\n", btsbsc, hexdump(msg->data, msg->len));
834
835 hh = (struct ipaccess_head *) msg->data;
836 if (hh->proto == IPAC_PROTO_IPACCESS) {
837 ret = ipaccess_rcvmsg(ipc, msg, bfd);
838 if (ret < 0) {
839 bsc_unregister_fd(bfd);
840 close(bfd->fd);
841 bfd->fd = -1;
842 talloc_free(bfd);
843 }
844 /* we do not forward the CCM protocol through the
845 * proxy but rather terminate it ourselves */
846 msgb_free(msg);
847 return ret;
848 }
849
850 if (!ipbc) {
851 LOGP(DINP, LOGL_ERROR,
852 "received %s packet but no ipc->bts_conn?!?\n", btsbsc);
853 msgb_free(msg);
854 return -EIO;
855 }
856
857 bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr);
858 if (bsc_conn) {
859 /* enqueue packet towards BSC */
860 msgb_enqueue(&bsc_conn->tx_queue, msg);
861 /* mark respective filedescriptor as 'we want to write' */
862 bsc_conn->fd.when |= BSC_FD_WRITE;
863 } else {
864 logp_ipbc_uid(DINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8);
865 LOGPC(DINP, LOGL_INFO, "Dropping packet from %s, "
866 "since remote connection is dead\n", btsbsc);
867 msgb_free(msg);
868 }
869
870 return ret;
871}
872
873/* a TCP socket is ready to be written to */
874static int handle_tcp_write(struct bsc_fd *bfd)
875{
876 struct ipa_proxy_conn *ipc = bfd->data;
877 struct ipa_bts_conn *ipbc = ipc->bts_conn;
878 struct llist_head *lh;
879 struct msgb *msg;
880 char *btsbsc;
881 int ret;
882
883 if ((bfd->priv_nr & 0xff) <= 2)
884 btsbsc = "BTS";
885 else
886 btsbsc = "BSC";
887
888
889 /* get the next msg for this timeslot */
890 if (llist_empty(&ipc->tx_queue)) {
891 bfd->when &= ~BSC_FD_WRITE;
892 return 0;
893 }
894 lh = ipc->tx_queue.next;
895 llist_del(lh);
896 msg = llist_entry(lh, struct msgb, list);
897
898 logp_ipbc_uid(DMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
899 DEBUGPC(DMI, "TX %04x: %s\n", bfd->priv_nr,
900 hexdump(msg->data, msg->len));
901
902 ret = send(bfd->fd, msg->data, msg->len, 0);
903 msgb_free(msg);
904
905 if (ret == 0) {
906 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
907 LOGP(DINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc);
908 handle_dead_socket(bfd);
909 }
910
911 return ret;
912}
913
914/* callback from select.c in case one of the fd's can be read/written */
915static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
916{
917 int rc = 0;
918
919 if (what & BSC_FD_READ) {
920 rc = handle_tcp_read(bfd);
921 if (rc < 0)
922 return rc;
923 }
924 if (what & BSC_FD_WRITE)
925 rc = handle_tcp_write(bfd);
926
927 return rc;
928}
929
930/* callback of the listening filedescriptor */
931static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
932{
933 int ret;
934 struct ipa_proxy_conn *ipc;
935 struct bsc_fd *bfd;
936 struct sockaddr_in sa;
937 socklen_t sa_len = sizeof(sa);
938
939 if (!(what & BSC_FD_READ))
940 return 0;
941
942 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
943 if (ret < 0) {
944 perror("accept");
945 return ret;
946 }
947 DEBUGP(DINP, "accept()ed new %s link from %s\n",
948 (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL",
949 inet_ntoa(sa.sin_addr));
950
951 ipc = alloc_conn();
952 if (!ipc) {
953 close(ret);
954 return -ENOMEM;
955 }
956
957 bfd = &ipc->fd;
958 bfd->fd = ret;
959 bfd->data = ipc;
960 bfd->priv_nr = listen_bfd->priv_nr;
961 bfd->cb = ipaccess_fd_cb;
962 bfd->when = BSC_FD_READ;
963 ret = bsc_register_fd(bfd);
964 if (ret < 0) {
965 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
966 close(bfd->fd);
967 talloc_free(ipc);
968 return ret;
969 }
970
971 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
972 ret = write(bfd->fd, id_req, sizeof(id_req));
973
974 return 0;
975}
976
977static int make_listen_sock(struct bsc_fd *bfd, u_int16_t port, int priv_nr,
978 int (*cb)(struct bsc_fd *fd, unsigned int what))
979{
980 struct sockaddr_in addr;
981 int ret, on = 1;
982
983 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
984 bfd->cb = cb;
985 bfd->when = BSC_FD_READ;
986 bfd->priv_nr = priv_nr;
987
988 memset(&addr, 0, sizeof(addr));
989 addr.sin_family = AF_INET;
990 addr.sin_port = htons(port);
991 if (!listen_ipaddr)
992 addr.sin_addr.s_addr = INADDR_ANY;
993 else
994 inet_aton(listen_ipaddr, &addr.sin_addr);
995
996 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
997
998 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
999 if (ret < 0) {
1000 LOGP(DINP, LOGL_ERROR, "could not bind listen socket %s\n",
1001 strerror(errno));
1002 return -EIO;
1003 }
1004
1005 ret = listen(bfd->fd, 1);
1006 if (ret < 0) {
1007 perror("listen");
1008 return ret;
1009 }
1010
1011 ret = bsc_register_fd(bfd);
1012 if (ret < 0) {
1013 perror("register_listen_fd");
1014 return ret;
1015 }
1016 return 0;
1017}
1018
1019/* Actively connect to a BSC. */
1020static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data)
1021{
1022 struct ipa_proxy_conn *ipc;
1023 struct bsc_fd *bfd;
1024 int ret, on = 1;
1025
1026 ipc = alloc_conn();
1027 if (!ipc)
1028 return NULL;
1029
1030 ipc->bts_conn = data;
1031
1032 bfd = &ipc->fd;
1033 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1034 bfd->cb = ipaccess_fd_cb;
1035 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
1036 bfd->data = ipc;
1037 bfd->priv_nr = priv_nr;
1038
1039 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1040
1041 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
1042 if (ret < 0) {
1043 LOGP(DINP, LOGL_ERROR, "could not connect socket\n");
1044 close(bfd->fd);
1045 talloc_free(ipc);
1046 return NULL;
1047 }
1048
1049 /* pre-fill tx_queue with identity request */
1050 ret = bsc_register_fd(bfd);
1051 if (ret < 0) {
1052 close(bfd->fd);
1053 talloc_free(ipc);
1054 return NULL;
1055 }
1056
1057 return ipc;
1058}
1059
1060static int ipaccess_proxy_setup(void)
1061{
1062 int ret;
1063
1064 ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy);
1065 if (!ipp)
1066 return -ENOMEM;
1067 INIT_LLIST_HEAD(&ipp->bts_list);
1068 ipp->reconn_timer.cb = reconn_tmr_cb;
1069 ipp->reconn_timer.data = ipp;
1070
1071 /* Listen for OML connections */
Harald Welte87ed5cd2009-12-23 22:47:53 +01001072 ret = make_listen_sock(&ipp->oml_listen_fd, IPA_TCP_PORT_OML,
1073 OML_FROM_BTS, listen_fd_cb);
Harald Welte2ca7c312009-12-23 22:44:04 +01001074 if (ret < 0)
1075 return ret;
1076
1077 /* Listen for RSL connections */
Harald Welte87ed5cd2009-12-23 22:47:53 +01001078 ret = make_listen_sock(&ipp->rsl_listen_fd, IPA_TCP_PORT_RSL,
1079 RSL_FROM_BTS, listen_fd_cb);
Harald Welte2ca7c312009-12-23 22:44:04 +01001080
1081 return ret;
1082}
1083
1084static void signal_handler(int signal)
1085{
1086 fprintf(stdout, "signal %u received\n", signal);
1087
1088 switch (signal) {
1089 case SIGABRT:
1090 /* in case of abort, we want to obtain a talloc report
1091 * and then return to the caller, who will abort the process */
1092 case SIGUSR1:
1093 talloc_report_full(tall_bsc_ctx, stderr);
1094 break;
1095 default:
1096 break;
1097 }
1098}
1099
1100int main(int argc, char **argv)
1101{
1102 int rc;
1103
1104 listen_ipaddr = "192.168.100.11";
1105 bsc_ipaddr = "192.168.100.239";
1106
1107 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
1108
1109 debug_init();
1110 stderr_target = debug_target_create_stderr();
1111 debug_add_target(stderr_target);
1112 debug_set_all_filter(stderr_target, 1);
1113 debug_parse_category_mask(stderr_target, "DINP:DMI");
1114
1115 rc = ipaccess_proxy_setup();
1116 if (rc < 0)
1117 exit(1);
1118
1119 signal(SIGUSR1, &signal_handler);
1120 signal(SIGABRT, &signal_handler);
1121
1122 while (1) {
1123 bsc_select_main(0);
1124 }
1125}