blob: daa909454d8c39d0209adfd9f9db1035f2ebdb1e [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>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <sys/ioctl.h>
35#include <arpa/inet.h>
36#include <netinet/in.h>
37
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +080038#define _GNU_SOURCE
39#include <getopt.h>
40
Harald Welte2ca7c312009-12-23 22:44:04 +010041#include <openbsc/gsm_data.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010042#include <osmocom/core/select.h>
43#include <osmocom/gsm/tlv.h>
44#include <osmocom/core/msgb.h>
Harald Welte2ca7c312009-12-23 22:44:04 +010045#include <openbsc/debug.h>
46#include <openbsc/ipaccess.h>
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +020047#include <openbsc/socket.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010048#include <osmocom/core/talloc.h>
Harald Welte2ca7c312009-12-23 22:44:04 +010049
Harald Weltedc5062b2010-03-26 21:28:59 +080050static struct log_target *stderr_target;
Harald Welte2ca7c312009-12-23 22:44:04 +010051
52/* one instance of an ip.access protocol proxy */
53struct ipa_proxy {
54 /* socket where we listen for incoming OML from BTS */
55 struct bsc_fd oml_listen_fd;
56 /* socket where we listen for incoming RSL from BTS */
57 struct bsc_fd rsl_listen_fd;
58 /* list of BTS's (struct ipa_bts_conn */
59 struct llist_head bts_list;
60 /* the BSC reconnect timer */
61 struct timer_list reconn_timer;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +080062 /* global GPRS NS data */
63 struct in_addr gprs_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +080064 struct in_addr listen_addr;
Harald Welte2ca7c312009-12-23 22:44:04 +010065};
66
67/* global pointer to the proxy structure */
68static struct ipa_proxy *ipp;
69
70struct ipa_proxy_conn {
71 struct bsc_fd fd;
72 struct llist_head tx_queue;
73 struct ipa_bts_conn *bts_conn;
74};
Harald Welte2ca7c312009-12-23 22:44:04 +010075#define MAX_TRX 4
76
77/* represents a particular BTS in our proxy */
78struct ipa_bts_conn {
79 /* list of BTS's (ipa_proxy->bts_list) */
80 struct llist_head list;
81 /* back pointer to the proxy which we belong to */
82 struct ipa_proxy *ipp;
83 /* the unit ID as determined by CCM */
84 struct {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 uint16_t site_id;
86 uint16_t bts_id;
Harald Welte2ca7c312009-12-23 22:44:04 +010087 } unit_id;
88
89 /* incoming connections from BTS */
90 struct ipa_proxy_conn *oml_conn;
91 struct ipa_proxy_conn *rsl_conn[MAX_TRX];
92
93 /* outgoing connections to BSC */
94 struct ipa_proxy_conn *bsc_oml_conn;
95 struct ipa_proxy_conn *bsc_rsl_conn[MAX_TRX];
96
97 /* UDP sockets for BTS and BSC injection */
98 struct bsc_fd udp_bts_fd;
99 struct bsc_fd udp_bsc_fd;
100
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800101 /* NS data */
102 struct in_addr bts_addr;
103 struct bsc_fd gprs_ns_fd;
104 int gprs_local_port;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800105 uint16_t gprs_orig_port;
106 uint32_t gprs_orig_ip;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800107
Harald Welte2ca7c312009-12-23 22:44:04 +0100108 char *id_tags[0xff];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200109 uint8_t *id_resp;
Harald Welte2ca7c312009-12-23 22:44:04 +0100110 unsigned int id_resp_len;
111};
112
113enum ipp_fd_type {
114 OML_FROM_BTS = 1,
115 RSL_FROM_BTS = 2,
116 OML_TO_BSC = 3,
117 RSL_TO_BSC = 4,
118 UDP_TO_BTS = 5,
119 UDP_TO_BSC = 6,
120};
121
122/* some of the code against we link from OpenBSC needs this */
123void *tall_bsc_ctx;
124
125static char *listen_ipaddr;
126static char *bsc_ipaddr;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800127static char *gprs_ns_ipaddr;
Harald Welte2ca7c312009-12-23 22:44:04 +0100128
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800129static int gprs_ns_cb(struct bsc_fd *bfd, unsigned int what);
130
Holger Hans Peter Freyther3dac8812010-06-09 14:39:22 +0800131#define PROXY_ALLOC_SIZE 1200
Harald Welte2ca7c312009-12-23 22:44:04 +0100132
Harald Welte2ca7c312009-12-23 22:44:04 +0100133static struct ipa_bts_conn *find_bts_by_unitid(struct ipa_proxy *ipp,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200134 uint16_t site_id,
135 uint16_t bts_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100136{
137 struct ipa_bts_conn *ipbc;
138
139 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
140 if (ipbc->unit_id.site_id == site_id &&
141 ipbc->unit_id.bts_id == bts_id)
142 return ipbc;
143 }
144
145 return NULL;
146}
147
148struct ipa_proxy_conn *alloc_conn(void)
149{
150 struct ipa_proxy_conn *ipc;
151
152 ipc = talloc_zero(tall_bsc_ctx, struct ipa_proxy_conn);
153 if (!ipc)
154 return NULL;
155
156 INIT_LLIST_HEAD(&ipc->tx_queue);
157
158 return ipc;
159}
160
161static int store_idtags(struct ipa_bts_conn *ipbc, struct tlv_parsed *tlvp)
162{
163 unsigned int i, len;
164
165 for (i = 0; i <= 0xff; i++) {
166 if (!TLVP_PRESENT(tlvp, i))
167 continue;
168
169 len = TLVP_LEN(tlvp, i);
170#if 0
171 if (!ipbc->id_tags[i])
172 ipbc->id_tags[i] = talloc_size(tall_bsc_ctx, len);
173 else
174#endif
Harald Weltea16ef3d2009-12-23 23:35:51 +0100175 ipbc->id_tags[i] = talloc_realloc_size(ipbc,
Harald Welte2ca7c312009-12-23 22:44:04 +0100176 ipbc->id_tags[i], len);
177 if (!ipbc->id_tags[i])
178 return -ENOMEM;
179
180 memset(ipbc->id_tags[i], 0, len);
181 //memcpy(ipbc->id_tags[i], TLVP_VAL(tlvp, i), len);
182 }
183 return 0;
184}
185
186
187static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data);
188
189#define logp_ipbc_uid(ss, lvl, ipbc, trx_id) _logp_ipbc_uid(ss, lvl, __FILE__, __LINE__, ipbc, trx_id)
190
191static void _logp_ipbc_uid(unsigned int ss, unsigned int lvl, char *file, int line,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200192 struct ipa_bts_conn *ipbc, uint8_t trx_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100193{
194 if (ipbc)
Harald Weltedc5062b2010-03-26 21:28:59 +0800195 logp2(ss, lvl, file, line, 0, "(%u/%u/%u) ", ipbc->unit_id.site_id,
Harald Welte2ca7c312009-12-23 22:44:04 +0100196 ipbc->unit_id.bts_id, trx_id);
197 else
Harald Weltedc5062b2010-03-26 21:28:59 +0800198 logp2(ss, lvl, file, line, 0, "unknown ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100199}
200
Harald Welte2ca7c312009-12-23 22:44:04 +0100201static int handle_udp_read(struct bsc_fd *bfd)
202{
203 struct ipa_bts_conn *ipbc = bfd->data;
204 struct ipa_proxy_conn *other_conn = NULL;
205 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP UDP");
206 struct ipaccess_head *hh;
207 int ret;
208
209 /* with UDP sockets, we cannot read partial packets but have to read
210 * all of it in one go */
211 hh = (struct ipaccess_head *) msg->data;
212 ret = recv(bfd->fd, msg->data, msg->data_len, 0);
213 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100214 if (errno != EAGAIN)
215 LOGP(DINP, LOGL_ERROR, "recv error %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100216 msgb_free(msg);
217 return ret;
218 }
219 if (ret == 0) {
220 DEBUGP(DINP, "UDP peer disappeared, dead socket\n");
221 bsc_unregister_fd(bfd);
222 close(bfd->fd);
223 bfd->fd = -1;
224 msgb_free(msg);
225 return -EIO;
226 }
227 if (ret < sizeof(*hh)) {
228 DEBUGP(DINP, "could not even read header!?!\n");
229 msgb_free(msg);
230 return -EIO;
231 }
232 msgb_put(msg, ret);
233 msg->l2h = msg->data + sizeof(*hh);
234 DEBUGP(DMI, "UDP RX: %s\n", hexdump(msg->data, msg->len));
235
236 if (hh->len != msg->len - sizeof(*hh)) {
237 DEBUGP(DINP, "length (%u/%u) disagrees with header(%u)\n",
238 msg->len, msg->len - 3, hh->len);
239 msgb_free(msg);
240 return -EIO;
241 }
242
243 switch (bfd->priv_nr & 0xff) {
244 case UDP_TO_BTS:
245 /* injection towards BTS */
246 switch (hh->proto) {
247 case IPAC_PROTO_RSL:
248 /* FIXME: what to do about TRX > 0 */
249 other_conn = ipbc->rsl_conn[0];
250 break;
251 default:
252 DEBUGP(DINP, "Unknown protocol 0x%02x, sending to "
253 "OML FD\n", hh->proto);
254 /* fall through */
255 case IPAC_PROTO_IPACCESS:
256 case IPAC_PROTO_OML:
257 other_conn = ipbc->oml_conn;
258 break;
259 }
260 break;
261 case UDP_TO_BSC:
262 /* injection towards BSC */
263 switch (hh->proto) {
264 case IPAC_PROTO_RSL:
265 /* FIXME: what to do about TRX > 0 */
266 other_conn = ipbc->bsc_rsl_conn[0];
267 break;
268 default:
269 DEBUGP(DINP, "Unknown protocol 0x%02x, sending to "
270 "OML FD\n", hh->proto);
271 case IPAC_PROTO_IPACCESS:
272 case IPAC_PROTO_OML:
273 other_conn = ipbc->bsc_oml_conn;
274 break;
275 }
276 break;
277 default:
278 DEBUGP(DINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr);
279 break;
280 }
281
282 if (other_conn) {
283 /* enqueue the message for TX on the respective FD */
284 msgb_enqueue(&other_conn->tx_queue, msg);
285 other_conn->fd.when |= BSC_FD_WRITE;
286 } else
287 msgb_free(msg);
288
289 return 0;
290}
291
292static int handle_udp_write(struct bsc_fd *bfd)
293{
294 /* not implemented yet */
295 bfd->when &= ~BSC_FD_WRITE;
296
297 return -EIO;
298}
299
300/* callback from select.c in case one of the fd's can be read/written */
301static int udp_fd_cb(struct bsc_fd *bfd, unsigned int what)
302{
303 int rc = 0;
304
305 if (what & BSC_FD_READ)
306 rc = handle_udp_read(bfd);
307 if (what & BSC_FD_WRITE)
308 rc = handle_udp_write(bfd);
309
310 return rc;
311}
312
313
314static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct bsc_fd *bfd,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200315 uint16_t site_id, uint16_t bts_id,
316 uint16_t trx_id, struct tlv_parsed *tlvp,
Harald Welte2ca7c312009-12-23 22:44:04 +0100317 struct msgb *msg)
318{
319 struct ipa_bts_conn *ipbc;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200320 uint16_t udp_port;
Harald Welte2ca7c312009-12-23 22:44:04 +0100321 int ret = 0;
322 struct sockaddr_in sin;
323
324 memset(&sin, 0, sizeof(sin));
325 sin.sin_family = AF_INET;
326 inet_aton(bsc_ipaddr, &sin.sin_addr);
327
328 DEBUGP(DINP, "(%u/%u/%u) New BTS connection: ",
329 site_id, bts_id, trx_id);
330
331 /* OML needs to be established before RSL */
332 if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) {
333 DEBUGPC(DINP, "Not a OML connection ?!?\n");
334 return -EIO;
335 }
336
337 /* allocate new BTS connection data structure */
338 ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn);
339 if (!ipbc) {
340 ret = -ENOMEM;
341 goto err_out;
342 }
343
344 DEBUGPC(DINP, "Created BTS Conn data structure\n");
345 ipbc->ipp = ipp;
346 ipbc->unit_id.site_id = site_id;
347 ipbc->unit_id.bts_id = bts_id;
348 ipbc->oml_conn = ipc;
349 ipc->bts_conn = ipbc;
350
351 /* store the content of the ID TAGS for later reference */
352 store_idtags(ipbc, tlvp);
353 ipbc->id_resp_len = msg->len;
354 ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len);
355 memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len);
356
357 /* Create OML TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100358 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100359 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
360 if (!ipbc->bsc_oml_conn) {
361 ret = -EIO;
362 goto err_bsc_conn;
363 }
364
365 DEBUGP(DINP, "(%u/%u/%u) OML Connected to BSC\n",
366 site_id, bts_id, trx_id);
367
368 /* Create UDP socket for BTS packet injection */
369 udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200370 ret = make_sock(&ipbc->udp_bts_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100371 UDP_TO_BTS, udp_fd_cb, ipbc);
372 if (ret < 0)
373 goto err_udp_bts;
374 DEBUGP(DINP, "(%u/%u/%u) Created UDP socket for injection "
375 "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port);
376
377 /* Create UDP socket for BSC packet injection */
378 udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200379 ret = make_sock(&ipbc->udp_bsc_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100380 UDP_TO_BSC, udp_fd_cb, ipbc);
381 if (ret < 0)
382 goto err_udp_bsc;
383 DEBUGP(DINP, "(%u/%u/%u) Created UDP socket for injection "
384 "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800385
386
387 /* GPRS NS related code */
388 if (gprs_ns_ipaddr) {
389 struct sockaddr_in sock;
390 socklen_t len = sizeof(sock);
Pablo Neira Ayuso7e737002011-04-11 16:33:17 +0200391 struct in_addr addr;
392 uint32_t ip;
393
394 inet_aton(listen_ipaddr, &addr);
395 ip = ntohl(addr.s_addr); /* make_sock() needs host byte order */
396 ret = make_sock(&ipbc->gprs_ns_fd, IPPROTO_UDP, ip, 0, 0,
397 gprs_ns_cb, ipbc);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800398 if (ret < 0) {
399 LOGP(DINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
400 goto err_udp_bsc;
401 }
402
403 ret = getsockname(ipbc->gprs_ns_fd.fd, (struct sockaddr* ) &sock, &len);
404 ipbc->gprs_local_port = ntohs(sock.sin_port);
405 LOGP(DINP, LOGL_NOTICE,
406 "Created GPRS NS Socket. Listening on: %s:%d\n",
407 inet_ntoa(sock.sin_addr), ipbc->gprs_local_port);
408
409 ret = getpeername(bfd->fd, (struct sockaddr* ) &sock, &len);
410 ipbc->bts_addr = sock.sin_addr;
411 }
412
Harald Welte2ca7c312009-12-23 22:44:04 +0100413 llist_add(&ipbc->list, &ipp->bts_list);
414
415 return 0;
416
417err_udp_bsc:
418 bsc_unregister_fd(&ipbc->udp_bts_fd);
419err_udp_bts:
420 bsc_unregister_fd(&ipbc->bsc_oml_conn->fd);
421 close(ipbc->bsc_oml_conn->fd.fd);
422 talloc_free(ipbc->bsc_oml_conn);
423 ipbc->bsc_oml_conn = NULL;
424err_bsc_conn:
425 talloc_free(ipbc->id_resp);
426 talloc_free(ipbc);
427#if 0
428 bsc_unregister_fd(bfd);
429 close(bfd->fd);
430 talloc_free(bfd);
431#endif
432err_out:
433 return ret;
434}
435
436static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg,
437 struct bsc_fd *bfd)
438{
439 struct tlv_parsed tlvp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200440 uint8_t msg_type = *(msg->l2h);
441 uint16_t site_id, bts_id, trx_id;
Harald Welte2ca7c312009-12-23 22:44:04 +0100442 struct ipa_bts_conn *ipbc;
443 int ret = 0;
444
445 switch (msg_type) {
446 case IPAC_MSGT_PING:
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200447 ret = ipaccess_send_pong(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100448 break;
449 case IPAC_MSGT_PONG:
450 DEBUGP(DMI, "PONG!\n");
451 break;
452 case IPAC_MSGT_ID_RESP:
453 DEBUGP(DMI, "ID_RESP ");
454 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200455 ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
Pablo Neira Ayuso625295b2011-04-07 14:15:16 +0200456 msgb_l2len(msg)-2);
Harald Welte2ca7c312009-12-23 22:44:04 +0100457 DEBUGP(DMI, "\n");
458
459 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
460 LOGP(DINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n");
461 return -EIO;
462 }
463
464 /* lookup BTS, create sign_link, ... */
Holger Hans Peter Freyther82ae7162010-03-30 15:20:46 +0200465 site_id = bts_id = trx_id = 0;
Pablo Neira Ayusoc281b4e2011-04-07 14:15:20 +0200466 ipaccess_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
467 &site_id, &bts_id, &trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100468 ipbc = find_bts_by_unitid(ipp, site_id, bts_id);
469 if (!ipbc) {
470 /* We have not found an ipbc (per-bts proxy instance)
471 * for this BTS yet. The first connection of a new BTS must
472 * be a OML connection. We allocate the associated data structures,
473 * and try to connect to the remote end */
474
475 return ipbc_alloc_connect(ipc, bfd, site_id, bts_id,
476 trx_id, &tlvp, msg);
477 /* if this fails, the caller will clean up bfd */
478 } else {
479 struct sockaddr_in sin;
480 memset(&sin, 0, sizeof(sin));
481 sin.sin_family = AF_INET;
482 inet_aton(bsc_ipaddr, &sin.sin_addr);
483
484 DEBUGP(DINP, "Identified BTS %u/%u/%u\n",
485 site_id, bts_id, trx_id);
486
487 if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) {
488 LOGP(DINP, LOGL_ERROR, "Second OML connection from "
489 "same BTS ?!?\n");
490 return 0;
491 }
492
493 if (trx_id > MAX_TRX) {
494 LOGP(DINP, LOGL_ERROR, "We don't support more "
495 "than %u TRX\n", MAX_TRX);
496 return -EINVAL;
497 }
498
499 ipc->bts_conn = ipbc;
500 /* store TRX number in higher 8 bit of the bfd private number */
501 bfd->priv_nr |= trx_id << 8;
502 ipbc->rsl_conn[trx_id] = ipc;
503
504 /* Create RSL TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100505 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welte2ca7c312009-12-23 22:44:04 +0100506 ipbc->bsc_rsl_conn[trx_id] =
507 connect_bsc(&sin, RSL_TO_BSC | (trx_id << 8), ipbc);
508 if (!ipbc->bsc_oml_conn)
509 return -EIO;
510 DEBUGP(DINP, "(%u/%u/%u) Connected RSL to BSC\n",
511 site_id, bts_id, trx_id);
512 }
513 break;
514 case IPAC_MSGT_ID_GET:
515 DEBUGP(DMI, "ID_GET\n");
516 if ((bfd->priv_nr & 0xff) != OML_TO_BSC &&
517 (bfd->priv_nr & 0xff) != RSL_TO_BSC) {
518 DEBUGP(DINP, "IDentity REQuest from BTS ?!?\n");
519 return -EIO;
520 }
521 ipbc = ipc->bts_conn;
522 if (!ipbc) {
523 DEBUGP(DINP, "ID_GET from BSC before we have ID_RESP from BTS\n");
524 return -EIO;
525 }
526 ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len);
527 break;
528 case IPAC_MSGT_ID_ACK:
529 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200530 ret = ipaccess_send_id_ack(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100531 break;
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800532 default:
533 LOGP(DMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type);
534 return 1;
535 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100536 }
537 return 0;
538}
539
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200540struct msgb *ipaccess_proxy_read_msg(struct bsc_fd *bfd, int *error)
Harald Welte2ca7c312009-12-23 22:44:04 +0100541{
542 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
543 struct ipaccess_head *hh;
544 int len, ret = 0;
545
546 if (!msg) {
547 *error = -ENOMEM;
548 return NULL;
549 }
550
551 /* first read our 3-byte header */
552 hh = (struct ipaccess_head *) msg->data;
553 ret = recv(bfd->fd, msg->data, 3, 0);
554 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100555 if (errno != EAGAIN)
Harald Welteda956932009-12-24 12:49:43 +0100556 LOGP(DINP, LOGL_ERROR, "recv error: %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100557 msgb_free(msg);
558 *error = ret;
559 return NULL;
560 } else if (ret == 0) {
561 msgb_free(msg);
562 *error = ret;
563 return NULL;
564 }
565
566 msgb_put(msg, ret);
567
568 /* then read te length as specified in header */
569 msg->l2h = msg->data + sizeof(*hh);
570 len = ntohs(hh->len);
571 ret = recv(bfd->fd, msg->l2h, len, 0);
572 if (ret < len) {
573 LOGP(DINP, LOGL_ERROR, "short read!\n");
574 msgb_free(msg);
575 *error = -EIO;
576 return NULL;
577 }
578 msgb_put(msg, ret);
579
580 return msg;
581}
582
583static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc,
584 unsigned int priv_nr)
585{
586 struct ipa_proxy_conn *bsc_conn;
587 unsigned int trx_id = priv_nr >> 8;
588
589 switch (priv_nr & 0xff) {
590 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
591 bsc_conn = ipbc->bsc_oml_conn;
592 break;
593 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
594 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
595 break;
596 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
597 bsc_conn = ipbc->oml_conn;
598 break;
599 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
600 bsc_conn = ipbc->rsl_conn[trx_id];
601 break;
602 default:
603 bsc_conn = NULL;
604 break;
605 }
606 return bsc_conn;
607}
608
609static void reconn_tmr_cb(void *data)
610{
611 struct ipa_proxy *ipp = data;
612 struct ipa_bts_conn *ipbc;
613 struct sockaddr_in sin;
614 int i;
615
616 DEBUGP(DINP, "Running reconnect timer\n");
617
618 memset(&sin, 0, sizeof(sin));
619 sin.sin_family = AF_INET;
620 inet_aton(bsc_ipaddr, &sin.sin_addr);
621
622 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
623 /* if OML to BSC is dead, try to restore it */
624 if (ipbc->oml_conn && !ipbc->bsc_oml_conn) {
Harald Welte87ed5cd2009-12-23 22:47:53 +0100625 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100626 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, 0);
627 LOGPC(DINP, LOGL_NOTICE, "OML Trying to reconnect\n");
628 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
629 if (!ipbc->bsc_oml_conn)
630 goto reschedule;
631 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, 0);
632 LOGPC(DINP, LOGL_NOTICE, "OML Reconnected\n");
633 }
634 /* if we (still) don't have a OML connection, skip RSL */
635 if (!ipbc->oml_conn || !ipbc->bsc_oml_conn)
636 continue;
637
638 for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) {
639 unsigned int priv_nr;
640 /* don't establish RSL links which we don't have */
641 if (!ipbc->rsl_conn[i])
642 continue;
643 if (ipbc->bsc_rsl_conn[i])
644 continue;
645 priv_nr = ipbc->rsl_conn[i]->fd.priv_nr;
646 priv_nr &= ~0xff;
647 priv_nr |= RSL_TO_BSC;
Harald Welte87ed5cd2009-12-23 22:47:53 +0100648 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welte2ca7c312009-12-23 22:44:04 +0100649 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
650 LOGPC(DINP, LOGL_NOTICE, "RSL Trying to reconnect\n");
651 ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc);
652 if (!ipbc->bsc_rsl_conn)
653 goto reschedule;
654 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
655 LOGPC(DINP, LOGL_NOTICE, "RSL Reconnected\n");
656 }
657 }
658 return;
659
660reschedule:
661 bsc_schedule_timer(&ipp->reconn_timer, 5, 0);
662}
663
664static void handle_dead_socket(struct bsc_fd *bfd)
665{
666 struct ipa_proxy_conn *ipc = bfd->data; /* local conn */
667 struct ipa_proxy_conn *bsc_conn; /* remote conn */
668 struct ipa_bts_conn *ipbc = ipc->bts_conn;
669 unsigned int trx_id = bfd->priv_nr >> 8;
670 struct msgb *msg, *msg2;
671
672 bsc_unregister_fd(bfd);
673 close(bfd->fd);
674 bfd->fd = -1;
675
676 /* FIXME: clear tx_queue, remove all references, etc. */
677 llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list)
678 msgb_free(msg);
679
680 switch (bfd->priv_nr & 0xff) {
681 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
Pablo Neira Ayuso3c409c22011-03-27 21:31:48 +0200682 /* The BTS started a connection with us but we got no
683 * IPAC_MSGT_ID_RESP message yet, in that scenario we did not
684 * allocate the ipa_bts_conn structure. */
685 if (ipbc == NULL)
686 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100687 ipbc->oml_conn = NULL;
688 bsc_conn = ipbc->bsc_oml_conn;
689 /* close the connection to the BSC */
690 bsc_unregister_fd(&bsc_conn->fd);
691 close(bsc_conn->fd.fd);
692 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
693 msgb_free(msg);
694 talloc_free(bsc_conn);
695 ipbc->bsc_oml_conn = NULL;
696 /* FIXME: do we need to delete the entire ipbc ? */
697 break;
698 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
699 ipbc->rsl_conn[trx_id] = NULL;
700 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
701 /* close the connection to the BSC */
702 bsc_unregister_fd(&bsc_conn->fd);
703 close(bsc_conn->fd.fd);
704 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
705 msgb_free(msg);
706 talloc_free(bsc_conn);
707 ipbc->bsc_rsl_conn[trx_id] = NULL;
708 break;
709 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
710 ipbc->bsc_oml_conn = NULL;
711 bsc_conn = ipbc->oml_conn;
712 /* start reconnect timer */
713 bsc_schedule_timer(&ipp->reconn_timer, 5, 0);
714 break;
715 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
716 ipbc->bsc_rsl_conn[trx_id] = NULL;
717 bsc_conn = ipbc->rsl_conn[trx_id];
718 /* start reconnect timer */
719 bsc_schedule_timer(&ipp->reconn_timer, 5, 0);
720 break;
721 default:
722 bsc_conn = NULL;
723 break;
724 }
725
726 talloc_free(ipc);
727}
728
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800729static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg)
730{
731 uint8_t *nsvci;
732
733 if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC)
734 return;
735
736 if (msgb_l2len(msg) != 39)
737 return;
738
739 /*
740 * Check if this is a IPA Set Attribute or IPA Set Attribute ACK
741 * and if the FOM Class is GPRS NSVC0 and then we will patch it.
742 *
743 * The patch assumes the message looks like the one from the trace
744 * but we only match messages with a specific size anyway... So
745 * this hack should work just fine.
746 */
747
748 if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
749 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
750 msg->l2h[18] == 0xf5 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800751 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200752 ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8);
753 ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10);
754 *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port);
755 *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800756 } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
757 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
758 msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800759 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200760 *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port;
761 *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800762 }
763}
764
Harald Welte2ca7c312009-12-23 22:44:04 +0100765static int handle_tcp_read(struct bsc_fd *bfd)
766{
767 struct ipa_proxy_conn *ipc = bfd->data;
768 struct ipa_bts_conn *ipbc = ipc->bts_conn;
769 struct ipa_proxy_conn *bsc_conn;
Harald Weltea16ef3d2009-12-23 23:35:51 +0100770 struct msgb *msg;
Harald Welte2ca7c312009-12-23 22:44:04 +0100771 struct ipaccess_head *hh;
772 int ret = 0;
773 char *btsbsc;
774
Harald Welte2ca7c312009-12-23 22:44:04 +0100775 if ((bfd->priv_nr & 0xff) <= 2)
776 btsbsc = "BTS";
777 else
778 btsbsc = "BSC";
779
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200780 msg = ipaccess_proxy_read_msg(bfd, &ret);
Harald Welte2ca7c312009-12-23 22:44:04 +0100781 if (!msg) {
782 if (ret == 0) {
783 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
784 LOGPC(DINP, LOGL_NOTICE, "%s disappeared, "
785 "dead socket\n", btsbsc);
786 handle_dead_socket(bfd);
787 }
788 return ret;
789 }
790
791 msgb_put(msg, ret);
792 logp_ipbc_uid(DMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
793 DEBUGPC(DMI, "RX<-%s: %s\n", btsbsc, hexdump(msg->data, msg->len));
794
795 hh = (struct ipaccess_head *) msg->data;
796 if (hh->proto == IPAC_PROTO_IPACCESS) {
797 ret = ipaccess_rcvmsg(ipc, msg, bfd);
798 if (ret < 0) {
799 bsc_unregister_fd(bfd);
800 close(bfd->fd);
801 bfd->fd = -1;
802 talloc_free(bfd);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800803 msgb_free(msg);
804 return ret;
805 } else if (ret == 0) {
806 /* we do not forward parts of the CCM protocol
807 * through the proxy but rather terminate it ourselves. */
808 msgb_free(msg);
809 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100810 }
Harald Welte2ca7c312009-12-23 22:44:04 +0100811 }
812
813 if (!ipbc) {
814 LOGP(DINP, LOGL_ERROR,
815 "received %s packet but no ipc->bts_conn?!?\n", btsbsc);
816 msgb_free(msg);
817 return -EIO;
818 }
819
820 bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr);
821 if (bsc_conn) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800822 if (gprs_ns_ipaddr)
823 patch_gprs_msg(ipbc, bfd->priv_nr, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100824 /* enqueue packet towards BSC */
825 msgb_enqueue(&bsc_conn->tx_queue, msg);
826 /* mark respective filedescriptor as 'we want to write' */
827 bsc_conn->fd.when |= BSC_FD_WRITE;
828 } else {
829 logp_ipbc_uid(DINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8);
830 LOGPC(DINP, LOGL_INFO, "Dropping packet from %s, "
831 "since remote connection is dead\n", btsbsc);
832 msgb_free(msg);
833 }
834
835 return ret;
836}
837
838/* a TCP socket is ready to be written to */
839static int handle_tcp_write(struct bsc_fd *bfd)
840{
841 struct ipa_proxy_conn *ipc = bfd->data;
842 struct ipa_bts_conn *ipbc = ipc->bts_conn;
843 struct llist_head *lh;
844 struct msgb *msg;
845 char *btsbsc;
846 int ret;
847
848 if ((bfd->priv_nr & 0xff) <= 2)
849 btsbsc = "BTS";
850 else
851 btsbsc = "BSC";
852
853
854 /* get the next msg for this timeslot */
855 if (llist_empty(&ipc->tx_queue)) {
856 bfd->when &= ~BSC_FD_WRITE;
857 return 0;
858 }
859 lh = ipc->tx_queue.next;
860 llist_del(lh);
861 msg = llist_entry(lh, struct msgb, list);
862
863 logp_ipbc_uid(DMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
864 DEBUGPC(DMI, "TX %04x: %s\n", bfd->priv_nr,
865 hexdump(msg->data, msg->len));
866
867 ret = send(bfd->fd, msg->data, msg->len, 0);
868 msgb_free(msg);
869
870 if (ret == 0) {
871 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
872 LOGP(DINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc);
873 handle_dead_socket(bfd);
874 }
875
876 return ret;
877}
878
879/* callback from select.c in case one of the fd's can be read/written */
880static int ipaccess_fd_cb(struct bsc_fd *bfd, unsigned int what)
881{
882 int rc = 0;
883
884 if (what & BSC_FD_READ) {
885 rc = handle_tcp_read(bfd);
886 if (rc < 0)
887 return rc;
888 }
889 if (what & BSC_FD_WRITE)
890 rc = handle_tcp_write(bfd);
891
892 return rc;
893}
894
895/* callback of the listening filedescriptor */
896static int listen_fd_cb(struct bsc_fd *listen_bfd, unsigned int what)
897{
898 int ret;
899 struct ipa_proxy_conn *ipc;
900 struct bsc_fd *bfd;
901 struct sockaddr_in sa;
902 socklen_t sa_len = sizeof(sa);
903
904 if (!(what & BSC_FD_READ))
905 return 0;
906
907 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
908 if (ret < 0) {
909 perror("accept");
910 return ret;
911 }
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200912 DEBUGP(DINP, "accept()ed new %s link from %s\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100913 (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL",
914 inet_ntoa(sa.sin_addr));
915
916 ipc = alloc_conn();
917 if (!ipc) {
918 close(ret);
919 return -ENOMEM;
920 }
921
922 bfd = &ipc->fd;
923 bfd->fd = ret;
924 bfd->data = ipc;
925 bfd->priv_nr = listen_bfd->priv_nr;
926 bfd->cb = ipaccess_fd_cb;
927 bfd->when = BSC_FD_READ;
928 ret = bsc_register_fd(bfd);
929 if (ret < 0) {
930 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
931 close(bfd->fd);
932 talloc_free(ipc);
933 return ret;
934 }
935
936 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200937 ret = ipaccess_send_id_req(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100938
939 return 0;
940}
941
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800942static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port)
943{
944 int ret;
945 struct sockaddr_in addr;
946 socklen_t len = sizeof(addr);
947 memset(&addr, 0, sizeof(addr));
948
949 addr.sin_family = AF_INET;
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800950 addr.sin_port = htons(port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800951 addr.sin_addr = ip;
952
953 ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len);
954 if (ret < 0) {
955 LOGP(DINP, LOGL_ERROR, "Failed to forward GPRS message.\n");
956 }
957}
958
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800959static int gprs_ns_cb(struct bsc_fd *bfd, unsigned int what)
960{
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800961 struct ipa_bts_conn *bts;
962 char buf[4096];
963 int ret;
964 struct sockaddr_in sock;
965 socklen_t len = sizeof(sock);
966
967 /* 1. get the data... */
968 ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len);
969 if (ret < 0) {
970 LOGP(DINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno));
971 return -1;
972 }
973
974 bts = bfd->data;
975
976 /* 2. figure out where to send it to */
977 if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) {
978 LOGP(DINP, LOGL_DEBUG, "GPRS NS msg from network.\n");
979 send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000);
980 } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) {
981 LOGP(DINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n");
982 send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000);
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800983 } else {
984 LOGP(DINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800985 }
986
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800987 return 0;
988}
989
Harald Welte2ca7c312009-12-23 22:44:04 +0100990/* Actively connect to a BSC. */
991static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data)
992{
993 struct ipa_proxy_conn *ipc;
994 struct bsc_fd *bfd;
995 int ret, on = 1;
996
997 ipc = alloc_conn();
998 if (!ipc)
999 return NULL;
1000
1001 ipc->bts_conn = data;
1002
1003 bfd = &ipc->fd;
1004 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1005 bfd->cb = ipaccess_fd_cb;
1006 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
1007 bfd->data = ipc;
1008 bfd->priv_nr = priv_nr;
1009
1010 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1011
1012 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
1013 if (ret < 0) {
Holger Hans Peter Freyther6cb9b232010-06-09 15:15:11 +08001014 LOGP(DINP, LOGL_ERROR, "Could not connect socket: %s\n",
1015 inet_ntoa(sa->sin_addr));
Harald Welte2ca7c312009-12-23 22:44:04 +01001016 close(bfd->fd);
1017 talloc_free(ipc);
1018 return NULL;
1019 }
1020
1021 /* pre-fill tx_queue with identity request */
1022 ret = bsc_register_fd(bfd);
1023 if (ret < 0) {
1024 close(bfd->fd);
1025 talloc_free(ipc);
1026 return NULL;
1027 }
1028
1029 return ipc;
1030}
1031
1032static int ipaccess_proxy_setup(void)
1033{
1034 int ret;
1035
1036 ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy);
1037 if (!ipp)
1038 return -ENOMEM;
1039 INIT_LLIST_HEAD(&ipp->bts_list);
1040 ipp->reconn_timer.cb = reconn_tmr_cb;
1041 ipp->reconn_timer.data = ipp;
1042
1043 /* Listen for OML connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001044 ret = make_sock(&ipp->oml_listen_fd, IPPROTO_TCP, INADDR_ANY,
1045 IPA_TCP_PORT_OML, OML_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001046 if (ret < 0)
1047 return ret;
1048
1049 /* Listen for RSL connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001050 ret = make_sock(&ipp->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY,
1051 IPA_TCP_PORT_RSL, RSL_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001052
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001053 if (ret < 0)
1054 return ret;
1055
1056 /* Connect the GPRS NS Socket */
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001057 if (gprs_ns_ipaddr) {
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +08001058 inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr);
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001059 inet_aton(listen_ipaddr, &ipp->listen_addr);
1060 }
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001061
Harald Welte2ca7c312009-12-23 22:44:04 +01001062 return ret;
1063}
1064
1065static void signal_handler(int signal)
1066{
1067 fprintf(stdout, "signal %u received\n", signal);
1068
1069 switch (signal) {
1070 case SIGABRT:
1071 /* in case of abort, we want to obtain a talloc report
1072 * and then return to the caller, who will abort the process */
1073 case SIGUSR1:
1074 talloc_report_full(tall_bsc_ctx, stderr);
1075 break;
1076 default:
1077 break;
1078 }
1079}
1080
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001081static void print_help()
1082{
1083 printf(" ipaccess-proxy is a proxy BTS.\n");
1084 printf(" -h --help. This help text.\n");
1085 printf(" -l --listen IP. The ip to listen to.\n");
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001086 printf(" -b --bsc IP. The BSC IP address.\n");
1087 printf(" -g --gprs IP. Take GPRS NS from that IP.\n");
1088 printf("\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001089 printf(" -s --disable-color. Disable the color inside the logging message.\n");
1090 printf(" -e --log-level number. Set the global loglevel.\n");
1091 printf(" -T --timestamp. Prefix every log message with a timestamp.\n");
1092 printf(" -V --version. Print the version of OpenBSC.\n");
1093}
1094
1095static void print_usage()
1096{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001097 printf("Usage: ipaccess-proxy [options]\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001098}
1099
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001100enum {
1101 IPA_PROXY_OPT_LISTEN_NONE = 0,
1102 IPA_PROXY_OPT_LISTEN_IP = (1 << 0),
1103 IPA_PROXY_OPT_BSC_IP = (1 << 1),
1104};
1105
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001106static void handle_options(int argc, char** argv)
1107{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001108 int options_mask = 0;
1109
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001110 /* disable explicit missing arguments error output from getopt_long */
1111 opterr = 0;
1112
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001113 while (1) {
1114 int option_index = 0, c;
1115 static struct option long_options[] = {
1116 {"help", 0, 0, 'h'},
1117 {"disable-color", 0, 0, 's'},
1118 {"timestamp", 0, 0, 'T'},
1119 {"log-level", 1, 0, 'e'},
1120 {"listen", 1, 0, 'l'},
1121 {"bsc", 1, 0, 'b'},
1122 {0, 0, 0, 0}
1123 };
1124
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001125 c = getopt_long(argc, argv, "hsTe:l:b:g:",
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001126 long_options, &option_index);
1127 if (c == -1)
1128 break;
1129
1130 switch (c) {
1131 case 'h':
1132 print_usage();
1133 print_help();
1134 exit(0);
1135 case 'l':
1136 listen_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001137 options_mask |= IPA_PROXY_OPT_LISTEN_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001138 break;
1139 case 'b':
1140 bsc_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001141 options_mask |= IPA_PROXY_OPT_BSC_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001142 break;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001143 case 'g':
1144 gprs_ns_ipaddr = optarg;
1145 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001146 case 's':
1147 log_set_use_color(stderr_target, 0);
1148 break;
1149 case 'T':
1150 log_set_print_timestamp(stderr_target, 1);
1151 break;
1152 case 'e':
1153 log_set_log_level(stderr_target, atoi(optarg));
1154 break;
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001155 case '?':
1156 if (optopt) {
1157 printf("ERROR: missing mandatory argument "
1158 "for `%s' option\n", argv[optind-1]);
1159 } else {
1160 printf("ERROR: unknown option `%s'\n",
1161 argv[optind-1]);
1162 }
1163 print_usage();
1164 print_help();
1165 exit(EXIT_FAILURE);
1166 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001167 default:
1168 /* ignore */
1169 break;
1170 }
1171 }
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001172 if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP))
1173 != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) {
1174 printf("ERROR: You have to specify `--listen' and `--bsc' "
1175 "options at least.\n");
1176 print_usage();
1177 print_help();
1178 exit(EXIT_FAILURE);
1179 }
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001180}
1181
Harald Welte2ca7c312009-12-23 22:44:04 +01001182int main(int argc, char **argv)
1183{
1184 int rc;
1185
Harald Welte2ca7c312009-12-23 22:44:04 +01001186 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
1187
Harald Weltedc5062b2010-03-26 21:28:59 +08001188 log_init(&log_info);
1189 stderr_target = log_target_create_stderr();
1190 log_add_target(stderr_target);
1191 log_set_all_filter(stderr_target, 1);
1192 log_parse_category_mask(stderr_target, "DINP:DMI");
Harald Welte2ca7c312009-12-23 22:44:04 +01001193
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001194 handle_options(argc, argv);
1195
Harald Welte2ca7c312009-12-23 22:44:04 +01001196 rc = ipaccess_proxy_setup();
1197 if (rc < 0)
1198 exit(1);
1199
1200 signal(SIGUSR1, &signal_handler);
1201 signal(SIGABRT, &signal_handler);
1202
1203 while (1) {
1204 bsc_select_main(0);
1205 }
1206}