blob: 39a0d8b246c14b4622c8fd49d748b5b0440050f6 [file] [log] [blame]
Harald Welte2ca7c312009-12-23 22:44:04 +01001/* OpenBSC Abis/IP proxy ip.access nanoBTS */
2
3/* (C) 2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08004 * (C) 2010 by On-Waves
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08005 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte2ca7c312009-12-23 22:44:04 +01006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte2ca7c312009-12-23 22:44:04 +010012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte2ca7c312009-12-23 22:44:04 +010018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte2ca7c312009-12-23 22:44:04 +010021 *
22 */
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <string.h>
29#include <signal.h>
30#include <time.h>
31#include <sys/fcntl.h>
Harald Welte2ca7c312009-12-23 22:44:04 +010032#include <sys/socket.h>
33#include <sys/ioctl.h>
34#include <arpa/inet.h>
35#include <netinet/in.h>
36
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +080037#define _GNU_SOURCE
38#include <getopt.h>
39
Harald Welte2ca7c312009-12-23 22:44:04 +010040#include <openbsc/gsm_data.h>
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 Welteeb623012014-08-18 22:33:12 +020045#include <osmocom/abis/ipa.h>
Harald Welte2ca7c312009-12-23 22:44:04 +010046#include <openbsc/debug.h>
47#include <openbsc/ipaccess.h>
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +020048#include <openbsc/socket.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010049#include <osmocom/core/talloc.h>
Harald Welte2ca7c312009-12-23 22:44:04 +010050
Harald Welte2ca7c312009-12-23 22:44:04 +010051/* one instance of an ip.access protocol proxy */
52struct ipa_proxy {
53 /* socket where we listen for incoming OML from BTS */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020054 struct osmo_fd oml_listen_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010055 /* socket where we listen for incoming RSL from BTS */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020056 struct osmo_fd rsl_listen_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010057 /* list of BTS's (struct ipa_bts_conn */
58 struct llist_head bts_list;
59 /* the BSC reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020060 struct osmo_timer_list reconn_timer;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +080061 /* global GPRS NS data */
62 struct in_addr gprs_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +080063 struct in_addr listen_addr;
Harald Welte2ca7c312009-12-23 22:44:04 +010064};
65
66/* global pointer to the proxy structure */
67static struct ipa_proxy *ipp;
68
69struct ipa_proxy_conn {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020070 struct osmo_fd fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010071 struct llist_head tx_queue;
72 struct ipa_bts_conn *bts_conn;
73};
Harald Welte2ca7c312009-12-23 22:44:04 +010074#define MAX_TRX 4
75
76/* represents a particular BTS in our proxy */
77struct ipa_bts_conn {
78 /* list of BTS's (ipa_proxy->bts_list) */
79 struct llist_head list;
80 /* back pointer to the proxy which we belong to */
81 struct ipa_proxy *ipp;
82 /* the unit ID as determined by CCM */
83 struct {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020084 uint16_t site_id;
85 uint16_t bts_id;
Harald Welte2ca7c312009-12-23 22:44:04 +010086 } unit_id;
87
88 /* incoming connections from BTS */
89 struct ipa_proxy_conn *oml_conn;
90 struct ipa_proxy_conn *rsl_conn[MAX_TRX];
91
92 /* outgoing connections to BSC */
93 struct ipa_proxy_conn *bsc_oml_conn;
94 struct ipa_proxy_conn *bsc_rsl_conn[MAX_TRX];
95
96 /* UDP sockets for BTS and BSC injection */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020097 struct osmo_fd udp_bts_fd;
98 struct osmo_fd udp_bsc_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010099
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800100 /* NS data */
101 struct in_addr bts_addr;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200102 struct osmo_fd gprs_ns_fd;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800103 int gprs_local_port;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800104 uint16_t gprs_orig_port;
105 uint32_t gprs_orig_ip;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800106
Harald Welte36ac7752011-07-16 13:38:48 +0200107 char *id_tags[256];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200108 uint8_t *id_resp;
Harald Welte2ca7c312009-12-23 22:44:04 +0100109 unsigned int id_resp_len;
110};
111
112enum ipp_fd_type {
113 OML_FROM_BTS = 1,
114 RSL_FROM_BTS = 2,
115 OML_TO_BSC = 3,
116 RSL_TO_BSC = 4,
117 UDP_TO_BTS = 5,
118 UDP_TO_BSC = 6,
119};
120
121/* some of the code against we link from OpenBSC needs this */
122void *tall_bsc_ctx;
123
124static char *listen_ipaddr;
125static char *bsc_ipaddr;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800126static char *gprs_ns_ipaddr;
Harald Welte2ca7c312009-12-23 22:44:04 +0100127
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200128static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800129
Holger Hans Peter Freyther3dac8812010-06-09 14:39:22 +0800130#define PROXY_ALLOC_SIZE 1200
Harald Welte2ca7c312009-12-23 22:44:04 +0100131
Harald Welte2ca7c312009-12-23 22:44:04 +0100132static struct ipa_bts_conn *find_bts_by_unitid(struct ipa_proxy *ipp,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200133 uint16_t site_id,
134 uint16_t bts_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100135{
136 struct ipa_bts_conn *ipbc;
137
138 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
139 if (ipbc->unit_id.site_id == site_id &&
140 ipbc->unit_id.bts_id == bts_id)
141 return ipbc;
142 }
143
144 return NULL;
145}
146
147struct ipa_proxy_conn *alloc_conn(void)
148{
149 struct ipa_proxy_conn *ipc;
150
151 ipc = talloc_zero(tall_bsc_ctx, struct ipa_proxy_conn);
152 if (!ipc)
153 return NULL;
154
155 INIT_LLIST_HEAD(&ipc->tx_queue);
156
157 return ipc;
158}
159
160static int store_idtags(struct ipa_bts_conn *ipbc, struct tlv_parsed *tlvp)
161{
162 unsigned int i, len;
163
164 for (i = 0; i <= 0xff; i++) {
165 if (!TLVP_PRESENT(tlvp, i))
166 continue;
167
168 len = TLVP_LEN(tlvp, i);
169#if 0
170 if (!ipbc->id_tags[i])
171 ipbc->id_tags[i] = talloc_size(tall_bsc_ctx, len);
172 else
173#endif
Harald Weltea16ef3d2009-12-23 23:35:51 +0100174 ipbc->id_tags[i] = talloc_realloc_size(ipbc,
Harald Welte2ca7c312009-12-23 22:44:04 +0100175 ipbc->id_tags[i], len);
176 if (!ipbc->id_tags[i])
177 return -ENOMEM;
178
179 memset(ipbc->id_tags[i], 0, len);
180 //memcpy(ipbc->id_tags[i], TLVP_VAL(tlvp, i), len);
181 }
182 return 0;
183}
184
185
186static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data);
187
188#define logp_ipbc_uid(ss, lvl, ipbc, trx_id) _logp_ipbc_uid(ss, lvl, __FILE__, __LINE__, ipbc, trx_id)
189
190static void _logp_ipbc_uid(unsigned int ss, unsigned int lvl, char *file, int line,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200191 struct ipa_bts_conn *ipbc, uint8_t trx_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100192{
193 if (ipbc)
Harald Weltedc5062b2010-03-26 21:28:59 +0800194 logp2(ss, lvl, file, line, 0, "(%u/%u/%u) ", ipbc->unit_id.site_id,
Harald Welte2ca7c312009-12-23 22:44:04 +0100195 ipbc->unit_id.bts_id, trx_id);
196 else
Harald Weltedc5062b2010-03-26 21:28:59 +0800197 logp2(ss, lvl, file, line, 0, "unknown ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100198}
199
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200200static int handle_udp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100201{
202 struct ipa_bts_conn *ipbc = bfd->data;
203 struct ipa_proxy_conn *other_conn = NULL;
204 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP UDP");
205 struct ipaccess_head *hh;
206 int ret;
207
208 /* with UDP sockets, we cannot read partial packets but have to read
209 * all of it in one go */
210 hh = (struct ipaccess_head *) msg->data;
211 ret = recv(bfd->fd, msg->data, msg->data_len, 0);
212 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100213 if (errno != EAGAIN)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200214 LOGP(DLINP, LOGL_ERROR, "recv error %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100215 msgb_free(msg);
216 return ret;
217 }
218 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200219 DEBUGP(DLINP, "UDP peer disappeared, dead socket\n");
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200220 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100221 close(bfd->fd);
222 bfd->fd = -1;
223 msgb_free(msg);
224 return -EIO;
225 }
226 if (ret < sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200227 DEBUGP(DLINP, "could not even read header!?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100228 msgb_free(msg);
229 return -EIO;
230 }
231 msgb_put(msg, ret);
232 msg->l2h = msg->data + sizeof(*hh);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200233 DEBUGP(DLMI, "UDP RX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100234
235 if (hh->len != msg->len - sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200236 DEBUGP(DLINP, "length (%u/%u) disagrees with header(%u)\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100237 msg->len, msg->len - 3, hh->len);
238 msgb_free(msg);
239 return -EIO;
240 }
241
242 switch (bfd->priv_nr & 0xff) {
243 case UDP_TO_BTS:
244 /* injection towards BTS */
245 switch (hh->proto) {
246 case IPAC_PROTO_RSL:
247 /* FIXME: what to do about TRX > 0 */
248 other_conn = ipbc->rsl_conn[0];
249 break;
250 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200251 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100252 "OML FD\n", hh->proto);
253 /* fall through */
254 case IPAC_PROTO_IPACCESS:
255 case IPAC_PROTO_OML:
256 other_conn = ipbc->oml_conn;
257 break;
258 }
259 break;
260 case UDP_TO_BSC:
261 /* injection towards BSC */
262 switch (hh->proto) {
263 case IPAC_PROTO_RSL:
264 /* FIXME: what to do about TRX > 0 */
265 other_conn = ipbc->bsc_rsl_conn[0];
266 break;
267 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200268 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100269 "OML FD\n", hh->proto);
Holger Hans Peter Freyther4e5f93c2014-06-12 11:32:25 +0200270 /* fall through */
Harald Welte2ca7c312009-12-23 22:44:04 +0100271 case IPAC_PROTO_IPACCESS:
272 case IPAC_PROTO_OML:
273 other_conn = ipbc->bsc_oml_conn;
274 break;
275 }
276 break;
277 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200278 DEBUGP(DLINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr);
Harald Welte2ca7c312009-12-23 22:44:04 +0100279 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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200292static int handle_udp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100293{
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 */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200301static int udp_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100302{
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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200314static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_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
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200328 DEBUGP(DLINP, "(%u/%u/%u) New BTS connection: ",
Harald Welte2ca7c312009-12-23 22:44:04 +0100329 site_id, bts_id, trx_id);
330
331 /* OML needs to be established before RSL */
332 if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200333 DEBUGPC(DLINP, "Not a OML connection ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100334 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
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200344 DEBUGPC(DLINP, "Created BTS Conn data structure\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100345 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
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200365 DEBUGP(DLINP, "(%u/%u/%u) OML Connected to BSC\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100366 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;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200374 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100375 "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;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200383 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100384 "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) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200399 LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800400 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);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200405 LOGP(DLINP, LOGL_NOTICE,
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800406 "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:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200418 osmo_fd_unregister(&ipbc->udp_bts_fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100419err_udp_bts:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200420 osmo_fd_unregister(&ipbc->bsc_oml_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100421 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
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200428 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100429 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,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200437 struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100438{
439 struct tlv_parsed tlvp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200440 uint8_t msg_type = *(msg->l2h);
Harald Welteeb623012014-08-18 22:33:12 +0200441 struct ipaccess_unit unit_data;
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:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200450 DEBUGP(DLMI, "PONG!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100451 break;
452 case IPAC_MSGT_ID_RESP:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200453 DEBUGP(DLMI, "ID_RESP ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100454 /* 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);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200457 DEBUGP(DLMI, "\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100458
459 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200460 LOGP(DLINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100461 return -EIO;
462 }
463
464 /* lookup BTS, create sign_link, ... */
Harald Welteeb623012014-08-18 22:33:12 +0200465 memset(&unit_data, 0, sizeof(unit_data));
Pablo Neira Ayusoc281b4e2011-04-07 14:15:20 +0200466 ipaccess_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
Harald Welteeb623012014-08-18 22:33:12 +0200467 &unit_data);
468 ipbc = find_bts_by_unitid(ipp, unit_data.site_id, unit_data.bts_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100469 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
Harald Welteeb623012014-08-18 22:33:12 +0200475 return ipbc_alloc_connect(ipc, bfd, unit_data.site_id,
476 unit_data.bts_id,
477 unit_data.trx_id, &tlvp, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100478 /* if this fails, the caller will clean up bfd */
479 } else {
480 struct sockaddr_in sin;
481 memset(&sin, 0, sizeof(sin));
482 sin.sin_family = AF_INET;
483 inet_aton(bsc_ipaddr, &sin.sin_addr);
484
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200485 DEBUGP(DLINP, "Identified BTS %u/%u/%u\n",
Harald Welteeb623012014-08-18 22:33:12 +0200486 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100487
488 if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200489 LOGP(DLINP, LOGL_ERROR, "Second OML connection from "
Harald Welte2ca7c312009-12-23 22:44:04 +0100490 "same BTS ?!?\n");
491 return 0;
492 }
493
Harald Welteeb623012014-08-18 22:33:12 +0200494 if (unit_data.trx_id >= MAX_TRX) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200495 LOGP(DLINP, LOGL_ERROR, "We don't support more "
Harald Welte2ca7c312009-12-23 22:44:04 +0100496 "than %u TRX\n", MAX_TRX);
497 return -EINVAL;
498 }
499
500 ipc->bts_conn = ipbc;
501 /* store TRX number in higher 8 bit of the bfd private number */
Harald Welteeb623012014-08-18 22:33:12 +0200502 bfd->priv_nr |= unit_data.trx_id << 8;
503 ipbc->rsl_conn[unit_data.trx_id] = ipc;
Harald Welte2ca7c312009-12-23 22:44:04 +0100504
505 /* Create RSL TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100506 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welteeb623012014-08-18 22:33:12 +0200507 ipbc->bsc_rsl_conn[unit_data.trx_id] =
508 connect_bsc(&sin, RSL_TO_BSC | (unit_data.trx_id << 8), ipbc);
Harald Welte2ca7c312009-12-23 22:44:04 +0100509 if (!ipbc->bsc_oml_conn)
510 return -EIO;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200511 DEBUGP(DLINP, "(%u/%u/%u) Connected RSL to BSC\n",
Harald Welteeb623012014-08-18 22:33:12 +0200512 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100513 }
514 break;
515 case IPAC_MSGT_ID_GET:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200516 DEBUGP(DLMI, "ID_GET\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100517 if ((bfd->priv_nr & 0xff) != OML_TO_BSC &&
518 (bfd->priv_nr & 0xff) != RSL_TO_BSC) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200519 DEBUGP(DLINP, "IDentity REQuest from BTS ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100520 return -EIO;
521 }
522 ipbc = ipc->bts_conn;
523 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200524 DEBUGP(DLINP, "ID_GET from BSC before we have ID_RESP from BTS\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100525 return -EIO;
526 }
527 ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len);
528 break;
529 case IPAC_MSGT_ID_ACK:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200530 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200531 ret = ipaccess_send_id_ack(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100532 break;
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800533 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200534 LOGP(DLMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800535 return 1;
536 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100537 }
538 return 0;
539}
540
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200541struct msgb *ipaccess_proxy_read_msg(struct osmo_fd *bfd, int *error)
Harald Welte2ca7c312009-12-23 22:44:04 +0100542{
543 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
544 struct ipaccess_head *hh;
545 int len, ret = 0;
546
547 if (!msg) {
548 *error = -ENOMEM;
549 return NULL;
550 }
551
552 /* first read our 3-byte header */
553 hh = (struct ipaccess_head *) msg->data;
554 ret = recv(bfd->fd, msg->data, 3, 0);
555 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100556 if (errno != EAGAIN)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200557 LOGP(DLINP, LOGL_ERROR, "recv error: %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100558 msgb_free(msg);
559 *error = ret;
560 return NULL;
561 } else if (ret == 0) {
562 msgb_free(msg);
563 *error = ret;
564 return NULL;
565 }
566
567 msgb_put(msg, ret);
568
569 /* then read te length as specified in header */
570 msg->l2h = msg->data + sizeof(*hh);
571 len = ntohs(hh->len);
572 ret = recv(bfd->fd, msg->l2h, len, 0);
573 if (ret < len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200574 LOGP(DLINP, LOGL_ERROR, "short read!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100575 msgb_free(msg);
576 *error = -EIO;
577 return NULL;
578 }
579 msgb_put(msg, ret);
580
581 return msg;
582}
583
584static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc,
585 unsigned int priv_nr)
586{
587 struct ipa_proxy_conn *bsc_conn;
588 unsigned int trx_id = priv_nr >> 8;
589
590 switch (priv_nr & 0xff) {
591 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
592 bsc_conn = ipbc->bsc_oml_conn;
593 break;
594 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
595 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
596 break;
597 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
598 bsc_conn = ipbc->oml_conn;
599 break;
600 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
601 bsc_conn = ipbc->rsl_conn[trx_id];
602 break;
603 default:
604 bsc_conn = NULL;
605 break;
606 }
607 return bsc_conn;
608}
609
610static void reconn_tmr_cb(void *data)
611{
612 struct ipa_proxy *ipp = data;
613 struct ipa_bts_conn *ipbc;
614 struct sockaddr_in sin;
615 int i;
616
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200617 DEBUGP(DLINP, "Running reconnect timer\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100618
619 memset(&sin, 0, sizeof(sin));
620 sin.sin_family = AF_INET;
621 inet_aton(bsc_ipaddr, &sin.sin_addr);
622
623 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
624 /* if OML to BSC is dead, try to restore it */
625 if (ipbc->oml_conn && !ipbc->bsc_oml_conn) {
Harald Welte87ed5cd2009-12-23 22:47:53 +0100626 sin.sin_port = htons(IPA_TCP_PORT_OML);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200627 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
628 LOGPC(DLINP, LOGL_NOTICE, "OML Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100629 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
630 if (!ipbc->bsc_oml_conn)
631 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200632 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
633 LOGPC(DLINP, LOGL_NOTICE, "OML Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100634 }
635 /* if we (still) don't have a OML connection, skip RSL */
636 if (!ipbc->oml_conn || !ipbc->bsc_oml_conn)
637 continue;
638
639 for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) {
640 unsigned int priv_nr;
641 /* don't establish RSL links which we don't have */
642 if (!ipbc->rsl_conn[i])
643 continue;
644 if (ipbc->bsc_rsl_conn[i])
645 continue;
646 priv_nr = ipbc->rsl_conn[i]->fd.priv_nr;
647 priv_nr &= ~0xff;
648 priv_nr |= RSL_TO_BSC;
Harald Welte87ed5cd2009-12-23 22:47:53 +0100649 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200650 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
651 LOGPC(DLINP, LOGL_NOTICE, "RSL Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100652 ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc);
Holger Hans Peter Freytherc9251fa2013-07-14 08:47:06 +0200653 if (!ipbc->bsc_rsl_conn[i])
Harald Welte2ca7c312009-12-23 22:44:04 +0100654 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200655 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
656 LOGPC(DLINP, LOGL_NOTICE, "RSL Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100657 }
658 }
659 return;
660
661reschedule:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200662 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100663}
664
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200665static void handle_dead_socket(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100666{
667 struct ipa_proxy_conn *ipc = bfd->data; /* local conn */
668 struct ipa_proxy_conn *bsc_conn; /* remote conn */
669 struct ipa_bts_conn *ipbc = ipc->bts_conn;
670 unsigned int trx_id = bfd->priv_nr >> 8;
671 struct msgb *msg, *msg2;
672
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200673 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100674 close(bfd->fd);
675 bfd->fd = -1;
676
677 /* FIXME: clear tx_queue, remove all references, etc. */
678 llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list)
679 msgb_free(msg);
680
681 switch (bfd->priv_nr & 0xff) {
682 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
Pablo Neira Ayuso3c409c22011-03-27 21:31:48 +0200683 /* The BTS started a connection with us but we got no
684 * IPAC_MSGT_ID_RESP message yet, in that scenario we did not
685 * allocate the ipa_bts_conn structure. */
686 if (ipbc == NULL)
687 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100688 ipbc->oml_conn = NULL;
689 bsc_conn = ipbc->bsc_oml_conn;
690 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200691 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100692 close(bsc_conn->fd.fd);
693 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
694 msgb_free(msg);
695 talloc_free(bsc_conn);
696 ipbc->bsc_oml_conn = NULL;
697 /* FIXME: do we need to delete the entire ipbc ? */
698 break;
699 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
700 ipbc->rsl_conn[trx_id] = NULL;
701 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
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_rsl_conn[trx_id] = NULL;
709 break;
710 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
711 ipbc->bsc_oml_conn = NULL;
712 bsc_conn = ipbc->oml_conn;
713 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200714 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100715 break;
716 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
717 ipbc->bsc_rsl_conn[trx_id] = NULL;
718 bsc_conn = ipbc->rsl_conn[trx_id];
719 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200720 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100721 break;
722 default:
723 bsc_conn = NULL;
724 break;
725 }
726
727 talloc_free(ipc);
728}
729
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800730static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg)
731{
732 uint8_t *nsvci;
733
734 if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC)
735 return;
736
737 if (msgb_l2len(msg) != 39)
738 return;
739
740 /*
741 * Check if this is a IPA Set Attribute or IPA Set Attribute ACK
742 * and if the FOM Class is GPRS NSVC0 and then we will patch it.
743 *
744 * The patch assumes the message looks like the one from the trace
745 * but we only match messages with a specific size anyway... So
746 * this hack should work just fine.
747 */
748
749 if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
750 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
751 msg->l2h[18] == 0xf5 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800752 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200753 ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8);
754 ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10);
755 *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port);
756 *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800757 } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
758 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
759 msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800760 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200761 *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port;
762 *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800763 }
764}
765
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200766static int handle_tcp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100767{
768 struct ipa_proxy_conn *ipc = bfd->data;
769 struct ipa_bts_conn *ipbc = ipc->bts_conn;
770 struct ipa_proxy_conn *bsc_conn;
Harald Weltea16ef3d2009-12-23 23:35:51 +0100771 struct msgb *msg;
Harald Welte2ca7c312009-12-23 22:44:04 +0100772 struct ipaccess_head *hh;
773 int ret = 0;
774 char *btsbsc;
775
Harald Welte2ca7c312009-12-23 22:44:04 +0100776 if ((bfd->priv_nr & 0xff) <= 2)
777 btsbsc = "BTS";
778 else
779 btsbsc = "BSC";
780
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200781 msg = ipaccess_proxy_read_msg(bfd, &ret);
Harald Welte2ca7c312009-12-23 22:44:04 +0100782 if (!msg) {
783 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200784 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
785 LOGPC(DLINP, LOGL_NOTICE, "%s disappeared, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100786 "dead socket\n", btsbsc);
787 handle_dead_socket(bfd);
788 }
789 return ret;
790 }
791
792 msgb_put(msg, ret);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200793 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
794 DEBUGPC(DLMI, "RX<-%s: %s\n", btsbsc, osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100795
796 hh = (struct ipaccess_head *) msg->data;
797 if (hh->proto == IPAC_PROTO_IPACCESS) {
798 ret = ipaccess_rcvmsg(ipc, msg, bfd);
799 if (ret < 0) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200800 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100801 close(bfd->fd);
802 bfd->fd = -1;
803 talloc_free(bfd);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800804 msgb_free(msg);
805 return ret;
806 } else if (ret == 0) {
807 /* we do not forward parts of the CCM protocol
808 * through the proxy but rather terminate it ourselves. */
809 msgb_free(msg);
810 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100811 }
Harald Welte2ca7c312009-12-23 22:44:04 +0100812 }
813
814 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200815 LOGP(DLINP, LOGL_ERROR,
Harald Welte2ca7c312009-12-23 22:44:04 +0100816 "received %s packet but no ipc->bts_conn?!?\n", btsbsc);
817 msgb_free(msg);
818 return -EIO;
819 }
820
821 bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr);
822 if (bsc_conn) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800823 if (gprs_ns_ipaddr)
824 patch_gprs_msg(ipbc, bfd->priv_nr, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100825 /* enqueue packet towards BSC */
826 msgb_enqueue(&bsc_conn->tx_queue, msg);
827 /* mark respective filedescriptor as 'we want to write' */
828 bsc_conn->fd.when |= BSC_FD_WRITE;
829 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200830 logp_ipbc_uid(DLINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8);
831 LOGPC(DLINP, LOGL_INFO, "Dropping packet from %s, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100832 "since remote connection is dead\n", btsbsc);
833 msgb_free(msg);
834 }
835
836 return ret;
837}
838
839/* a TCP socket is ready to be written to */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200840static int handle_tcp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100841{
842 struct ipa_proxy_conn *ipc = bfd->data;
843 struct ipa_bts_conn *ipbc = ipc->bts_conn;
844 struct llist_head *lh;
845 struct msgb *msg;
846 char *btsbsc;
847 int ret;
848
849 if ((bfd->priv_nr & 0xff) <= 2)
850 btsbsc = "BTS";
851 else
852 btsbsc = "BSC";
853
854
855 /* get the next msg for this timeslot */
856 if (llist_empty(&ipc->tx_queue)) {
857 bfd->when &= ~BSC_FD_WRITE;
858 return 0;
859 }
860 lh = ipc->tx_queue.next;
861 llist_del(lh);
862 msg = llist_entry(lh, struct msgb, list);
863
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200864 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
865 DEBUGPC(DLMI, "TX %04x: %s\n", bfd->priv_nr,
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200866 osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100867
868 ret = send(bfd->fd, msg->data, msg->len, 0);
869 msgb_free(msg);
870
871 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200872 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
873 LOGP(DLINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc);
Harald Welte2ca7c312009-12-23 22:44:04 +0100874 handle_dead_socket(bfd);
875 }
876
877 return ret;
878}
879
880/* callback from select.c in case one of the fd's can be read/written */
Harald Welteeb623012014-08-18 22:33:12 +0200881static int proxy_ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100882{
883 int rc = 0;
884
885 if (what & BSC_FD_READ) {
886 rc = handle_tcp_read(bfd);
887 if (rc < 0)
888 return rc;
889 }
890 if (what & BSC_FD_WRITE)
891 rc = handle_tcp_write(bfd);
892
893 return rc;
894}
895
896/* callback of the listening filedescriptor */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200897static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100898{
899 int ret;
900 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200901 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100902 struct sockaddr_in sa;
903 socklen_t sa_len = sizeof(sa);
904
905 if (!(what & BSC_FD_READ))
906 return 0;
907
908 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
909 if (ret < 0) {
910 perror("accept");
911 return ret;
912 }
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200913 DEBUGP(DLINP, "accept()ed new %s link from %s\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100914 (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL",
915 inet_ntoa(sa.sin_addr));
916
917 ipc = alloc_conn();
918 if (!ipc) {
919 close(ret);
920 return -ENOMEM;
921 }
922
923 bfd = &ipc->fd;
924 bfd->fd = ret;
925 bfd->data = ipc;
926 bfd->priv_nr = listen_bfd->priv_nr;
Harald Welteeb623012014-08-18 22:33:12 +0200927 bfd->cb = proxy_ipaccess_fd_cb;
Harald Welte2ca7c312009-12-23 22:44:04 +0100928 bfd->when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200929 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100930 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200931 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100932 close(bfd->fd);
933 talloc_free(ipc);
934 return ret;
935 }
936
937 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200938 ret = ipaccess_send_id_req(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100939
940 return 0;
941}
942
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800943static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port)
944{
945 int ret;
946 struct sockaddr_in addr;
947 socklen_t len = sizeof(addr);
948 memset(&addr, 0, sizeof(addr));
949
950 addr.sin_family = AF_INET;
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800951 addr.sin_port = htons(port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800952 addr.sin_addr = ip;
953
954 ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len);
955 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200956 LOGP(DLINP, LOGL_ERROR, "Failed to forward GPRS message.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800957 }
958}
959
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200960static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800961{
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800962 struct ipa_bts_conn *bts;
963 char buf[4096];
964 int ret;
965 struct sockaddr_in sock;
966 socklen_t len = sizeof(sock);
967
968 /* 1. get the data... */
969 ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len);
970 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200971 LOGP(DLINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800972 return -1;
973 }
974
975 bts = bfd->data;
976
977 /* 2. figure out where to send it to */
978 if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200979 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from network.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800980 send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000);
981 } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200982 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800983 send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000);
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800984 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200985 LOGP(DLINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800986 }
987
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800988 return 0;
989}
990
Harald Welte2ca7c312009-12-23 22:44:04 +0100991/* Actively connect to a BSC. */
992static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data)
993{
994 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200995 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100996 int ret, on = 1;
997
998 ipc = alloc_conn();
999 if (!ipc)
1000 return NULL;
1001
1002 ipc->bts_conn = data;
1003
1004 bfd = &ipc->fd;
1005 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1006 bfd->cb = ipaccess_fd_cb;
1007 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
1008 bfd->data = ipc;
1009 bfd->priv_nr = priv_nr;
1010
Holger Hans Peter Freythere18209c2013-12-12 16:16:35 +01001011 if (bfd->fd < 0) {
1012 LOGP(DLINP, LOGL_ERROR, "Could not create socket: %s\n",
1013 strerror(errno));
1014 talloc_free(ipc);
1015 return NULL;
1016 }
1017
Harald Welte2ca7c312009-12-23 22:44:04 +01001018 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1019
1020 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
1021 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001022 LOGP(DLINP, LOGL_ERROR, "Could not connect socket: %s\n",
Holger Hans Peter Freyther6cb9b232010-06-09 15:15:11 +08001023 inet_ntoa(sa->sin_addr));
Harald Welte2ca7c312009-12-23 22:44:04 +01001024 close(bfd->fd);
1025 talloc_free(ipc);
1026 return NULL;
1027 }
1028
1029 /* pre-fill tx_queue with identity request */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001030 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +01001031 if (ret < 0) {
1032 close(bfd->fd);
1033 talloc_free(ipc);
1034 return NULL;
1035 }
1036
1037 return ipc;
1038}
1039
1040static int ipaccess_proxy_setup(void)
1041{
1042 int ret;
1043
1044 ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy);
1045 if (!ipp)
1046 return -ENOMEM;
1047 INIT_LLIST_HEAD(&ipp->bts_list);
1048 ipp->reconn_timer.cb = reconn_tmr_cb;
1049 ipp->reconn_timer.data = ipp;
1050
1051 /* Listen for OML connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001052 ret = make_sock(&ipp->oml_listen_fd, IPPROTO_TCP, INADDR_ANY,
1053 IPA_TCP_PORT_OML, OML_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001054 if (ret < 0)
1055 return ret;
1056
1057 /* Listen for RSL connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001058 ret = make_sock(&ipp->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY,
1059 IPA_TCP_PORT_RSL, RSL_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001060
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001061 if (ret < 0)
1062 return ret;
1063
1064 /* Connect the GPRS NS Socket */
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001065 if (gprs_ns_ipaddr) {
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +08001066 inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr);
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001067 inet_aton(listen_ipaddr, &ipp->listen_addr);
1068 }
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001069
Harald Welte2ca7c312009-12-23 22:44:04 +01001070 return ret;
1071}
1072
1073static void signal_handler(int signal)
1074{
1075 fprintf(stdout, "signal %u received\n", signal);
1076
1077 switch (signal) {
1078 case SIGABRT:
1079 /* in case of abort, we want to obtain a talloc report
1080 * and then return to the caller, who will abort the process */
1081 case SIGUSR1:
1082 talloc_report_full(tall_bsc_ctx, stderr);
1083 break;
1084 default:
1085 break;
1086 }
1087}
1088
Harald Welted4ab13b2011-07-16 13:39:44 +02001089static void print_help(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001090{
1091 printf(" ipaccess-proxy is a proxy BTS.\n");
1092 printf(" -h --help. This help text.\n");
1093 printf(" -l --listen IP. The ip to listen to.\n");
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001094 printf(" -b --bsc IP. The BSC IP address.\n");
1095 printf(" -g --gprs IP. Take GPRS NS from that IP.\n");
1096 printf("\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001097 printf(" -s --disable-color. Disable the color inside the logging message.\n");
1098 printf(" -e --log-level number. Set the global loglevel.\n");
1099 printf(" -T --timestamp. Prefix every log message with a timestamp.\n");
1100 printf(" -V --version. Print the version of OpenBSC.\n");
1101}
1102
Harald Welted4ab13b2011-07-16 13:39:44 +02001103static void print_usage(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001104{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001105 printf("Usage: ipaccess-proxy [options]\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001106}
1107
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001108enum {
1109 IPA_PROXY_OPT_LISTEN_NONE = 0,
1110 IPA_PROXY_OPT_LISTEN_IP = (1 << 0),
1111 IPA_PROXY_OPT_BSC_IP = (1 << 1),
1112};
1113
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001114static void handle_options(int argc, char** argv)
1115{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001116 int options_mask = 0;
1117
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001118 /* disable explicit missing arguments error output from getopt_long */
1119 opterr = 0;
1120
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001121 while (1) {
1122 int option_index = 0, c;
1123 static struct option long_options[] = {
1124 {"help", 0, 0, 'h'},
1125 {"disable-color", 0, 0, 's'},
1126 {"timestamp", 0, 0, 'T'},
1127 {"log-level", 1, 0, 'e'},
1128 {"listen", 1, 0, 'l'},
1129 {"bsc", 1, 0, 'b'},
1130 {0, 0, 0, 0}
1131 };
1132
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001133 c = getopt_long(argc, argv, "hsTe:l:b:g:",
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001134 long_options, &option_index);
1135 if (c == -1)
1136 break;
1137
1138 switch (c) {
1139 case 'h':
1140 print_usage();
1141 print_help();
1142 exit(0);
1143 case 'l':
1144 listen_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001145 options_mask |= IPA_PROXY_OPT_LISTEN_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001146 break;
1147 case 'b':
1148 bsc_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001149 options_mask |= IPA_PROXY_OPT_BSC_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001150 break;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001151 case 'g':
1152 gprs_ns_ipaddr = optarg;
1153 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001154 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001155 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001156 break;
1157 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001158 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001159 break;
1160 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001161 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001162 break;
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001163 case '?':
1164 if (optopt) {
1165 printf("ERROR: missing mandatory argument "
1166 "for `%s' option\n", argv[optind-1]);
1167 } else {
1168 printf("ERROR: unknown option `%s'\n",
1169 argv[optind-1]);
1170 }
1171 print_usage();
1172 print_help();
1173 exit(EXIT_FAILURE);
1174 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001175 default:
1176 /* ignore */
1177 break;
1178 }
1179 }
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001180 if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP))
1181 != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) {
1182 printf("ERROR: You have to specify `--listen' and `--bsc' "
1183 "options at least.\n");
1184 print_usage();
1185 print_help();
1186 exit(EXIT_FAILURE);
1187 }
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001188}
1189
Harald Welte2ca7c312009-12-23 22:44:04 +01001190int main(int argc, char **argv)
1191{
1192 int rc;
1193
Harald Welte2ca7c312009-12-23 22:44:04 +01001194 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
1195
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001196 osmo_init_logging(&log_info);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001197 log_parse_category_mask(osmo_stderr_target, "DLINP:DLMI");
Harald Welte2ca7c312009-12-23 22:44:04 +01001198
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001199 handle_options(argc, argv);
1200
Harald Welte2ca7c312009-12-23 22:44:04 +01001201 rc = ipaccess_proxy_setup();
1202 if (rc < 0)
1203 exit(1);
1204
1205 signal(SIGUSR1, &signal_handler);
1206 signal(SIGABRT, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001207 osmo_init_ignore_signals();
Harald Welte2ca7c312009-12-23 22:44:04 +01001208
1209 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001210 osmo_select_main(0);
Harald Welte2ca7c312009-12-23 22:44:04 +01001211 }
1212}