blob: d3674426c22fd9fa8c976ff67117109dbf5118c0 [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 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>
Harald Welte2ca7c312009-12-23 22:44:04 +010048#include <openbsc/debug.h>
49#include <openbsc/ipaccess.h>
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +020050#include <openbsc/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
123/* some of the code against we link from OpenBSC needs this */
124void *tall_bsc_ctx;
125
126static char *listen_ipaddr;
127static char *bsc_ipaddr;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800128static char *gprs_ns_ipaddr;
Harald Welte2ca7c312009-12-23 22:44:04 +0100129
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200130static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800131
Holger Hans Peter Freyther3dac8812010-06-09 14:39:22 +0800132#define PROXY_ALLOC_SIZE 1200
Harald Welte2ca7c312009-12-23 22:44:04 +0100133
Harald Welte2ca7c312009-12-23 22:44:04 +0100134static struct ipa_bts_conn *find_bts_by_unitid(struct ipa_proxy *ipp,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200135 uint16_t site_id,
136 uint16_t bts_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100137{
138 struct ipa_bts_conn *ipbc;
139
140 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
141 if (ipbc->unit_id.site_id == site_id &&
142 ipbc->unit_id.bts_id == bts_id)
143 return ipbc;
144 }
145
146 return NULL;
147}
148
149struct ipa_proxy_conn *alloc_conn(void)
150{
151 struct ipa_proxy_conn *ipc;
152
153 ipc = talloc_zero(tall_bsc_ctx, struct ipa_proxy_conn);
154 if (!ipc)
155 return NULL;
156
157 INIT_LLIST_HEAD(&ipc->tx_queue);
158
159 return ipc;
160}
161
162static int store_idtags(struct ipa_bts_conn *ipbc, struct tlv_parsed *tlvp)
163{
164 unsigned int i, len;
165
166 for (i = 0; i <= 0xff; i++) {
167 if (!TLVP_PRESENT(tlvp, i))
168 continue;
169
170 len = TLVP_LEN(tlvp, i);
171#if 0
172 if (!ipbc->id_tags[i])
173 ipbc->id_tags[i] = talloc_size(tall_bsc_ctx, len);
174 else
175#endif
Harald Weltea16ef3d2009-12-23 23:35:51 +0100176 ipbc->id_tags[i] = talloc_realloc_size(ipbc,
Harald Welte2ca7c312009-12-23 22:44:04 +0100177 ipbc->id_tags[i], len);
178 if (!ipbc->id_tags[i])
179 return -ENOMEM;
180
181 memset(ipbc->id_tags[i], 0, len);
182 //memcpy(ipbc->id_tags[i], TLVP_VAL(tlvp, i), len);
183 }
184 return 0;
185}
186
187
188static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data);
189
190#define logp_ipbc_uid(ss, lvl, ipbc, trx_id) _logp_ipbc_uid(ss, lvl, __FILE__, __LINE__, ipbc, trx_id)
191
192static void _logp_ipbc_uid(unsigned int ss, unsigned int lvl, char *file, int line,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200193 struct ipa_bts_conn *ipbc, uint8_t trx_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100194{
195 if (ipbc)
Harald Weltedc5062b2010-03-26 21:28:59 +0800196 logp2(ss, lvl, file, line, 0, "(%u/%u/%u) ", ipbc->unit_id.site_id,
Harald Welte2ca7c312009-12-23 22:44:04 +0100197 ipbc->unit_id.bts_id, trx_id);
198 else
Harald Weltedc5062b2010-03-26 21:28:59 +0800199 logp2(ss, lvl, file, line, 0, "unknown ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100200}
201
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200202static int handle_udp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100203{
204 struct ipa_bts_conn *ipbc = bfd->data;
205 struct ipa_proxy_conn *other_conn = NULL;
206 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP UDP");
207 struct ipaccess_head *hh;
208 int ret;
209
210 /* with UDP sockets, we cannot read partial packets but have to read
211 * all of it in one go */
212 hh = (struct ipaccess_head *) msg->data;
213 ret = recv(bfd->fd, msg->data, msg->data_len, 0);
214 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100215 if (errno != EAGAIN)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200216 LOGP(DLINP, LOGL_ERROR, "recv error %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100217 msgb_free(msg);
218 return ret;
219 }
220 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200221 DEBUGP(DLINP, "UDP peer disappeared, dead socket\n");
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200222 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100223 close(bfd->fd);
224 bfd->fd = -1;
225 msgb_free(msg);
226 return -EIO;
227 }
228 if (ret < sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200229 DEBUGP(DLINP, "could not even read header!?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100230 msgb_free(msg);
231 return -EIO;
232 }
233 msgb_put(msg, ret);
234 msg->l2h = msg->data + sizeof(*hh);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200235 DEBUGP(DLMI, "UDP RX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100236
237 if (hh->len != msg->len - sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200238 DEBUGP(DLINP, "length (%u/%u) disagrees with header(%u)\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100239 msg->len, msg->len - 3, hh->len);
240 msgb_free(msg);
241 return -EIO;
242 }
243
244 switch (bfd->priv_nr & 0xff) {
245 case UDP_TO_BTS:
246 /* injection towards BTS */
247 switch (hh->proto) {
248 case IPAC_PROTO_RSL:
249 /* FIXME: what to do about TRX > 0 */
250 other_conn = ipbc->rsl_conn[0];
251 break;
252 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200253 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100254 "OML FD\n", hh->proto);
255 /* fall through */
256 case IPAC_PROTO_IPACCESS:
257 case IPAC_PROTO_OML:
258 other_conn = ipbc->oml_conn;
259 break;
260 }
261 break;
262 case UDP_TO_BSC:
263 /* injection towards BSC */
264 switch (hh->proto) {
265 case IPAC_PROTO_RSL:
266 /* FIXME: what to do about TRX > 0 */
267 other_conn = ipbc->bsc_rsl_conn[0];
268 break;
269 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200270 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100271 "OML FD\n", hh->proto);
Holger Hans Peter Freyther4e5f93c2014-06-12 11:32:25 +0200272 /* fall through */
Harald Welte2ca7c312009-12-23 22:44:04 +0100273 case IPAC_PROTO_IPACCESS:
274 case IPAC_PROTO_OML:
275 other_conn = ipbc->bsc_oml_conn;
276 break;
277 }
278 break;
279 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200280 DEBUGP(DLINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr);
Harald Welte2ca7c312009-12-23 22:44:04 +0100281 break;
282 }
283
284 if (other_conn) {
285 /* enqueue the message for TX on the respective FD */
286 msgb_enqueue(&other_conn->tx_queue, msg);
287 other_conn->fd.when |= BSC_FD_WRITE;
288 } else
289 msgb_free(msg);
290
291 return 0;
292}
293
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200294static int handle_udp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100295{
296 /* not implemented yet */
297 bfd->when &= ~BSC_FD_WRITE;
298
299 return -EIO;
300}
301
302/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200303static int udp_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100304{
305 int rc = 0;
306
307 if (what & BSC_FD_READ)
308 rc = handle_udp_read(bfd);
309 if (what & BSC_FD_WRITE)
310 rc = handle_udp_write(bfd);
311
312 return rc;
313}
314
315
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200316static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200317 uint16_t site_id, uint16_t bts_id,
318 uint16_t trx_id, struct tlv_parsed *tlvp,
Harald Welte2ca7c312009-12-23 22:44:04 +0100319 struct msgb *msg)
320{
321 struct ipa_bts_conn *ipbc;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200322 uint16_t udp_port;
Harald Welte2ca7c312009-12-23 22:44:04 +0100323 int ret = 0;
324 struct sockaddr_in sin;
325
326 memset(&sin, 0, sizeof(sin));
327 sin.sin_family = AF_INET;
328 inet_aton(bsc_ipaddr, &sin.sin_addr);
329
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200330 DEBUGP(DLINP, "(%u/%u/%u) New BTS connection: ",
Harald Welte2ca7c312009-12-23 22:44:04 +0100331 site_id, bts_id, trx_id);
332
333 /* OML needs to be established before RSL */
334 if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200335 DEBUGPC(DLINP, "Not a OML connection ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100336 return -EIO;
337 }
338
339 /* allocate new BTS connection data structure */
340 ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn);
341 if (!ipbc) {
342 ret = -ENOMEM;
343 goto err_out;
344 }
345
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200346 DEBUGPC(DLINP, "Created BTS Conn data structure\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100347 ipbc->ipp = ipp;
348 ipbc->unit_id.site_id = site_id;
349 ipbc->unit_id.bts_id = bts_id;
350 ipbc->oml_conn = ipc;
351 ipc->bts_conn = ipbc;
352
353 /* store the content of the ID TAGS for later reference */
354 store_idtags(ipbc, tlvp);
355 ipbc->id_resp_len = msg->len;
356 ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len);
357 memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len);
358
359 /* Create OML TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100360 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100361 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
362 if (!ipbc->bsc_oml_conn) {
363 ret = -EIO;
364 goto err_bsc_conn;
365 }
366
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200367 DEBUGP(DLINP, "(%u/%u/%u) OML Connected to BSC\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100368 site_id, bts_id, trx_id);
369
370 /* Create UDP socket for BTS packet injection */
371 udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200372 ret = make_sock(&ipbc->udp_bts_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100373 UDP_TO_BTS, udp_fd_cb, ipbc);
374 if (ret < 0)
375 goto err_udp_bts;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200376 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100377 "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port);
378
379 /* Create UDP socket for BSC packet injection */
380 udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200381 ret = make_sock(&ipbc->udp_bsc_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100382 UDP_TO_BSC, udp_fd_cb, ipbc);
383 if (ret < 0)
384 goto err_udp_bsc;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200385 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100386 "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800387
388
389 /* GPRS NS related code */
390 if (gprs_ns_ipaddr) {
391 struct sockaddr_in sock;
392 socklen_t len = sizeof(sock);
Pablo Neira Ayuso7e737002011-04-11 16:33:17 +0200393 struct in_addr addr;
394 uint32_t ip;
395
396 inet_aton(listen_ipaddr, &addr);
397 ip = ntohl(addr.s_addr); /* make_sock() needs host byte order */
398 ret = make_sock(&ipbc->gprs_ns_fd, IPPROTO_UDP, ip, 0, 0,
399 gprs_ns_cb, ipbc);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800400 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200401 LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800402 goto err_udp_bsc;
403 }
404
405 ret = getsockname(ipbc->gprs_ns_fd.fd, (struct sockaddr* ) &sock, &len);
406 ipbc->gprs_local_port = ntohs(sock.sin_port);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200407 LOGP(DLINP, LOGL_NOTICE,
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800408 "Created GPRS NS Socket. Listening on: %s:%d\n",
409 inet_ntoa(sock.sin_addr), ipbc->gprs_local_port);
410
411 ret = getpeername(bfd->fd, (struct sockaddr* ) &sock, &len);
412 ipbc->bts_addr = sock.sin_addr;
413 }
414
Harald Welte2ca7c312009-12-23 22:44:04 +0100415 llist_add(&ipbc->list, &ipp->bts_list);
416
417 return 0;
418
419err_udp_bsc:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200420 osmo_fd_unregister(&ipbc->udp_bts_fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100421err_udp_bts:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200422 osmo_fd_unregister(&ipbc->bsc_oml_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100423 close(ipbc->bsc_oml_conn->fd.fd);
424 talloc_free(ipbc->bsc_oml_conn);
425 ipbc->bsc_oml_conn = NULL;
426err_bsc_conn:
427 talloc_free(ipbc->id_resp);
428 talloc_free(ipbc);
429#if 0
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200430 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100431 close(bfd->fd);
432 talloc_free(bfd);
433#endif
434err_out:
435 return ret;
436}
437
438static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200439 struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100440{
441 struct tlv_parsed tlvp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200442 uint8_t msg_type = *(msg->l2h);
Harald Welteeb623012014-08-18 22:33:12 +0200443 struct ipaccess_unit unit_data;
Harald Welte2ca7c312009-12-23 22:44:04 +0100444 struct ipa_bts_conn *ipbc;
445 int ret = 0;
446
447 switch (msg_type) {
448 case IPAC_MSGT_PING:
Harald Welte4a88a492014-08-20 23:46:40 +0200449 ret = ipa_ccm_send_pong(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100450 break;
451 case IPAC_MSGT_PONG:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200452 DEBUGP(DLMI, "PONG!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100453 break;
454 case IPAC_MSGT_ID_RESP:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200455 DEBUGP(DLMI, "ID_RESP ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100456 /* parse tags, search for Unit ID */
Harald Welte4a88a492014-08-20 23:46:40 +0200457 ipa_ccm_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
Pablo Neira Ayuso625295b2011-04-07 14:15:16 +0200458 msgb_l2len(msg)-2);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200459 DEBUGP(DLMI, "\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100460
461 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200462 LOGP(DLINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100463 return -EIO;
464 }
465
466 /* lookup BTS, create sign_link, ... */
Harald Welteeb623012014-08-18 22:33:12 +0200467 memset(&unit_data, 0, sizeof(unit_data));
Harald Welte4a88a492014-08-20 23:46:40 +0200468 ipa_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
Harald Welteeb623012014-08-18 22:33:12 +0200469 &unit_data);
470 ipbc = find_bts_by_unitid(ipp, unit_data.site_id, unit_data.bts_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100471 if (!ipbc) {
472 /* We have not found an ipbc (per-bts proxy instance)
473 * for this BTS yet. The first connection of a new BTS must
474 * be a OML connection. We allocate the associated data structures,
475 * and try to connect to the remote end */
476
Harald Welteeb623012014-08-18 22:33:12 +0200477 return ipbc_alloc_connect(ipc, bfd, unit_data.site_id,
478 unit_data.bts_id,
479 unit_data.trx_id, &tlvp, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100480 /* if this fails, the caller will clean up bfd */
481 } else {
482 struct sockaddr_in sin;
483 memset(&sin, 0, sizeof(sin));
484 sin.sin_family = AF_INET;
485 inet_aton(bsc_ipaddr, &sin.sin_addr);
486
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200487 DEBUGP(DLINP, "Identified BTS %u/%u/%u\n",
Harald Welteeb623012014-08-18 22:33:12 +0200488 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100489
490 if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200491 LOGP(DLINP, LOGL_ERROR, "Second OML connection from "
Harald Welte2ca7c312009-12-23 22:44:04 +0100492 "same BTS ?!?\n");
493 return 0;
494 }
495
Harald Welteeb623012014-08-18 22:33:12 +0200496 if (unit_data.trx_id >= MAX_TRX) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200497 LOGP(DLINP, LOGL_ERROR, "We don't support more "
Harald Welte2ca7c312009-12-23 22:44:04 +0100498 "than %u TRX\n", MAX_TRX);
499 return -EINVAL;
500 }
501
502 ipc->bts_conn = ipbc;
503 /* store TRX number in higher 8 bit of the bfd private number */
Harald Welteeb623012014-08-18 22:33:12 +0200504 bfd->priv_nr |= unit_data.trx_id << 8;
505 ipbc->rsl_conn[unit_data.trx_id] = ipc;
Harald Welte2ca7c312009-12-23 22:44:04 +0100506
507 /* Create RSL TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100508 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welteeb623012014-08-18 22:33:12 +0200509 ipbc->bsc_rsl_conn[unit_data.trx_id] =
510 connect_bsc(&sin, RSL_TO_BSC | (unit_data.trx_id << 8), ipbc);
Harald Welte2ca7c312009-12-23 22:44:04 +0100511 if (!ipbc->bsc_oml_conn)
512 return -EIO;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200513 DEBUGP(DLINP, "(%u/%u/%u) Connected RSL to BSC\n",
Harald Welteeb623012014-08-18 22:33:12 +0200514 unit_data.site_id, unit_data.bts_id, unit_data.trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100515 }
516 break;
517 case IPAC_MSGT_ID_GET:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200518 DEBUGP(DLMI, "ID_GET\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100519 if ((bfd->priv_nr & 0xff) != OML_TO_BSC &&
520 (bfd->priv_nr & 0xff) != RSL_TO_BSC) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200521 DEBUGP(DLINP, "IDentity REQuest from BTS ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100522 return -EIO;
523 }
524 ipbc = ipc->bts_conn;
525 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200526 DEBUGP(DLINP, "ID_GET from BSC before we have ID_RESP from BTS\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100527 return -EIO;
528 }
529 ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len);
Neels Hofmeyr127fc932016-02-24 19:29:59 +0100530 if (ret != ipbc->id_resp_len) {
531 LOGP(DLINP, LOGL_ERROR, "Partial write: %d of %d\n",
532 ret, ipbc->id_resp_len);
533 return -EIO;
534 }
535 ret = 0;
Harald Welte2ca7c312009-12-23 22:44:04 +0100536 break;
537 case IPAC_MSGT_ID_ACK:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200538 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Harald Welte4a88a492014-08-20 23:46:40 +0200539 ret = ipa_ccm_send_id_ack(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100540 break;
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800541 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200542 LOGP(DLMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800543 return 1;
544 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100545 }
Neels Hofmeyr127fc932016-02-24 19:29:59 +0100546 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100547}
548
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200549struct msgb *ipaccess_proxy_read_msg(struct osmo_fd *bfd, int *error)
Harald Welte2ca7c312009-12-23 22:44:04 +0100550{
551 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
552 struct ipaccess_head *hh;
553 int len, ret = 0;
554
555 if (!msg) {
556 *error = -ENOMEM;
557 return NULL;
558 }
559
560 /* first read our 3-byte header */
561 hh = (struct ipaccess_head *) msg->data;
562 ret = recv(bfd->fd, msg->data, 3, 0);
563 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100564 if (errno != EAGAIN)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200565 LOGP(DLINP, LOGL_ERROR, "recv error: %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100566 msgb_free(msg);
567 *error = ret;
568 return NULL;
569 } else if (ret == 0) {
570 msgb_free(msg);
571 *error = ret;
572 return NULL;
573 }
574
575 msgb_put(msg, ret);
576
577 /* then read te length as specified in header */
578 msg->l2h = msg->data + sizeof(*hh);
579 len = ntohs(hh->len);
580 ret = recv(bfd->fd, msg->l2h, len, 0);
581 if (ret < len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200582 LOGP(DLINP, LOGL_ERROR, "short read!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100583 msgb_free(msg);
584 *error = -EIO;
585 return NULL;
586 }
587 msgb_put(msg, ret);
588
589 return msg;
590}
591
592static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc,
593 unsigned int priv_nr)
594{
595 struct ipa_proxy_conn *bsc_conn;
596 unsigned int trx_id = priv_nr >> 8;
597
598 switch (priv_nr & 0xff) {
599 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
600 bsc_conn = ipbc->bsc_oml_conn;
601 break;
602 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
603 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
604 break;
605 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
606 bsc_conn = ipbc->oml_conn;
607 break;
608 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
609 bsc_conn = ipbc->rsl_conn[trx_id];
610 break;
611 default:
612 bsc_conn = NULL;
613 break;
614 }
615 return bsc_conn;
616}
617
618static void reconn_tmr_cb(void *data)
619{
620 struct ipa_proxy *ipp = data;
621 struct ipa_bts_conn *ipbc;
622 struct sockaddr_in sin;
623 int i;
624
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200625 DEBUGP(DLINP, "Running reconnect timer\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100626
627 memset(&sin, 0, sizeof(sin));
628 sin.sin_family = AF_INET;
629 inet_aton(bsc_ipaddr, &sin.sin_addr);
630
631 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
632 /* if OML to BSC is dead, try to restore it */
633 if (ipbc->oml_conn && !ipbc->bsc_oml_conn) {
Harald Welte87ed5cd2009-12-23 22:47:53 +0100634 sin.sin_port = htons(IPA_TCP_PORT_OML);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200635 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
636 LOGPC(DLINP, LOGL_NOTICE, "OML Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100637 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
638 if (!ipbc->bsc_oml_conn)
639 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200640 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
641 LOGPC(DLINP, LOGL_NOTICE, "OML Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100642 }
643 /* if we (still) don't have a OML connection, skip RSL */
644 if (!ipbc->oml_conn || !ipbc->bsc_oml_conn)
645 continue;
646
647 for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) {
648 unsigned int priv_nr;
649 /* don't establish RSL links which we don't have */
650 if (!ipbc->rsl_conn[i])
651 continue;
652 if (ipbc->bsc_rsl_conn[i])
653 continue;
654 priv_nr = ipbc->rsl_conn[i]->fd.priv_nr;
655 priv_nr &= ~0xff;
656 priv_nr |= RSL_TO_BSC;
Harald Welte87ed5cd2009-12-23 22:47:53 +0100657 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200658 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
659 LOGPC(DLINP, LOGL_NOTICE, "RSL Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100660 ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc);
Holger Hans Peter Freytherc9251fa2013-07-14 08:47:06 +0200661 if (!ipbc->bsc_rsl_conn[i])
Harald Welte2ca7c312009-12-23 22:44:04 +0100662 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200663 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
664 LOGPC(DLINP, LOGL_NOTICE, "RSL Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100665 }
666 }
667 return;
668
669reschedule:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200670 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100671}
672
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200673static void handle_dead_socket(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100674{
675 struct ipa_proxy_conn *ipc = bfd->data; /* local conn */
676 struct ipa_proxy_conn *bsc_conn; /* remote conn */
677 struct ipa_bts_conn *ipbc = ipc->bts_conn;
678 unsigned int trx_id = bfd->priv_nr >> 8;
679 struct msgb *msg, *msg2;
680
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200681 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100682 close(bfd->fd);
683 bfd->fd = -1;
684
685 /* FIXME: clear tx_queue, remove all references, etc. */
686 llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list)
687 msgb_free(msg);
688
689 switch (bfd->priv_nr & 0xff) {
690 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
Pablo Neira Ayuso3c409c22011-03-27 21:31:48 +0200691 /* The BTS started a connection with us but we got no
692 * IPAC_MSGT_ID_RESP message yet, in that scenario we did not
693 * allocate the ipa_bts_conn structure. */
694 if (ipbc == NULL)
695 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100696 ipbc->oml_conn = NULL;
697 bsc_conn = ipbc->bsc_oml_conn;
698 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200699 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100700 close(bsc_conn->fd.fd);
701 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
702 msgb_free(msg);
703 talloc_free(bsc_conn);
704 ipbc->bsc_oml_conn = NULL;
705 /* FIXME: do we need to delete the entire ipbc ? */
706 break;
707 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
708 ipbc->rsl_conn[trx_id] = NULL;
709 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
710 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200711 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100712 close(bsc_conn->fd.fd);
713 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
714 msgb_free(msg);
715 talloc_free(bsc_conn);
716 ipbc->bsc_rsl_conn[trx_id] = NULL;
717 break;
718 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
719 ipbc->bsc_oml_conn = NULL;
720 bsc_conn = ipbc->oml_conn;
721 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200722 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100723 break;
724 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
725 ipbc->bsc_rsl_conn[trx_id] = NULL;
726 bsc_conn = ipbc->rsl_conn[trx_id];
727 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200728 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100729 break;
730 default:
731 bsc_conn = NULL;
732 break;
733 }
734
735 talloc_free(ipc);
736}
737
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800738static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg)
739{
740 uint8_t *nsvci;
741
742 if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC)
743 return;
744
745 if (msgb_l2len(msg) != 39)
746 return;
747
748 /*
749 * Check if this is a IPA Set Attribute or IPA Set Attribute ACK
750 * and if the FOM Class is GPRS NSVC0 and then we will patch it.
751 *
752 * The patch assumes the message looks like the one from the trace
753 * but we only match messages with a specific size anyway... So
754 * this hack should work just fine.
755 */
756
757 if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
758 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
759 msg->l2h[18] == 0xf5 && 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 ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8);
762 ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10);
763 *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port);
764 *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800765 } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
766 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
767 msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800768 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200769 *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port;
770 *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800771 }
772}
773
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200774static int handle_tcp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100775{
776 struct ipa_proxy_conn *ipc = bfd->data;
777 struct ipa_bts_conn *ipbc = ipc->bts_conn;
778 struct ipa_proxy_conn *bsc_conn;
Harald Weltea16ef3d2009-12-23 23:35:51 +0100779 struct msgb *msg;
Harald Welte2ca7c312009-12-23 22:44:04 +0100780 struct ipaccess_head *hh;
781 int ret = 0;
782 char *btsbsc;
783
Harald Welte2ca7c312009-12-23 22:44:04 +0100784 if ((bfd->priv_nr & 0xff) <= 2)
785 btsbsc = "BTS";
786 else
787 btsbsc = "BSC";
788
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200789 msg = ipaccess_proxy_read_msg(bfd, &ret);
Harald Welte2ca7c312009-12-23 22:44:04 +0100790 if (!msg) {
791 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200792 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
793 LOGPC(DLINP, LOGL_NOTICE, "%s disappeared, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100794 "dead socket\n", btsbsc);
795 handle_dead_socket(bfd);
796 }
797 return ret;
798 }
799
800 msgb_put(msg, ret);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200801 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
802 DEBUGPC(DLMI, "RX<-%s: %s\n", btsbsc, osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100803
804 hh = (struct ipaccess_head *) msg->data;
805 if (hh->proto == IPAC_PROTO_IPACCESS) {
806 ret = ipaccess_rcvmsg(ipc, msg, bfd);
807 if (ret < 0) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200808 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100809 close(bfd->fd);
810 bfd->fd = -1;
811 talloc_free(bfd);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800812 msgb_free(msg);
813 return ret;
814 } else if (ret == 0) {
815 /* we do not forward parts of the CCM protocol
816 * through the proxy but rather terminate it ourselves. */
817 msgb_free(msg);
818 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100819 }
Harald Welte2ca7c312009-12-23 22:44:04 +0100820 }
821
822 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200823 LOGP(DLINP, LOGL_ERROR,
Harald Welte2ca7c312009-12-23 22:44:04 +0100824 "received %s packet but no ipc->bts_conn?!?\n", btsbsc);
825 msgb_free(msg);
826 return -EIO;
827 }
828
829 bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr);
830 if (bsc_conn) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800831 if (gprs_ns_ipaddr)
832 patch_gprs_msg(ipbc, bfd->priv_nr, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100833 /* enqueue packet towards BSC */
834 msgb_enqueue(&bsc_conn->tx_queue, msg);
835 /* mark respective filedescriptor as 'we want to write' */
836 bsc_conn->fd.when |= BSC_FD_WRITE;
837 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200838 logp_ipbc_uid(DLINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8);
839 LOGPC(DLINP, LOGL_INFO, "Dropping packet from %s, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100840 "since remote connection is dead\n", btsbsc);
841 msgb_free(msg);
842 }
843
844 return ret;
845}
846
847/* a TCP socket is ready to be written to */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200848static int handle_tcp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100849{
850 struct ipa_proxy_conn *ipc = bfd->data;
851 struct ipa_bts_conn *ipbc = ipc->bts_conn;
852 struct llist_head *lh;
853 struct msgb *msg;
854 char *btsbsc;
855 int ret;
856
857 if ((bfd->priv_nr & 0xff) <= 2)
858 btsbsc = "BTS";
859 else
860 btsbsc = "BSC";
861
862
863 /* get the next msg for this timeslot */
864 if (llist_empty(&ipc->tx_queue)) {
865 bfd->when &= ~BSC_FD_WRITE;
866 return 0;
867 }
868 lh = ipc->tx_queue.next;
869 llist_del(lh);
870 msg = llist_entry(lh, struct msgb, list);
871
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200872 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
873 DEBUGPC(DLMI, "TX %04x: %s\n", bfd->priv_nr,
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200874 osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100875
876 ret = send(bfd->fd, msg->data, msg->len, 0);
877 msgb_free(msg);
878
879 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200880 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
881 LOGP(DLINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc);
Harald Welte2ca7c312009-12-23 22:44:04 +0100882 handle_dead_socket(bfd);
883 }
884
885 return ret;
886}
887
888/* callback from select.c in case one of the fd's can be read/written */
Harald Welteeb623012014-08-18 22:33:12 +0200889static int proxy_ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100890{
891 int rc = 0;
892
893 if (what & BSC_FD_READ) {
894 rc = handle_tcp_read(bfd);
895 if (rc < 0)
896 return rc;
897 }
898 if (what & BSC_FD_WRITE)
899 rc = handle_tcp_write(bfd);
900
901 return rc;
902}
903
904/* callback of the listening filedescriptor */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200905static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100906{
907 int ret;
908 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200909 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100910 struct sockaddr_in sa;
911 socklen_t sa_len = sizeof(sa);
912
913 if (!(what & BSC_FD_READ))
914 return 0;
915
916 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
917 if (ret < 0) {
918 perror("accept");
919 return ret;
920 }
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200921 DEBUGP(DLINP, "accept()ed new %s link from %s\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100922 (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL",
923 inet_ntoa(sa.sin_addr));
924
925 ipc = alloc_conn();
926 if (!ipc) {
927 close(ret);
928 return -ENOMEM;
929 }
930
931 bfd = &ipc->fd;
932 bfd->fd = ret;
933 bfd->data = ipc;
934 bfd->priv_nr = listen_bfd->priv_nr;
Harald Welteeb623012014-08-18 22:33:12 +0200935 bfd->cb = proxy_ipaccess_fd_cb;
Harald Welte2ca7c312009-12-23 22:44:04 +0100936 bfd->when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200937 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100938 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200939 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100940 close(bfd->fd);
941 talloc_free(ipc);
942 return ret;
943 }
944
945 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Harald Welte4a88a492014-08-20 23:46:40 +0200946 ret = ipa_ccm_send_id_req(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100947
948 return 0;
949}
950
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800951static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port)
952{
953 int ret;
954 struct sockaddr_in addr;
955 socklen_t len = sizeof(addr);
956 memset(&addr, 0, sizeof(addr));
957
958 addr.sin_family = AF_INET;
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800959 addr.sin_port = htons(port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800960 addr.sin_addr = ip;
961
962 ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len);
963 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200964 LOGP(DLINP, LOGL_ERROR, "Failed to forward GPRS message.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800965 }
966}
967
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200968static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800969{
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800970 struct ipa_bts_conn *bts;
971 char buf[4096];
972 int ret;
973 struct sockaddr_in sock;
974 socklen_t len = sizeof(sock);
975
976 /* 1. get the data... */
977 ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len);
978 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200979 LOGP(DLINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800980 return -1;
981 }
982
983 bts = bfd->data;
984
985 /* 2. figure out where to send it to */
986 if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200987 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from network.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800988 send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000);
989 } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200990 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800991 send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000);
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800992 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200993 LOGP(DLINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800994 }
995
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800996 return 0;
997}
998
Harald Welte2ca7c312009-12-23 22:44:04 +0100999/* Actively connect to a BSC. */
1000static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data)
1001{
1002 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001003 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +01001004 int ret, on = 1;
1005
1006 ipc = alloc_conn();
1007 if (!ipc)
1008 return NULL;
1009
1010 ipc->bts_conn = data;
1011
1012 bfd = &ipc->fd;
1013 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1014 bfd->cb = ipaccess_fd_cb;
1015 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
1016 bfd->data = ipc;
1017 bfd->priv_nr = priv_nr;
1018
Holger Hans Peter Freythere18209c2013-12-12 16:16:35 +01001019 if (bfd->fd < 0) {
1020 LOGP(DLINP, LOGL_ERROR, "Could not create socket: %s\n",
1021 strerror(errno));
1022 talloc_free(ipc);
1023 return NULL;
1024 }
1025
Harald Welte3c165d02016-11-26 14:09:34 +01001026 ret = setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1027 if (ret < 0) {
1028 LOGP(DLINP, LOGL_ERROR, "Could not set socket option\n");
1029 close(bfd->fd);
1030 talloc_free(ipc);
1031 return NULL;
1032 }
Harald Welte2ca7c312009-12-23 22:44:04 +01001033
1034 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
1035 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001036 LOGP(DLINP, LOGL_ERROR, "Could not connect socket: %s\n",
Holger Hans Peter Freyther6cb9b232010-06-09 15:15:11 +08001037 inet_ntoa(sa->sin_addr));
Harald Welte2ca7c312009-12-23 22:44:04 +01001038 close(bfd->fd);
1039 talloc_free(ipc);
1040 return NULL;
1041 }
1042
1043 /* pre-fill tx_queue with identity request */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001044 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +01001045 if (ret < 0) {
1046 close(bfd->fd);
1047 talloc_free(ipc);
1048 return NULL;
1049 }
1050
1051 return ipc;
1052}
1053
1054static int ipaccess_proxy_setup(void)
1055{
1056 int ret;
1057
1058 ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy);
1059 if (!ipp)
1060 return -ENOMEM;
1061 INIT_LLIST_HEAD(&ipp->bts_list);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001062 osmo_timer_setup(&ipp->reconn_timer, reconn_tmr_cb, ipp);
Harald Welte2ca7c312009-12-23 22:44:04 +01001063
1064 /* Listen for OML connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001065 ret = make_sock(&ipp->oml_listen_fd, IPPROTO_TCP, INADDR_ANY,
1066 IPA_TCP_PORT_OML, OML_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001067 if (ret < 0)
1068 return ret;
1069
1070 /* Listen for RSL connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001071 ret = make_sock(&ipp->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY,
1072 IPA_TCP_PORT_RSL, RSL_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001073
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001074 if (ret < 0)
1075 return ret;
1076
1077 /* Connect the GPRS NS Socket */
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001078 if (gprs_ns_ipaddr) {
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +08001079 inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr);
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001080 inet_aton(listen_ipaddr, &ipp->listen_addr);
1081 }
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001082
Harald Welte2ca7c312009-12-23 22:44:04 +01001083 return ret;
1084}
1085
1086static void signal_handler(int signal)
1087{
1088 fprintf(stdout, "signal %u received\n", signal);
1089
1090 switch (signal) {
1091 case SIGABRT:
1092 /* in case of abort, we want to obtain a talloc report
1093 * and then return to the caller, who will abort the process */
1094 case SIGUSR1:
1095 talloc_report_full(tall_bsc_ctx, stderr);
1096 break;
1097 default:
1098 break;
1099 }
1100}
1101
Harald Welted4ab13b2011-07-16 13:39:44 +02001102static void print_help(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001103{
1104 printf(" ipaccess-proxy is a proxy BTS.\n");
1105 printf(" -h --help. This help text.\n");
1106 printf(" -l --listen IP. The ip to listen to.\n");
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001107 printf(" -b --bsc IP. The BSC IP address.\n");
1108 printf(" -g --gprs IP. Take GPRS NS from that IP.\n");
1109 printf("\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001110 printf(" -s --disable-color. Disable the color inside the logging message.\n");
1111 printf(" -e --log-level number. Set the global loglevel.\n");
1112 printf(" -T --timestamp. Prefix every log message with a timestamp.\n");
1113 printf(" -V --version. Print the version of OpenBSC.\n");
1114}
1115
Harald Welted4ab13b2011-07-16 13:39:44 +02001116static void print_usage(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001117{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001118 printf("Usage: ipaccess-proxy [options]\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001119}
1120
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001121enum {
1122 IPA_PROXY_OPT_LISTEN_NONE = 0,
1123 IPA_PROXY_OPT_LISTEN_IP = (1 << 0),
1124 IPA_PROXY_OPT_BSC_IP = (1 << 1),
1125};
1126
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001127static void handle_options(int argc, char** argv)
1128{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001129 int options_mask = 0;
1130
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001131 /* disable explicit missing arguments error output from getopt_long */
1132 opterr = 0;
1133
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001134 while (1) {
1135 int option_index = 0, c;
1136 static struct option long_options[] = {
1137 {"help", 0, 0, 'h'},
1138 {"disable-color", 0, 0, 's'},
1139 {"timestamp", 0, 0, 'T'},
1140 {"log-level", 1, 0, 'e'},
1141 {"listen", 1, 0, 'l'},
1142 {"bsc", 1, 0, 'b'},
1143 {0, 0, 0, 0}
1144 };
1145
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001146 c = getopt_long(argc, argv, "hsTe:l:b:g:",
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001147 long_options, &option_index);
1148 if (c == -1)
1149 break;
1150
1151 switch (c) {
1152 case 'h':
1153 print_usage();
1154 print_help();
1155 exit(0);
1156 case 'l':
1157 listen_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001158 options_mask |= IPA_PROXY_OPT_LISTEN_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001159 break;
1160 case 'b':
1161 bsc_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001162 options_mask |= IPA_PROXY_OPT_BSC_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001163 break;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001164 case 'g':
1165 gprs_ns_ipaddr = optarg;
1166 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001167 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001168 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001169 break;
1170 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001171 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001172 break;
1173 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001174 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001175 break;
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001176 case '?':
1177 if (optopt) {
1178 printf("ERROR: missing mandatory argument "
1179 "for `%s' option\n", argv[optind-1]);
1180 } else {
1181 printf("ERROR: unknown option `%s'\n",
1182 argv[optind-1]);
1183 }
1184 print_usage();
1185 print_help();
1186 exit(EXIT_FAILURE);
1187 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001188 default:
1189 /* ignore */
1190 break;
1191 }
1192 }
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001193 if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP))
1194 != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) {
1195 printf("ERROR: You have to specify `--listen' and `--bsc' "
1196 "options at least.\n");
1197 print_usage();
1198 print_help();
1199 exit(EXIT_FAILURE);
1200 }
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001201}
1202
Harald Welte2ca7c312009-12-23 22:44:04 +01001203int main(int argc, char **argv)
1204{
1205 int rc;
1206
Harald Welte2ca7c312009-12-23 22:44:04 +01001207 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +02001208 msgb_talloc_ctx_init(tall_bsc_ctx, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +01001209
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001210 osmo_init_logging(&log_info);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001211 log_parse_category_mask(osmo_stderr_target, "DLINP:DLMI");
Harald Welte2ca7c312009-12-23 22:44:04 +01001212
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001213 handle_options(argc, argv);
1214
Harald Welte2ca7c312009-12-23 22:44:04 +01001215 rc = ipaccess_proxy_setup();
1216 if (rc < 0)
1217 exit(1);
1218
1219 signal(SIGUSR1, &signal_handler);
1220 signal(SIGABRT, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001221 osmo_init_ignore_signals();
Harald Welte2ca7c312009-12-23 22:44:04 +01001222
1223 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001224 osmo_select_main(0);
Harald Welte2ca7c312009-12-23 22:44:04 +01001225 }
1226}