blob: aa5e7b499484b20b5ca03553a3df118badbd0ccc [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
Neels Hofmeyrc0164792017-09-04 15:15:32 +020040#include <osmocom/bsc/gsm_data.h>
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020041#include <osmocom/core/application.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 Welte4a88a492014-08-20 23:46:40 +020045#include <osmocom/gsm/ipa.h>
Harald Welteeb623012014-08-18 22:33:12 +020046#include <osmocom/abis/ipa.h>
Harald Welte4a88a492014-08-20 23:46:40 +020047#include <osmocom/abis/ipaccess.h>
Neels Hofmeyrc0164792017-09-04 15:15:32 +020048#include <osmocom/bsc/debug.h>
49#include <osmocom/bsc/ipaccess.h>
Neels Hofmeyr978f58c2018-02-13 17:37:39 +010050#include <osmocom/core/socket.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010051#include <osmocom/core/talloc.h>
Harald Welte2ca7c312009-12-23 22:44:04 +010052
Harald Welte2ca7c312009-12-23 22:44:04 +010053/* one instance of an ip.access protocol proxy */
54struct ipa_proxy {
55 /* socket where we listen for incoming OML from BTS */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020056 struct osmo_fd oml_listen_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010057 /* socket where we listen for incoming RSL from BTS */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020058 struct osmo_fd rsl_listen_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010059 /* list of BTS's (struct ipa_bts_conn */
60 struct llist_head bts_list;
61 /* the BSC reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020062 struct osmo_timer_list reconn_timer;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +080063 /* global GPRS NS data */
64 struct in_addr gprs_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +080065 struct in_addr listen_addr;
Harald Welte2ca7c312009-12-23 22:44:04 +010066};
67
68/* global pointer to the proxy structure */
69static struct ipa_proxy *ipp;
70
71struct ipa_proxy_conn {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020072 struct osmo_fd fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010073 struct llist_head tx_queue;
74 struct ipa_bts_conn *bts_conn;
75};
Harald Welte2ca7c312009-12-23 22:44:04 +010076#define MAX_TRX 4
77
78/* represents a particular BTS in our proxy */
79struct ipa_bts_conn {
80 /* list of BTS's (ipa_proxy->bts_list) */
81 struct llist_head list;
82 /* back pointer to the proxy which we belong to */
83 struct ipa_proxy *ipp;
84 /* the unit ID as determined by CCM */
85 struct {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020086 uint16_t site_id;
87 uint16_t bts_id;
Harald Welte2ca7c312009-12-23 22:44:04 +010088 } unit_id;
89
90 /* incoming connections from BTS */
91 struct ipa_proxy_conn *oml_conn;
92 struct ipa_proxy_conn *rsl_conn[MAX_TRX];
93
94 /* outgoing connections to BSC */
95 struct ipa_proxy_conn *bsc_oml_conn;
96 struct ipa_proxy_conn *bsc_rsl_conn[MAX_TRX];
97
98 /* UDP sockets for BTS and BSC injection */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020099 struct osmo_fd udp_bts_fd;
100 struct osmo_fd udp_bsc_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100101
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800102 /* NS data */
103 struct in_addr bts_addr;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200104 struct osmo_fd gprs_ns_fd;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800105 int gprs_local_port;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800106 uint16_t gprs_orig_port;
107 uint32_t gprs_orig_ip;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800108
Harald Welte36ac7752011-07-16 13:38:48 +0200109 char *id_tags[256];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200110 uint8_t *id_resp;
Harald Welte2ca7c312009-12-23 22:44:04 +0100111 unsigned int id_resp_len;
112};
113
114enum ipp_fd_type {
115 OML_FROM_BTS = 1,
116 RSL_FROM_BTS = 2,
117 OML_TO_BSC = 3,
118 RSL_TO_BSC = 4,
119 UDP_TO_BTS = 5,
120 UDP_TO_BSC = 6,
121};
122
Neels Hofmeyr87592b82018-02-14 01:44:06 +0100123extern void *tall_bsc_ctx;
Harald Welte2ca7c312009-12-23 22:44:04 +0100124
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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200129static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800130
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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200201static int handle_udp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100202{
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)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200215 LOGP(DLINP, 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) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200220 DEBUGP(DLINP, "UDP peer disappeared, dead socket\n");
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200221 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100222 close(bfd->fd);
223 bfd->fd = -1;
224 msgb_free(msg);
225 return -EIO;
226 }
227 if (ret < sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200228 DEBUGP(DLINP, "could not even read header!?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100229 msgb_free(msg);
230 return -EIO;
231 }
232 msgb_put(msg, ret);
233 msg->l2h = msg->data + sizeof(*hh);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200234 DEBUGP(DLMI, "UDP RX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100235
236 if (hh->len != msg->len - sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200237 DEBUGP(DLINP, "length (%u/%u) disagrees with header(%u)\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100238 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:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200252 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100253 "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:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200269 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100270 "OML FD\n", hh->proto);
Holger Hans Peter Freyther4e5f93c2014-06-12 11:32:25 +0200271 /* fall through */
Harald Welte2ca7c312009-12-23 22:44:04 +0100272 case IPAC_PROTO_IPACCESS:
273 case IPAC_PROTO_OML:
274 other_conn = ipbc->bsc_oml_conn;
275 break;
276 }
277 break;
278 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200279 DEBUGP(DLINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr);
Harald Welte2ca7c312009-12-23 22:44:04 +0100280 break;
281 }
282
283 if (other_conn) {
284 /* enqueue the message for TX on the respective FD */
285 msgb_enqueue(&other_conn->tx_queue, msg);
286 other_conn->fd.when |= BSC_FD_WRITE;
287 } else
288 msgb_free(msg);
289
290 return 0;
291}
292
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200293static int handle_udp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100294{
295 /* not implemented yet */
296 bfd->when &= ~BSC_FD_WRITE;
297
298 return -EIO;
299}
300
301/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200302static int udp_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100303{
304 int rc = 0;
305
306 if (what & BSC_FD_READ)
307 rc = handle_udp_read(bfd);
308 if (what & BSC_FD_WRITE)
309 rc = handle_udp_write(bfd);
310
311 return rc;
312}
313
314
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200315static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200316 uint16_t site_id, uint16_t bts_id,
317 uint16_t trx_id, struct tlv_parsed *tlvp,
Harald Welte2ca7c312009-12-23 22:44:04 +0100318 struct msgb *msg)
319{
320 struct ipa_bts_conn *ipbc;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200321 uint16_t udp_port;
Harald Welte2ca7c312009-12-23 22:44:04 +0100322 int ret = 0;
323 struct sockaddr_in sin;
324
325 memset(&sin, 0, sizeof(sin));
326 sin.sin_family = AF_INET;
327 inet_aton(bsc_ipaddr, &sin.sin_addr);
328
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200329 DEBUGP(DLINP, "(%u/%u/%u) New BTS connection: ",
Harald Welte2ca7c312009-12-23 22:44:04 +0100330 site_id, bts_id, trx_id);
331
332 /* OML needs to be established before RSL */
333 if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200334 DEBUGPC(DLINP, "Not a OML connection ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100335 return -EIO;
336 }
337
338 /* allocate new BTS connection data structure */
339 ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn);
340 if (!ipbc) {
341 ret = -ENOMEM;
342 goto err_out;
343 }
344
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200345 DEBUGPC(DLINP, "Created BTS Conn data structure\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100346 ipbc->ipp = ipp;
347 ipbc->unit_id.site_id = site_id;
348 ipbc->unit_id.bts_id = bts_id;
349 ipbc->oml_conn = ipc;
350 ipc->bts_conn = ipbc;
351
352 /* store the content of the ID TAGS for later reference */
353 store_idtags(ipbc, tlvp);
354 ipbc->id_resp_len = msg->len;
355 ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len);
356 memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len);
357
358 /* Create OML TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100359 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100360 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
361 if (!ipbc->bsc_oml_conn) {
362 ret = -EIO;
363 goto err_bsc_conn;
364 }
365
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200366 DEBUGP(DLINP, "(%u/%u/%u) OML Connected to BSC\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100367 site_id, bts_id, trx_id);
368
369 /* Create UDP socket for BTS packet injection */
370 udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100);
Neels Hofmeyr978f58c2018-02-13 17:37:39 +0100371 ipbc->udp_bts_fd.priv_nr = UDP_TO_BTS;
372 ipbc->udp_bts_fd.cb = udp_fd_cb;
373 ipbc->udp_bts_fd.data = ipbc;
374 ret = osmo_sock_init_ofd(&ipbc->udp_bts_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
375 NULL, udp_port, OSMO_SOCK_F_BIND);
Harald Welte2ca7c312009-12-23 22:44:04 +0100376 if (ret < 0)
377 goto err_udp_bts;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200378 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100379 "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port);
380
381 /* Create UDP socket for BSC packet injection */
382 udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100);
Neels Hofmeyr978f58c2018-02-13 17:37:39 +0100383 ipbc->udp_bsc_fd.priv_nr = UDP_TO_BSC;
384 ipbc->udp_bsc_fd.cb = udp_fd_cb;
385 ipbc->udp_bsc_fd.data = ipbc;
386 ret = osmo_sock_init_ofd(&ipbc->udp_bsc_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
387 NULL, udp_port, OSMO_SOCK_F_BIND);
Harald Welte2ca7c312009-12-23 22:44:04 +0100388 if (ret < 0)
389 goto err_udp_bsc;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200390 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100391 "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800392
393
394 /* GPRS NS related code */
395 if (gprs_ns_ipaddr) {
396 struct sockaddr_in sock;
397 socklen_t len = sizeof(sock);
Pablo Neira Ayuso7e737002011-04-11 16:33:17 +0200398
Neels Hofmeyr978f58c2018-02-13 17:37:39 +0100399 ipbc->gprs_ns_fd.priv_nr = 0;
400 ipbc->gprs_ns_fd.cb = gprs_ns_cb;
401 ipbc->gprs_ns_fd.data = ipbc;
402 ret = osmo_sock_init_ofd(&ipbc->gprs_ns_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
403 listen_ipaddr, 0, OSMO_SOCK_F_BIND);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800404 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200405 LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800406 goto err_udp_bsc;
407 }
408
409 ret = getsockname(ipbc->gprs_ns_fd.fd, (struct sockaddr* ) &sock, &len);
410 ipbc->gprs_local_port = ntohs(sock.sin_port);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200411 LOGP(DLINP, LOGL_NOTICE,
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800412 "Created GPRS NS Socket. Listening on: %s:%d\n",
413 inet_ntoa(sock.sin_addr), ipbc->gprs_local_port);
414
415 ret = getpeername(bfd->fd, (struct sockaddr* ) &sock, &len);
416 ipbc->bts_addr = sock.sin_addr;
417 }
418
Harald Welte2ca7c312009-12-23 22:44:04 +0100419 llist_add(&ipbc->list, &ipp->bts_list);
420
421 return 0;
422
423err_udp_bsc:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200424 osmo_fd_unregister(&ipbc->udp_bts_fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100425err_udp_bts:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200426 osmo_fd_unregister(&ipbc->bsc_oml_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100427 close(ipbc->bsc_oml_conn->fd.fd);
428 talloc_free(ipbc->bsc_oml_conn);
429 ipbc->bsc_oml_conn = NULL;
430err_bsc_conn:
431 talloc_free(ipbc->id_resp);
432 talloc_free(ipbc);
433#if 0
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200434 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100435 close(bfd->fd);
436 talloc_free(bfd);
437#endif
438err_out:
439 return ret;
440}
441
442static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200443 struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100444{
445 struct tlv_parsed tlvp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200446 uint8_t msg_type = *(msg->l2h);
Harald Welteeb623012014-08-18 22:33:12 +0200447 struct ipaccess_unit unit_data;
Harald Welte2ca7c312009-12-23 22:44:04 +0100448 struct ipa_bts_conn *ipbc;
449 int ret = 0;
450
451 switch (msg_type) {
452 case IPAC_MSGT_PING:
Harald Welte4a88a492014-08-20 23:46:40 +0200453 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100454 break;
455 case IPAC_MSGT_PONG:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200456 DEBUGP(DLMI, "PONG!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100457 break;
458 case IPAC_MSGT_ID_RESP:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200459 DEBUGP(DLMI, "ID_RESP ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100460 /* parse tags, search for Unit ID */
Harald Welte4a88a492014-08-20 23:46:40 +0200461 ipa_ccm_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
Pablo Neira Ayuso625295b2011-04-07 14:15:16 +0200462 msgb_l2len(msg)-2);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200463 DEBUGP(DLMI, "\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100464
465 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200466 LOGP(DLINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100467 return -EIO;
468 }
469
470 /* lookup BTS, create sign_link, ... */
Harald Welteeb623012014-08-18 22:33:12 +0200471 memset(&unit_data, 0, sizeof(unit_data));
Harald Welte4a88a492014-08-20 23:46:40 +0200472 ipa_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
Harald Welteeb623012014-08-18 22:33:12 +0200473 &unit_data);
474 ipbc = find_bts_by_unitid(ipp, unit_data.site_id, unit_data.bts_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100475 if (!ipbc) {
476 /* We have not found an ipbc (per-bts proxy instance)
477 * for this BTS yet. The first connection of a new BTS must
478 * be a OML connection. We allocate the associated data structures,
479 * and try to connect to the remote end */
480
Harald Welteeb623012014-08-18 22:33:12 +0200481 return ipbc_alloc_connect(ipc, bfd, unit_data.site_id,
482 unit_data.bts_id,
483 unit_data.trx_id, &tlvp, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100484 /* if this fails, the caller will clean up bfd */
485 } else {
486 struct sockaddr_in sin;
487 memset(&sin, 0, sizeof(sin));
488 sin.sin_family = AF_INET;
489 inet_aton(bsc_ipaddr, &sin.sin_addr);
490
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200491 DEBUGP(DLINP, "Identified BTS %u/%u/%u\n",
Harald Welteeb623012014-08-18 22:33:12 +0200492 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100493
494 if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200495 LOGP(DLINP, LOGL_ERROR, "Second OML connection from "
Harald Welte2ca7c312009-12-23 22:44:04 +0100496 "same BTS ?!?\n");
497 return 0;
498 }
499
Harald Welteeb623012014-08-18 22:33:12 +0200500 if (unit_data.trx_id >= MAX_TRX) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200501 LOGP(DLINP, LOGL_ERROR, "We don't support more "
Harald Welte2ca7c312009-12-23 22:44:04 +0100502 "than %u TRX\n", MAX_TRX);
503 return -EINVAL;
504 }
505
506 ipc->bts_conn = ipbc;
507 /* store TRX number in higher 8 bit of the bfd private number */
Harald Welteeb623012014-08-18 22:33:12 +0200508 bfd->priv_nr |= unit_data.trx_id << 8;
509 ipbc->rsl_conn[unit_data.trx_id] = ipc;
Harald Welte2ca7c312009-12-23 22:44:04 +0100510
511 /* Create RSL TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100512 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welteeb623012014-08-18 22:33:12 +0200513 ipbc->bsc_rsl_conn[unit_data.trx_id] =
514 connect_bsc(&sin, RSL_TO_BSC | (unit_data.trx_id << 8), ipbc);
Harald Welte2ca7c312009-12-23 22:44:04 +0100515 if (!ipbc->bsc_oml_conn)
516 return -EIO;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200517 DEBUGP(DLINP, "(%u/%u/%u) Connected RSL to BSC\n",
Harald Welteeb623012014-08-18 22:33:12 +0200518 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100519 }
520 break;
521 case IPAC_MSGT_ID_GET:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200522 DEBUGP(DLMI, "ID_GET\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100523 if ((bfd->priv_nr & 0xff) != OML_TO_BSC &&
524 (bfd->priv_nr & 0xff) != RSL_TO_BSC) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200525 DEBUGP(DLINP, "IDentity REQuest from BTS ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100526 return -EIO;
527 }
528 ipbc = ipc->bts_conn;
529 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200530 DEBUGP(DLINP, "ID_GET from BSC before we have ID_RESP from BTS\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100531 return -EIO;
532 }
533 ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len);
Neels Hofmeyr127fc932016-02-24 19:29:59 +0100534 if (ret != ipbc->id_resp_len) {
535 LOGP(DLINP, LOGL_ERROR, "Partial write: %d of %d\n",
536 ret, ipbc->id_resp_len);
537 return -EIO;
538 }
539 ret = 0;
Harald Welte2ca7c312009-12-23 22:44:04 +0100540 break;
541 case IPAC_MSGT_ID_ACK:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200542 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Harald Welte4a88a492014-08-20 23:46:40 +0200543 ret = ipa_ccm_send_id_ack(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100544 break;
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800545 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200546 LOGP(DLMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800547 return 1;
548 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100549 }
Neels Hofmeyr127fc932016-02-24 19:29:59 +0100550 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100551}
552
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200553struct msgb *ipaccess_proxy_read_msg(struct osmo_fd *bfd, int *error)
Harald Welte2ca7c312009-12-23 22:44:04 +0100554{
555 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
556 struct ipaccess_head *hh;
557 int len, ret = 0;
558
559 if (!msg) {
560 *error = -ENOMEM;
561 return NULL;
562 }
563
564 /* first read our 3-byte header */
565 hh = (struct ipaccess_head *) msg->data;
566 ret = recv(bfd->fd, msg->data, 3, 0);
567 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100568 if (errno != EAGAIN)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200569 LOGP(DLINP, LOGL_ERROR, "recv error: %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100570 msgb_free(msg);
571 *error = ret;
572 return NULL;
573 } else if (ret == 0) {
574 msgb_free(msg);
575 *error = ret;
576 return NULL;
577 }
578
579 msgb_put(msg, ret);
580
581 /* then read te length as specified in header */
582 msg->l2h = msg->data + sizeof(*hh);
583 len = ntohs(hh->len);
584 ret = recv(bfd->fd, msg->l2h, len, 0);
585 if (ret < len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200586 LOGP(DLINP, LOGL_ERROR, "short read!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100587 msgb_free(msg);
588 *error = -EIO;
589 return NULL;
590 }
591 msgb_put(msg, ret);
592
593 return msg;
594}
595
596static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc,
597 unsigned int priv_nr)
598{
599 struct ipa_proxy_conn *bsc_conn;
600 unsigned int trx_id = priv_nr >> 8;
601
602 switch (priv_nr & 0xff) {
603 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
604 bsc_conn = ipbc->bsc_oml_conn;
605 break;
606 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
607 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
608 break;
609 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
610 bsc_conn = ipbc->oml_conn;
611 break;
612 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
613 bsc_conn = ipbc->rsl_conn[trx_id];
614 break;
615 default:
616 bsc_conn = NULL;
617 break;
618 }
619 return bsc_conn;
620}
621
622static void reconn_tmr_cb(void *data)
623{
624 struct ipa_proxy *ipp = data;
625 struct ipa_bts_conn *ipbc;
626 struct sockaddr_in sin;
627 int i;
628
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200629 DEBUGP(DLINP, "Running reconnect timer\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100630
631 memset(&sin, 0, sizeof(sin));
632 sin.sin_family = AF_INET;
633 inet_aton(bsc_ipaddr, &sin.sin_addr);
634
635 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
636 /* if OML to BSC is dead, try to restore it */
637 if (ipbc->oml_conn && !ipbc->bsc_oml_conn) {
Harald Welte87ed5cd2009-12-23 22:47:53 +0100638 sin.sin_port = htons(IPA_TCP_PORT_OML);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200639 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
640 LOGPC(DLINP, LOGL_NOTICE, "OML Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100641 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
642 if (!ipbc->bsc_oml_conn)
643 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200644 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
645 LOGPC(DLINP, LOGL_NOTICE, "OML Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100646 }
647 /* if we (still) don't have a OML connection, skip RSL */
648 if (!ipbc->oml_conn || !ipbc->bsc_oml_conn)
649 continue;
650
651 for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) {
652 unsigned int priv_nr;
653 /* don't establish RSL links which we don't have */
654 if (!ipbc->rsl_conn[i])
655 continue;
656 if (ipbc->bsc_rsl_conn[i])
657 continue;
658 priv_nr = ipbc->rsl_conn[i]->fd.priv_nr;
659 priv_nr &= ~0xff;
660 priv_nr |= RSL_TO_BSC;
Harald Welte87ed5cd2009-12-23 22:47:53 +0100661 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200662 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
663 LOGPC(DLINP, LOGL_NOTICE, "RSL Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100664 ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc);
Holger Hans Peter Freytherc9251fa2013-07-14 08:47:06 +0200665 if (!ipbc->bsc_rsl_conn[i])
Harald Welte2ca7c312009-12-23 22:44:04 +0100666 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200667 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
668 LOGPC(DLINP, LOGL_NOTICE, "RSL Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100669 }
670 }
671 return;
672
673reschedule:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200674 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100675}
676
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200677static void handle_dead_socket(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100678{
679 struct ipa_proxy_conn *ipc = bfd->data; /* local conn */
680 struct ipa_proxy_conn *bsc_conn; /* remote conn */
681 struct ipa_bts_conn *ipbc = ipc->bts_conn;
682 unsigned int trx_id = bfd->priv_nr >> 8;
683 struct msgb *msg, *msg2;
684
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200685 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100686 close(bfd->fd);
687 bfd->fd = -1;
688
689 /* FIXME: clear tx_queue, remove all references, etc. */
690 llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list)
691 msgb_free(msg);
692
693 switch (bfd->priv_nr & 0xff) {
694 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
Pablo Neira Ayuso3c409c22011-03-27 21:31:48 +0200695 /* The BTS started a connection with us but we got no
696 * IPAC_MSGT_ID_RESP message yet, in that scenario we did not
697 * allocate the ipa_bts_conn structure. */
698 if (ipbc == NULL)
699 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100700 ipbc->oml_conn = NULL;
701 bsc_conn = ipbc->bsc_oml_conn;
702 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200703 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100704 close(bsc_conn->fd.fd);
705 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
706 msgb_free(msg);
707 talloc_free(bsc_conn);
708 ipbc->bsc_oml_conn = NULL;
709 /* FIXME: do we need to delete the entire ipbc ? */
710 break;
711 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
712 ipbc->rsl_conn[trx_id] = NULL;
713 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
714 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200715 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100716 close(bsc_conn->fd.fd);
717 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
718 msgb_free(msg);
719 talloc_free(bsc_conn);
720 ipbc->bsc_rsl_conn[trx_id] = NULL;
721 break;
722 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
723 ipbc->bsc_oml_conn = NULL;
724 bsc_conn = ipbc->oml_conn;
725 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200726 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100727 break;
728 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
729 ipbc->bsc_rsl_conn[trx_id] = NULL;
730 bsc_conn = ipbc->rsl_conn[trx_id];
731 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200732 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100733 break;
734 default:
735 bsc_conn = NULL;
736 break;
737 }
738
739 talloc_free(ipc);
740}
741
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800742static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg)
743{
744 uint8_t *nsvci;
745
746 if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC)
747 return;
748
749 if (msgb_l2len(msg) != 39)
750 return;
751
752 /*
753 * Check if this is a IPA Set Attribute or IPA Set Attribute ACK
754 * and if the FOM Class is GPRS NSVC0 and then we will patch it.
755 *
756 * The patch assumes the message looks like the one from the trace
757 * but we only match messages with a specific size anyway... So
758 * this hack should work just fine.
759 */
760
761 if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
762 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
763 msg->l2h[18] == 0xf5 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800764 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200765 ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8);
766 ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10);
767 *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port);
768 *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800769 } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
770 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
771 msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800772 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200773 *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port;
774 *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800775 }
776}
777
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200778static int handle_tcp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100779{
780 struct ipa_proxy_conn *ipc = bfd->data;
781 struct ipa_bts_conn *ipbc = ipc->bts_conn;
782 struct ipa_proxy_conn *bsc_conn;
Harald Weltea16ef3d2009-12-23 23:35:51 +0100783 struct msgb *msg;
Harald Welte2ca7c312009-12-23 22:44:04 +0100784 struct ipaccess_head *hh;
785 int ret = 0;
786 char *btsbsc;
787
Harald Welte2ca7c312009-12-23 22:44:04 +0100788 if ((bfd->priv_nr & 0xff) <= 2)
789 btsbsc = "BTS";
790 else
791 btsbsc = "BSC";
792
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200793 msg = ipaccess_proxy_read_msg(bfd, &ret);
Harald Welte2ca7c312009-12-23 22:44:04 +0100794 if (!msg) {
795 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200796 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
797 LOGPC(DLINP, LOGL_NOTICE, "%s disappeared, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100798 "dead socket\n", btsbsc);
799 handle_dead_socket(bfd);
800 }
801 return ret;
802 }
803
804 msgb_put(msg, ret);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200805 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
806 DEBUGPC(DLMI, "RX<-%s: %s\n", btsbsc, osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100807
808 hh = (struct ipaccess_head *) msg->data;
809 if (hh->proto == IPAC_PROTO_IPACCESS) {
810 ret = ipaccess_rcvmsg(ipc, msg, bfd);
811 if (ret < 0) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200812 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100813 close(bfd->fd);
814 bfd->fd = -1;
815 talloc_free(bfd);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800816 msgb_free(msg);
817 return ret;
818 } else if (ret == 0) {
819 /* we do not forward parts of the CCM protocol
820 * through the proxy but rather terminate it ourselves. */
821 msgb_free(msg);
822 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100823 }
Harald Welte2ca7c312009-12-23 22:44:04 +0100824 }
825
826 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200827 LOGP(DLINP, LOGL_ERROR,
Harald Welte2ca7c312009-12-23 22:44:04 +0100828 "received %s packet but no ipc->bts_conn?!?\n", btsbsc);
829 msgb_free(msg);
830 return -EIO;
831 }
832
833 bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr);
834 if (bsc_conn) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800835 if (gprs_ns_ipaddr)
836 patch_gprs_msg(ipbc, bfd->priv_nr, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100837 /* enqueue packet towards BSC */
838 msgb_enqueue(&bsc_conn->tx_queue, msg);
839 /* mark respective filedescriptor as 'we want to write' */
840 bsc_conn->fd.when |= BSC_FD_WRITE;
841 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200842 logp_ipbc_uid(DLINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8);
843 LOGPC(DLINP, LOGL_INFO, "Dropping packet from %s, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100844 "since remote connection is dead\n", btsbsc);
845 msgb_free(msg);
846 }
847
848 return ret;
849}
850
851/* a TCP socket is ready to be written to */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200852static int handle_tcp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100853{
854 struct ipa_proxy_conn *ipc = bfd->data;
855 struct ipa_bts_conn *ipbc = ipc->bts_conn;
856 struct llist_head *lh;
857 struct msgb *msg;
858 char *btsbsc;
859 int ret;
860
861 if ((bfd->priv_nr & 0xff) <= 2)
862 btsbsc = "BTS";
863 else
864 btsbsc = "BSC";
865
866
867 /* get the next msg for this timeslot */
868 if (llist_empty(&ipc->tx_queue)) {
869 bfd->when &= ~BSC_FD_WRITE;
870 return 0;
871 }
872 lh = ipc->tx_queue.next;
873 llist_del(lh);
874 msg = llist_entry(lh, struct msgb, list);
875
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200876 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
877 DEBUGPC(DLMI, "TX %04x: %s\n", bfd->priv_nr,
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200878 osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100879
880 ret = send(bfd->fd, msg->data, msg->len, 0);
881 msgb_free(msg);
882
883 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200884 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
885 LOGP(DLINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc);
Harald Welte2ca7c312009-12-23 22:44:04 +0100886 handle_dead_socket(bfd);
887 }
888
889 return ret;
890}
891
892/* callback from select.c in case one of the fd's can be read/written */
Harald Welteeb623012014-08-18 22:33:12 +0200893static int proxy_ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100894{
895 int rc = 0;
896
897 if (what & BSC_FD_READ) {
898 rc = handle_tcp_read(bfd);
899 if (rc < 0)
900 return rc;
901 }
902 if (what & BSC_FD_WRITE)
903 rc = handle_tcp_write(bfd);
904
905 return rc;
906}
907
908/* callback of the listening filedescriptor */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200909static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100910{
911 int ret;
912 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200913 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100914 struct sockaddr_in sa;
915 socklen_t sa_len = sizeof(sa);
916
917 if (!(what & BSC_FD_READ))
918 return 0;
919
920 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
921 if (ret < 0) {
922 perror("accept");
923 return ret;
924 }
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200925 DEBUGP(DLINP, "accept()ed new %s link from %s\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100926 (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL",
927 inet_ntoa(sa.sin_addr));
928
929 ipc = alloc_conn();
930 if (!ipc) {
931 close(ret);
932 return -ENOMEM;
933 }
934
935 bfd = &ipc->fd;
936 bfd->fd = ret;
937 bfd->data = ipc;
938 bfd->priv_nr = listen_bfd->priv_nr;
Harald Welteeb623012014-08-18 22:33:12 +0200939 bfd->cb = proxy_ipaccess_fd_cb;
Harald Welte2ca7c312009-12-23 22:44:04 +0100940 bfd->when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200941 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100942 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200943 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100944 close(bfd->fd);
945 talloc_free(ipc);
946 return ret;
947 }
948
949 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welte4a88a492014-08-20 23:46:40 +0200950 ret = ipa_ccm_send_id_req(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100951
952 return 0;
953}
954
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800955static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port)
956{
957 int ret;
958 struct sockaddr_in addr;
959 socklen_t len = sizeof(addr);
960 memset(&addr, 0, sizeof(addr));
961
962 addr.sin_family = AF_INET;
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800963 addr.sin_port = htons(port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800964 addr.sin_addr = ip;
965
966 ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len);
967 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200968 LOGP(DLINP, LOGL_ERROR, "Failed to forward GPRS message.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800969 }
970}
971
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200972static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800973{
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800974 struct ipa_bts_conn *bts;
975 char buf[4096];
976 int ret;
977 struct sockaddr_in sock;
978 socklen_t len = sizeof(sock);
979
980 /* 1. get the data... */
981 ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len);
982 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200983 LOGP(DLINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800984 return -1;
985 }
986
987 bts = bfd->data;
988
989 /* 2. figure out where to send it to */
990 if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200991 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from network.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800992 send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000);
993 } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200994 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800995 send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000);
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800996 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200997 LOGP(DLINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800998 }
999
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001000 return 0;
1001}
1002
Harald Welte2ca7c312009-12-23 22:44:04 +01001003/* Actively connect to a BSC. */
1004static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data)
1005{
1006 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001007 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +01001008 int ret, on = 1;
1009
1010 ipc = alloc_conn();
1011 if (!ipc)
1012 return NULL;
1013
1014 ipc->bts_conn = data;
1015
1016 bfd = &ipc->fd;
1017 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1018 bfd->cb = ipaccess_fd_cb;
1019 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
1020 bfd->data = ipc;
1021 bfd->priv_nr = priv_nr;
1022
Holger Hans Peter Freythere18209c2013-12-12 16:16:35 +01001023 if (bfd->fd < 0) {
1024 LOGP(DLINP, LOGL_ERROR, "Could not create socket: %s\n",
1025 strerror(errno));
1026 talloc_free(ipc);
1027 return NULL;
1028 }
1029
Harald Welte3c165d02016-11-26 14:09:34 +01001030 ret = setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1031 if (ret < 0) {
1032 LOGP(DLINP, LOGL_ERROR, "Could not set socket option\n");
1033 close(bfd->fd);
1034 talloc_free(ipc);
1035 return NULL;
1036 }
Harald Welte2ca7c312009-12-23 22:44:04 +01001037
1038 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
1039 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001040 LOGP(DLINP, LOGL_ERROR, "Could not connect socket: %s\n",
Holger Hans Peter Freyther6cb9b232010-06-09 15:15:11 +08001041 inet_ntoa(sa->sin_addr));
Harald Welte2ca7c312009-12-23 22:44:04 +01001042 close(bfd->fd);
1043 talloc_free(ipc);
1044 return NULL;
1045 }
1046
1047 /* pre-fill tx_queue with identity request */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001048 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +01001049 if (ret < 0) {
1050 close(bfd->fd);
1051 talloc_free(ipc);
1052 return NULL;
1053 }
1054
1055 return ipc;
1056}
1057
1058static int ipaccess_proxy_setup(void)
1059{
1060 int ret;
1061
1062 ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy);
1063 if (!ipp)
1064 return -ENOMEM;
1065 INIT_LLIST_HEAD(&ipp->bts_list);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001066 osmo_timer_setup(&ipp->reconn_timer, reconn_tmr_cb, ipp);
Harald Welte2ca7c312009-12-23 22:44:04 +01001067
1068 /* Listen for OML connections */
Neels Hofmeyr978f58c2018-02-13 17:37:39 +01001069 ipp->oml_listen_fd.priv_nr = OML_FROM_BTS;
1070 ipp->oml_listen_fd.cb = listen_fd_cb;
1071 ret = osmo_sock_init_ofd(&ipp->oml_listen_fd, AF_INET, SOCK_STREAM, IPPROTO_TCP,
1072 NULL, IPA_TCP_PORT_OML, OSMO_SOCK_F_BIND);
Harald Welte2ca7c312009-12-23 22:44:04 +01001073 if (ret < 0)
1074 return ret;
1075
1076 /* Listen for RSL connections */
Neels Hofmeyr978f58c2018-02-13 17:37:39 +01001077 ipp->rsl_listen_fd.priv_nr = RSL_FROM_BTS;
1078 ipp->rsl_listen_fd.cb = listen_fd_cb;
1079 ret = osmo_sock_init_ofd(&ipp->rsl_listen_fd, AF_INET, SOCK_STREAM, IPPROTO_TCP,
1080 NULL, IPA_TCP_PORT_RSL, OSMO_SOCK_F_BIND);
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001081 if (ret < 0)
1082 return ret;
1083
1084 /* Connect the GPRS NS Socket */
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001085 if (gprs_ns_ipaddr) {
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +08001086 inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr);
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001087 inet_aton(listen_ipaddr, &ipp->listen_addr);
1088 }
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001089
Harald Welte2ca7c312009-12-23 22:44:04 +01001090 return ret;
1091}
1092
1093static void signal_handler(int signal)
1094{
1095 fprintf(stdout, "signal %u received\n", signal);
1096
1097 switch (signal) {
1098 case SIGABRT:
1099 /* in case of abort, we want to obtain a talloc report
1100 * and then return to the caller, who will abort the process */
1101 case SIGUSR1:
1102 talloc_report_full(tall_bsc_ctx, stderr);
1103 break;
1104 default:
1105 break;
1106 }
1107}
1108
Harald Welted4ab13b2011-07-16 13:39:44 +02001109static void print_help(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001110{
1111 printf(" ipaccess-proxy is a proxy BTS.\n");
1112 printf(" -h --help. This help text.\n");
1113 printf(" -l --listen IP. The ip to listen to.\n");
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001114 printf(" -b --bsc IP. The BSC IP address.\n");
1115 printf(" -g --gprs IP. Take GPRS NS from that IP.\n");
1116 printf("\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001117 printf(" -s --disable-color. Disable the color inside the logging message.\n");
1118 printf(" -e --log-level number. Set the global loglevel.\n");
1119 printf(" -T --timestamp. Prefix every log message with a timestamp.\n");
1120 printf(" -V --version. Print the version of OpenBSC.\n");
1121}
1122
Harald Welted4ab13b2011-07-16 13:39:44 +02001123static void print_usage(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001124{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001125 printf("Usage: ipaccess-proxy [options]\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001126}
1127
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001128enum {
1129 IPA_PROXY_OPT_LISTEN_NONE = 0,
1130 IPA_PROXY_OPT_LISTEN_IP = (1 << 0),
1131 IPA_PROXY_OPT_BSC_IP = (1 << 1),
1132};
1133
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001134static void handle_options(int argc, char** argv)
1135{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001136 int options_mask = 0;
1137
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001138 /* disable explicit missing arguments error output from getopt_long */
1139 opterr = 0;
1140
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001141 while (1) {
1142 int option_index = 0, c;
1143 static struct option long_options[] = {
1144 {"help", 0, 0, 'h'},
1145 {"disable-color", 0, 0, 's'},
1146 {"timestamp", 0, 0, 'T'},
1147 {"log-level", 1, 0, 'e'},
1148 {"listen", 1, 0, 'l'},
1149 {"bsc", 1, 0, 'b'},
1150 {0, 0, 0, 0}
1151 };
1152
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001153 c = getopt_long(argc, argv, "hsTe:l:b:g:",
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001154 long_options, &option_index);
1155 if (c == -1)
1156 break;
1157
1158 switch (c) {
1159 case 'h':
1160 print_usage();
1161 print_help();
1162 exit(0);
1163 case 'l':
1164 listen_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001165 options_mask |= IPA_PROXY_OPT_LISTEN_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001166 break;
1167 case 'b':
1168 bsc_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001169 options_mask |= IPA_PROXY_OPT_BSC_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001170 break;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001171 case 'g':
1172 gprs_ns_ipaddr = optarg;
1173 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001174 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001175 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001176 break;
1177 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001178 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001179 break;
1180 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001181 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001182 break;
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001183 case '?':
1184 if (optopt) {
1185 printf("ERROR: missing mandatory argument "
1186 "for `%s' option\n", argv[optind-1]);
1187 } else {
1188 printf("ERROR: unknown option `%s'\n",
1189 argv[optind-1]);
1190 }
1191 print_usage();
1192 print_help();
1193 exit(EXIT_FAILURE);
1194 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001195 default:
1196 /* ignore */
1197 break;
1198 }
1199 }
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001200 if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP))
1201 != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) {
1202 printf("ERROR: You have to specify `--listen' and `--bsc' "
1203 "options at least.\n");
1204 print_usage();
1205 print_help();
1206 exit(EXIT_FAILURE);
1207 }
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001208}
1209
Neels Hofmeyr7997bf42018-02-13 17:16:44 +01001210static const struct log_info_cat log_categories[] = {
1211 [DMM] = {
1212 .name = "DMM",
1213 .description = "Layer3 Mobility Management (MM)",
1214 .color = "\033[1;33m",
1215 .enabled = 1, .loglevel = LOGL_NOTICE,
1216 },
1217};
1218
1219const struct log_info log_info = {
1220 .cat = log_categories,
1221 .num_cat = ARRAY_SIZE(log_categories),
1222};
1223
Harald Welte2ca7c312009-12-23 22:44:04 +01001224int main(int argc, char **argv)
1225{
1226 int rc;
1227
Harald Welte2ca7c312009-12-23 22:44:04 +01001228 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +02001229 msgb_talloc_ctx_init(tall_bsc_ctx, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +01001230
Neels Hofmeyre3416182018-03-05 05:31:14 +01001231 osmo_init_logging2(tall_bsc_ctx, &log_info);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001232 log_parse_category_mask(osmo_stderr_target, "DLINP:DLMI");
Harald Welte2ca7c312009-12-23 22:44:04 +01001233
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001234 handle_options(argc, argv);
1235
Harald Welte2ca7c312009-12-23 22:44:04 +01001236 rc = ipaccess_proxy_setup();
1237 if (rc < 0)
1238 exit(1);
1239
1240 signal(SIGUSR1, &signal_handler);
1241 signal(SIGABRT, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001242 osmo_init_ignore_signals();
Harald Welte2ca7c312009-12-23 22:44:04 +01001243
1244 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001245 osmo_select_main(0);
Harald Welte2ca7c312009-12-23 22:44:04 +01001246 }
1247}