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