blob: 390510a9b1e73289b0780eb9eecab3836bcae7f4 [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 Welte2ca7c312009-12-23 22:44:04 +010045#include <openbsc/debug.h>
46#include <openbsc/ipaccess.h>
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +020047#include <openbsc/socket.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010048#include <osmocom/core/talloc.h>
Harald Welte2ca7c312009-12-23 22:44:04 +010049
Harald Welte2ca7c312009-12-23 22:44:04 +010050/* one instance of an ip.access protocol proxy */
51struct ipa_proxy {
52 /* socket where we listen for incoming OML from BTS */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020053 struct osmo_fd oml_listen_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010054 /* socket where we listen for incoming RSL from BTS */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020055 struct osmo_fd rsl_listen_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010056 /* list of BTS's (struct ipa_bts_conn */
57 struct llist_head bts_list;
58 /* the BSC reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020059 struct osmo_timer_list reconn_timer;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +080060 /* global GPRS NS data */
61 struct in_addr gprs_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +080062 struct in_addr listen_addr;
Harald Welte2ca7c312009-12-23 22:44:04 +010063};
64
65/* global pointer to the proxy structure */
66static struct ipa_proxy *ipp;
67
68struct ipa_proxy_conn {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020069 struct osmo_fd fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010070 struct llist_head tx_queue;
71 struct ipa_bts_conn *bts_conn;
72};
Harald Welte2ca7c312009-12-23 22:44:04 +010073#define MAX_TRX 4
74
75/* represents a particular BTS in our proxy */
76struct ipa_bts_conn {
77 /* list of BTS's (ipa_proxy->bts_list) */
78 struct llist_head list;
79 /* back pointer to the proxy which we belong to */
80 struct ipa_proxy *ipp;
81 /* the unit ID as determined by CCM */
82 struct {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 uint16_t site_id;
84 uint16_t bts_id;
Harald Welte2ca7c312009-12-23 22:44:04 +010085 } unit_id;
86
87 /* incoming connections from BTS */
88 struct ipa_proxy_conn *oml_conn;
89 struct ipa_proxy_conn *rsl_conn[MAX_TRX];
90
91 /* outgoing connections to BSC */
92 struct ipa_proxy_conn *bsc_oml_conn;
93 struct ipa_proxy_conn *bsc_rsl_conn[MAX_TRX];
94
95 /* UDP sockets for BTS and BSC injection */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020096 struct osmo_fd udp_bts_fd;
97 struct osmo_fd udp_bsc_fd;
Harald Welte2ca7c312009-12-23 22:44:04 +010098
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +080099 /* NS data */
100 struct in_addr bts_addr;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200101 struct osmo_fd gprs_ns_fd;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800102 int gprs_local_port;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800103 uint16_t gprs_orig_port;
104 uint32_t gprs_orig_ip;
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800105
Harald Welte36ac7752011-07-16 13:38:48 +0200106 char *id_tags[256];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200107 uint8_t *id_resp;
Harald Welte2ca7c312009-12-23 22:44:04 +0100108 unsigned int id_resp_len;
109};
110
111enum ipp_fd_type {
112 OML_FROM_BTS = 1,
113 RSL_FROM_BTS = 2,
114 OML_TO_BSC = 3,
115 RSL_TO_BSC = 4,
116 UDP_TO_BTS = 5,
117 UDP_TO_BSC = 6,
118};
119
120/* some of the code against we link from OpenBSC needs this */
121void *tall_bsc_ctx;
122
123static char *listen_ipaddr;
124static char *bsc_ipaddr;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800125static char *gprs_ns_ipaddr;
Harald Welte2ca7c312009-12-23 22:44:04 +0100126
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200127static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800128
Holger Hans Peter Freyther3dac8812010-06-09 14:39:22 +0800129#define PROXY_ALLOC_SIZE 1200
Harald Welte2ca7c312009-12-23 22:44:04 +0100130
Harald Welte2ca7c312009-12-23 22:44:04 +0100131static struct ipa_bts_conn *find_bts_by_unitid(struct ipa_proxy *ipp,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200132 uint16_t site_id,
133 uint16_t bts_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100134{
135 struct ipa_bts_conn *ipbc;
136
137 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
138 if (ipbc->unit_id.site_id == site_id &&
139 ipbc->unit_id.bts_id == bts_id)
140 return ipbc;
141 }
142
143 return NULL;
144}
145
146struct ipa_proxy_conn *alloc_conn(void)
147{
148 struct ipa_proxy_conn *ipc;
149
150 ipc = talloc_zero(tall_bsc_ctx, struct ipa_proxy_conn);
151 if (!ipc)
152 return NULL;
153
154 INIT_LLIST_HEAD(&ipc->tx_queue);
155
156 return ipc;
157}
158
159static int store_idtags(struct ipa_bts_conn *ipbc, struct tlv_parsed *tlvp)
160{
161 unsigned int i, len;
162
163 for (i = 0; i <= 0xff; i++) {
164 if (!TLVP_PRESENT(tlvp, i))
165 continue;
166
167 len = TLVP_LEN(tlvp, i);
168#if 0
169 if (!ipbc->id_tags[i])
170 ipbc->id_tags[i] = talloc_size(tall_bsc_ctx, len);
171 else
172#endif
Harald Weltea16ef3d2009-12-23 23:35:51 +0100173 ipbc->id_tags[i] = talloc_realloc_size(ipbc,
Harald Welte2ca7c312009-12-23 22:44:04 +0100174 ipbc->id_tags[i], len);
175 if (!ipbc->id_tags[i])
176 return -ENOMEM;
177
178 memset(ipbc->id_tags[i], 0, len);
179 //memcpy(ipbc->id_tags[i], TLVP_VAL(tlvp, i), len);
180 }
181 return 0;
182}
183
184
185static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data);
186
187#define logp_ipbc_uid(ss, lvl, ipbc, trx_id) _logp_ipbc_uid(ss, lvl, __FILE__, __LINE__, ipbc, trx_id)
188
189static void _logp_ipbc_uid(unsigned int ss, unsigned int lvl, char *file, int line,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200190 struct ipa_bts_conn *ipbc, uint8_t trx_id)
Harald Welte2ca7c312009-12-23 22:44:04 +0100191{
192 if (ipbc)
Harald Weltedc5062b2010-03-26 21:28:59 +0800193 logp2(ss, lvl, file, line, 0, "(%u/%u/%u) ", ipbc->unit_id.site_id,
Harald Welte2ca7c312009-12-23 22:44:04 +0100194 ipbc->unit_id.bts_id, trx_id);
195 else
Harald Weltedc5062b2010-03-26 21:28:59 +0800196 logp2(ss, lvl, file, line, 0, "unknown ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100197}
198
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200199static int handle_udp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100200{
201 struct ipa_bts_conn *ipbc = bfd->data;
202 struct ipa_proxy_conn *other_conn = NULL;
203 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP UDP");
204 struct ipaccess_head *hh;
205 int ret;
206
207 /* with UDP sockets, we cannot read partial packets but have to read
208 * all of it in one go */
209 hh = (struct ipaccess_head *) msg->data;
210 ret = recv(bfd->fd, msg->data, msg->data_len, 0);
211 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100212 if (errno != EAGAIN)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200213 LOGP(DLINP, LOGL_ERROR, "recv error %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100214 msgb_free(msg);
215 return ret;
216 }
217 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200218 DEBUGP(DLINP, "UDP peer disappeared, dead socket\n");
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200219 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100220 close(bfd->fd);
221 bfd->fd = -1;
222 msgb_free(msg);
223 return -EIO;
224 }
225 if (ret < sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200226 DEBUGP(DLINP, "could not even read header!?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100227 msgb_free(msg);
228 return -EIO;
229 }
230 msgb_put(msg, ret);
231 msg->l2h = msg->data + sizeof(*hh);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200232 DEBUGP(DLMI, "UDP RX: %s\n", osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100233
234 if (hh->len != msg->len - sizeof(*hh)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200235 DEBUGP(DLINP, "length (%u/%u) disagrees with header(%u)\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100236 msg->len, msg->len - 3, hh->len);
237 msgb_free(msg);
238 return -EIO;
239 }
240
241 switch (bfd->priv_nr & 0xff) {
242 case UDP_TO_BTS:
243 /* injection towards BTS */
244 switch (hh->proto) {
245 case IPAC_PROTO_RSL:
246 /* FIXME: what to do about TRX > 0 */
247 other_conn = ipbc->rsl_conn[0];
248 break;
249 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200250 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100251 "OML FD\n", hh->proto);
252 /* fall through */
253 case IPAC_PROTO_IPACCESS:
254 case IPAC_PROTO_OML:
255 other_conn = ipbc->oml_conn;
256 break;
257 }
258 break;
259 case UDP_TO_BSC:
260 /* injection towards BSC */
261 switch (hh->proto) {
262 case IPAC_PROTO_RSL:
263 /* FIXME: what to do about TRX > 0 */
264 other_conn = ipbc->bsc_rsl_conn[0];
265 break;
266 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200267 DEBUGP(DLINP, "Unknown protocol 0x%02x, sending to "
Harald Welte2ca7c312009-12-23 22:44:04 +0100268 "OML FD\n", hh->proto);
Holger Hans Peter Freyther4e5f93c2014-06-12 11:32:25 +0200269 /* fall through */
Harald Welte2ca7c312009-12-23 22:44:04 +0100270 case IPAC_PROTO_IPACCESS:
271 case IPAC_PROTO_OML:
272 other_conn = ipbc->bsc_oml_conn;
273 break;
274 }
275 break;
276 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200277 DEBUGP(DLINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr);
Harald Welte2ca7c312009-12-23 22:44:04 +0100278 break;
279 }
280
281 if (other_conn) {
282 /* enqueue the message for TX on the respective FD */
283 msgb_enqueue(&other_conn->tx_queue, msg);
284 other_conn->fd.when |= BSC_FD_WRITE;
285 } else
286 msgb_free(msg);
287
288 return 0;
289}
290
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200291static int handle_udp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100292{
293 /* not implemented yet */
294 bfd->when &= ~BSC_FD_WRITE;
295
296 return -EIO;
297}
298
299/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200300static int udp_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100301{
302 int rc = 0;
303
304 if (what & BSC_FD_READ)
305 rc = handle_udp_read(bfd);
306 if (what & BSC_FD_WRITE)
307 rc = handle_udp_write(bfd);
308
309 return rc;
310}
311
312
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200313static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200314 uint16_t site_id, uint16_t bts_id,
315 uint16_t trx_id, struct tlv_parsed *tlvp,
Harald Welte2ca7c312009-12-23 22:44:04 +0100316 struct msgb *msg)
317{
318 struct ipa_bts_conn *ipbc;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200319 uint16_t udp_port;
Harald Welte2ca7c312009-12-23 22:44:04 +0100320 int ret = 0;
321 struct sockaddr_in sin;
322
323 memset(&sin, 0, sizeof(sin));
324 sin.sin_family = AF_INET;
325 inet_aton(bsc_ipaddr, &sin.sin_addr);
326
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200327 DEBUGP(DLINP, "(%u/%u/%u) New BTS connection: ",
Harald Welte2ca7c312009-12-23 22:44:04 +0100328 site_id, bts_id, trx_id);
329
330 /* OML needs to be established before RSL */
331 if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200332 DEBUGPC(DLINP, "Not a OML connection ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100333 return -EIO;
334 }
335
336 /* allocate new BTS connection data structure */
337 ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn);
338 if (!ipbc) {
339 ret = -ENOMEM;
340 goto err_out;
341 }
342
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200343 DEBUGPC(DLINP, "Created BTS Conn data structure\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100344 ipbc->ipp = ipp;
345 ipbc->unit_id.site_id = site_id;
346 ipbc->unit_id.bts_id = bts_id;
347 ipbc->oml_conn = ipc;
348 ipc->bts_conn = ipbc;
349
350 /* store the content of the ID TAGS for later reference */
351 store_idtags(ipbc, tlvp);
352 ipbc->id_resp_len = msg->len;
353 ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len);
354 memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len);
355
356 /* Create OML TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100357 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100358 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
359 if (!ipbc->bsc_oml_conn) {
360 ret = -EIO;
361 goto err_bsc_conn;
362 }
363
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200364 DEBUGP(DLINP, "(%u/%u/%u) OML Connected to BSC\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100365 site_id, bts_id, trx_id);
366
367 /* Create UDP socket for BTS packet injection */
368 udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200369 ret = make_sock(&ipbc->udp_bts_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100370 UDP_TO_BTS, udp_fd_cb, ipbc);
371 if (ret < 0)
372 goto err_udp_bts;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200373 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100374 "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port);
375
376 /* Create UDP socket for BSC packet injection */
377 udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200378 ret = make_sock(&ipbc->udp_bsc_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100379 UDP_TO_BSC, udp_fd_cb, ipbc);
380 if (ret < 0)
381 goto err_udp_bsc;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200382 DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
Harald Welte2ca7c312009-12-23 22:44:04 +0100383 "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800384
385
386 /* GPRS NS related code */
387 if (gprs_ns_ipaddr) {
388 struct sockaddr_in sock;
389 socklen_t len = sizeof(sock);
Pablo Neira Ayuso7e737002011-04-11 16:33:17 +0200390 struct in_addr addr;
391 uint32_t ip;
392
393 inet_aton(listen_ipaddr, &addr);
394 ip = ntohl(addr.s_addr); /* make_sock() needs host byte order */
395 ret = make_sock(&ipbc->gprs_ns_fd, IPPROTO_UDP, ip, 0, 0,
396 gprs_ns_cb, ipbc);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800397 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200398 LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800399 goto err_udp_bsc;
400 }
401
402 ret = getsockname(ipbc->gprs_ns_fd.fd, (struct sockaddr* ) &sock, &len);
403 ipbc->gprs_local_port = ntohs(sock.sin_port);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200404 LOGP(DLINP, LOGL_NOTICE,
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800405 "Created GPRS NS Socket. Listening on: %s:%d\n",
406 inet_ntoa(sock.sin_addr), ipbc->gprs_local_port);
407
408 ret = getpeername(bfd->fd, (struct sockaddr* ) &sock, &len);
409 ipbc->bts_addr = sock.sin_addr;
410 }
411
Harald Welte2ca7c312009-12-23 22:44:04 +0100412 llist_add(&ipbc->list, &ipp->bts_list);
413
414 return 0;
415
416err_udp_bsc:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200417 osmo_fd_unregister(&ipbc->udp_bts_fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100418err_udp_bts:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200419 osmo_fd_unregister(&ipbc->bsc_oml_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100420 close(ipbc->bsc_oml_conn->fd.fd);
421 talloc_free(ipbc->bsc_oml_conn);
422 ipbc->bsc_oml_conn = NULL;
423err_bsc_conn:
424 talloc_free(ipbc->id_resp);
425 talloc_free(ipbc);
426#if 0
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200427 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100428 close(bfd->fd);
429 talloc_free(bfd);
430#endif
431err_out:
432 return ret;
433}
434
435static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200436 struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100437{
438 struct tlv_parsed tlvp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200439 uint8_t msg_type = *(msg->l2h);
440 uint16_t site_id, bts_id, trx_id;
Harald Welte2ca7c312009-12-23 22:44:04 +0100441 struct ipa_bts_conn *ipbc;
442 int ret = 0;
443
444 switch (msg_type) {
445 case IPAC_MSGT_PING:
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200446 ret = ipaccess_send_pong(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100447 break;
448 case IPAC_MSGT_PONG:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200449 DEBUGP(DLMI, "PONG!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100450 break;
451 case IPAC_MSGT_ID_RESP:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200452 DEBUGP(DLMI, "ID_RESP ");
Harald Welte2ca7c312009-12-23 22:44:04 +0100453 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200454 ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
Pablo Neira Ayuso625295b2011-04-07 14:15:16 +0200455 msgb_l2len(msg)-2);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200456 DEBUGP(DLMI, "\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100457
458 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200459 LOGP(DLINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100460 return -EIO;
461 }
462
463 /* lookup BTS, create sign_link, ... */
Holger Hans Peter Freyther82ae7162010-03-30 15:20:46 +0200464 site_id = bts_id = trx_id = 0;
Pablo Neira Ayusoc281b4e2011-04-07 14:15:20 +0200465 ipaccess_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
466 &site_id, &bts_id, &trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100467 ipbc = find_bts_by_unitid(ipp, site_id, bts_id);
468 if (!ipbc) {
469 /* We have not found an ipbc (per-bts proxy instance)
470 * for this BTS yet. The first connection of a new BTS must
471 * be a OML connection. We allocate the associated data structures,
472 * and try to connect to the remote end */
473
474 return ipbc_alloc_connect(ipc, bfd, site_id, bts_id,
475 trx_id, &tlvp, msg);
476 /* if this fails, the caller will clean up bfd */
477 } else {
478 struct sockaddr_in sin;
479 memset(&sin, 0, sizeof(sin));
480 sin.sin_family = AF_INET;
481 inet_aton(bsc_ipaddr, &sin.sin_addr);
482
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200483 DEBUGP(DLINP, "Identified BTS %u/%u/%u\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100484 site_id, bts_id, trx_id);
485
486 if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200487 LOGP(DLINP, LOGL_ERROR, "Second OML connection from "
Harald Welte2ca7c312009-12-23 22:44:04 +0100488 "same BTS ?!?\n");
489 return 0;
490 }
491
Harald Welte36ac7752011-07-16 13:38:48 +0200492 if (trx_id >= MAX_TRX) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200493 LOGP(DLINP, LOGL_ERROR, "We don't support more "
Harald Welte2ca7c312009-12-23 22:44:04 +0100494 "than %u TRX\n", MAX_TRX);
495 return -EINVAL;
496 }
497
498 ipc->bts_conn = ipbc;
499 /* store TRX number in higher 8 bit of the bfd private number */
500 bfd->priv_nr |= trx_id << 8;
501 ipbc->rsl_conn[trx_id] = ipc;
502
503 /* Create RSL TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100504 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welte2ca7c312009-12-23 22:44:04 +0100505 ipbc->bsc_rsl_conn[trx_id] =
506 connect_bsc(&sin, RSL_TO_BSC | (trx_id << 8), ipbc);
507 if (!ipbc->bsc_oml_conn)
508 return -EIO;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200509 DEBUGP(DLINP, "(%u/%u/%u) Connected RSL to BSC\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100510 site_id, bts_id, trx_id);
511 }
512 break;
513 case IPAC_MSGT_ID_GET:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200514 DEBUGP(DLMI, "ID_GET\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100515 if ((bfd->priv_nr & 0xff) != OML_TO_BSC &&
516 (bfd->priv_nr & 0xff) != RSL_TO_BSC) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200517 DEBUGP(DLINP, "IDentity REQuest from BTS ?!?\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100518 return -EIO;
519 }
520 ipbc = ipc->bts_conn;
521 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200522 DEBUGP(DLINP, "ID_GET from BSC before we have ID_RESP from BTS\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100523 return -EIO;
524 }
525 ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len);
526 break;
527 case IPAC_MSGT_ID_ACK:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200528 DEBUGP(DLMI, "ID_ACK? -> ACK!\n");
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200529 ret = ipaccess_send_id_ack(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100530 break;
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800531 default:
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200532 LOGP(DLMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800533 return 1;
534 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100535 }
536 return 0;
537}
538
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200539struct msgb *ipaccess_proxy_read_msg(struct osmo_fd *bfd, int *error)
Harald Welte2ca7c312009-12-23 22:44:04 +0100540{
541 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
542 struct ipaccess_head *hh;
543 int len, ret = 0;
544
545 if (!msg) {
546 *error = -ENOMEM;
547 return NULL;
548 }
549
550 /* first read our 3-byte header */
551 hh = (struct ipaccess_head *) msg->data;
552 ret = recv(bfd->fd, msg->data, 3, 0);
553 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100554 if (errno != EAGAIN)
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200555 LOGP(DLINP, LOGL_ERROR, "recv error: %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100556 msgb_free(msg);
557 *error = ret;
558 return NULL;
559 } else if (ret == 0) {
560 msgb_free(msg);
561 *error = ret;
562 return NULL;
563 }
564
565 msgb_put(msg, ret);
566
567 /* then read te length as specified in header */
568 msg->l2h = msg->data + sizeof(*hh);
569 len = ntohs(hh->len);
570 ret = recv(bfd->fd, msg->l2h, len, 0);
571 if (ret < len) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200572 LOGP(DLINP, LOGL_ERROR, "short read!\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100573 msgb_free(msg);
574 *error = -EIO;
575 return NULL;
576 }
577 msgb_put(msg, ret);
578
579 return msg;
580}
581
582static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc,
583 unsigned int priv_nr)
584{
585 struct ipa_proxy_conn *bsc_conn;
586 unsigned int trx_id = priv_nr >> 8;
587
588 switch (priv_nr & 0xff) {
589 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
590 bsc_conn = ipbc->bsc_oml_conn;
591 break;
592 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
593 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
594 break;
595 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
596 bsc_conn = ipbc->oml_conn;
597 break;
598 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
599 bsc_conn = ipbc->rsl_conn[trx_id];
600 break;
601 default:
602 bsc_conn = NULL;
603 break;
604 }
605 return bsc_conn;
606}
607
608static void reconn_tmr_cb(void *data)
609{
610 struct ipa_proxy *ipp = data;
611 struct ipa_bts_conn *ipbc;
612 struct sockaddr_in sin;
613 int i;
614
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200615 DEBUGP(DLINP, "Running reconnect timer\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100616
617 memset(&sin, 0, sizeof(sin));
618 sin.sin_family = AF_INET;
619 inet_aton(bsc_ipaddr, &sin.sin_addr);
620
621 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
622 /* if OML to BSC is dead, try to restore it */
623 if (ipbc->oml_conn && !ipbc->bsc_oml_conn) {
Harald Welte87ed5cd2009-12-23 22:47:53 +0100624 sin.sin_port = htons(IPA_TCP_PORT_OML);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200625 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
626 LOGPC(DLINP, LOGL_NOTICE, "OML Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100627 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
628 if (!ipbc->bsc_oml_conn)
629 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200630 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, 0);
631 LOGPC(DLINP, LOGL_NOTICE, "OML Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100632 }
633 /* if we (still) don't have a OML connection, skip RSL */
634 if (!ipbc->oml_conn || !ipbc->bsc_oml_conn)
635 continue;
636
637 for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) {
638 unsigned int priv_nr;
639 /* don't establish RSL links which we don't have */
640 if (!ipbc->rsl_conn[i])
641 continue;
642 if (ipbc->bsc_rsl_conn[i])
643 continue;
644 priv_nr = ipbc->rsl_conn[i]->fd.priv_nr;
645 priv_nr &= ~0xff;
646 priv_nr |= RSL_TO_BSC;
Harald Welte87ed5cd2009-12-23 22:47:53 +0100647 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200648 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
649 LOGPC(DLINP, LOGL_NOTICE, "RSL Trying to reconnect\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100650 ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc);
Holger Hans Peter Freytherc9251fa2013-07-14 08:47:06 +0200651 if (!ipbc->bsc_rsl_conn[i])
Harald Welte2ca7c312009-12-23 22:44:04 +0100652 goto reschedule;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200653 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
654 LOGPC(DLINP, LOGL_NOTICE, "RSL Reconnected\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100655 }
656 }
657 return;
658
659reschedule:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200660 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100661}
662
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200663static void handle_dead_socket(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100664{
665 struct ipa_proxy_conn *ipc = bfd->data; /* local conn */
666 struct ipa_proxy_conn *bsc_conn; /* remote conn */
667 struct ipa_bts_conn *ipbc = ipc->bts_conn;
668 unsigned int trx_id = bfd->priv_nr >> 8;
669 struct msgb *msg, *msg2;
670
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200671 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100672 close(bfd->fd);
673 bfd->fd = -1;
674
675 /* FIXME: clear tx_queue, remove all references, etc. */
676 llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list)
677 msgb_free(msg);
678
679 switch (bfd->priv_nr & 0xff) {
680 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
Pablo Neira Ayuso3c409c22011-03-27 21:31:48 +0200681 /* The BTS started a connection with us but we got no
682 * IPAC_MSGT_ID_RESP message yet, in that scenario we did not
683 * allocate the ipa_bts_conn structure. */
684 if (ipbc == NULL)
685 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100686 ipbc->oml_conn = NULL;
687 bsc_conn = ipbc->bsc_oml_conn;
688 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200689 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100690 close(bsc_conn->fd.fd);
691 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
692 msgb_free(msg);
693 talloc_free(bsc_conn);
694 ipbc->bsc_oml_conn = NULL;
695 /* FIXME: do we need to delete the entire ipbc ? */
696 break;
697 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
698 ipbc->rsl_conn[trx_id] = NULL;
699 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
700 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200701 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100702 close(bsc_conn->fd.fd);
703 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
704 msgb_free(msg);
705 talloc_free(bsc_conn);
706 ipbc->bsc_rsl_conn[trx_id] = NULL;
707 break;
708 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
709 ipbc->bsc_oml_conn = NULL;
710 bsc_conn = ipbc->oml_conn;
711 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200712 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100713 break;
714 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
715 ipbc->bsc_rsl_conn[trx_id] = NULL;
716 bsc_conn = ipbc->rsl_conn[trx_id];
717 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200718 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100719 break;
720 default:
721 bsc_conn = NULL;
722 break;
723 }
724
725 talloc_free(ipc);
726}
727
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800728static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg)
729{
730 uint8_t *nsvci;
731
732 if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC)
733 return;
734
735 if (msgb_l2len(msg) != 39)
736 return;
737
738 /*
739 * Check if this is a IPA Set Attribute or IPA Set Attribute ACK
740 * and if the FOM Class is GPRS NSVC0 and then we will patch it.
741 *
742 * The patch assumes the message looks like the one from the trace
743 * but we only match messages with a specific size anyway... So
744 * this hack should work just fine.
745 */
746
747 if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
748 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
749 msg->l2h[18] == 0xf5 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800750 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200751 ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8);
752 ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10);
753 *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port);
754 *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800755 } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
756 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
757 msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800758 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200759 *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port;
760 *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800761 }
762}
763
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200764static int handle_tcp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100765{
766 struct ipa_proxy_conn *ipc = bfd->data;
767 struct ipa_bts_conn *ipbc = ipc->bts_conn;
768 struct ipa_proxy_conn *bsc_conn;
Harald Weltea16ef3d2009-12-23 23:35:51 +0100769 struct msgb *msg;
Harald Welte2ca7c312009-12-23 22:44:04 +0100770 struct ipaccess_head *hh;
771 int ret = 0;
772 char *btsbsc;
773
Harald Welte2ca7c312009-12-23 22:44:04 +0100774 if ((bfd->priv_nr & 0xff) <= 2)
775 btsbsc = "BTS";
776 else
777 btsbsc = "BSC";
778
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200779 msg = ipaccess_proxy_read_msg(bfd, &ret);
Harald Welte2ca7c312009-12-23 22:44:04 +0100780 if (!msg) {
781 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200782 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
783 LOGPC(DLINP, LOGL_NOTICE, "%s disappeared, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100784 "dead socket\n", btsbsc);
785 handle_dead_socket(bfd);
786 }
787 return ret;
788 }
789
790 msgb_put(msg, ret);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200791 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
792 DEBUGPC(DLMI, "RX<-%s: %s\n", btsbsc, osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100793
794 hh = (struct ipaccess_head *) msg->data;
795 if (hh->proto == IPAC_PROTO_IPACCESS) {
796 ret = ipaccess_rcvmsg(ipc, msg, bfd);
797 if (ret < 0) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200798 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100799 close(bfd->fd);
800 bfd->fd = -1;
801 talloc_free(bfd);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800802 msgb_free(msg);
803 return ret;
804 } else if (ret == 0) {
805 /* we do not forward parts of the CCM protocol
806 * through the proxy but rather terminate it ourselves. */
807 msgb_free(msg);
808 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100809 }
Harald Welte2ca7c312009-12-23 22:44:04 +0100810 }
811
812 if (!ipbc) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200813 LOGP(DLINP, LOGL_ERROR,
Harald Welte2ca7c312009-12-23 22:44:04 +0100814 "received %s packet but no ipc->bts_conn?!?\n", btsbsc);
815 msgb_free(msg);
816 return -EIO;
817 }
818
819 bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr);
820 if (bsc_conn) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800821 if (gprs_ns_ipaddr)
822 patch_gprs_msg(ipbc, bfd->priv_nr, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100823 /* enqueue packet towards BSC */
824 msgb_enqueue(&bsc_conn->tx_queue, msg);
825 /* mark respective filedescriptor as 'we want to write' */
826 bsc_conn->fd.when |= BSC_FD_WRITE;
827 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200828 logp_ipbc_uid(DLINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8);
829 LOGPC(DLINP, LOGL_INFO, "Dropping packet from %s, "
Harald Welte2ca7c312009-12-23 22:44:04 +0100830 "since remote connection is dead\n", btsbsc);
831 msgb_free(msg);
832 }
833
834 return ret;
835}
836
837/* a TCP socket is ready to be written to */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200838static int handle_tcp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100839{
840 struct ipa_proxy_conn *ipc = bfd->data;
841 struct ipa_bts_conn *ipbc = ipc->bts_conn;
842 struct llist_head *lh;
843 struct msgb *msg;
844 char *btsbsc;
845 int ret;
846
847 if ((bfd->priv_nr & 0xff) <= 2)
848 btsbsc = "BTS";
849 else
850 btsbsc = "BSC";
851
852
853 /* get the next msg for this timeslot */
854 if (llist_empty(&ipc->tx_queue)) {
855 bfd->when &= ~BSC_FD_WRITE;
856 return 0;
857 }
858 lh = ipc->tx_queue.next;
859 llist_del(lh);
860 msg = llist_entry(lh, struct msgb, list);
861
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200862 logp_ipbc_uid(DLMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
863 DEBUGPC(DLMI, "TX %04x: %s\n", bfd->priv_nr,
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200864 osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100865
866 ret = send(bfd->fd, msg->data, msg->len, 0);
867 msgb_free(msg);
868
869 if (ret == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200870 logp_ipbc_uid(DLINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
871 LOGP(DLINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc);
Harald Welte2ca7c312009-12-23 22:44:04 +0100872 handle_dead_socket(bfd);
873 }
874
875 return ret;
876}
877
878/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200879static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100880{
881 int rc = 0;
882
883 if (what & BSC_FD_READ) {
884 rc = handle_tcp_read(bfd);
885 if (rc < 0)
886 return rc;
887 }
888 if (what & BSC_FD_WRITE)
889 rc = handle_tcp_write(bfd);
890
891 return rc;
892}
893
894/* callback of the listening filedescriptor */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200895static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100896{
897 int ret;
898 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200899 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100900 struct sockaddr_in sa;
901 socklen_t sa_len = sizeof(sa);
902
903 if (!(what & BSC_FD_READ))
904 return 0;
905
906 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
907 if (ret < 0) {
908 perror("accept");
909 return ret;
910 }
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200911 DEBUGP(DLINP, "accept()ed new %s link from %s\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100912 (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL",
913 inet_ntoa(sa.sin_addr));
914
915 ipc = alloc_conn();
916 if (!ipc) {
917 close(ret);
918 return -ENOMEM;
919 }
920
921 bfd = &ipc->fd;
922 bfd->fd = ret;
923 bfd->data = ipc;
924 bfd->priv_nr = listen_bfd->priv_nr;
925 bfd->cb = ipaccess_fd_cb;
926 bfd->when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200927 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100928 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200929 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Harald Welte2ca7c312009-12-23 22:44:04 +0100930 close(bfd->fd);
931 talloc_free(ipc);
932 return ret;
933 }
934
935 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200936 ret = ipaccess_send_id_req(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100937
938 return 0;
939}
940
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800941static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port)
942{
943 int ret;
944 struct sockaddr_in addr;
945 socklen_t len = sizeof(addr);
946 memset(&addr, 0, sizeof(addr));
947
948 addr.sin_family = AF_INET;
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800949 addr.sin_port = htons(port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800950 addr.sin_addr = ip;
951
952 ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len);
953 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200954 LOGP(DLINP, LOGL_ERROR, "Failed to forward GPRS message.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800955 }
956}
957
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200958static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800959{
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800960 struct ipa_bts_conn *bts;
961 char buf[4096];
962 int ret;
963 struct sockaddr_in sock;
964 socklen_t len = sizeof(sock);
965
966 /* 1. get the data... */
967 ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len);
968 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200969 LOGP(DLINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800970 return -1;
971 }
972
973 bts = bfd->data;
974
975 /* 2. figure out where to send it to */
976 if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200977 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from network.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800978 send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000);
979 } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200980 LOGP(DLINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n");
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800981 send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000);
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800982 } else {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200983 LOGP(DLINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800984 }
985
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800986 return 0;
987}
988
Harald Welte2ca7c312009-12-23 22:44:04 +0100989/* Actively connect to a BSC. */
990static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data)
991{
992 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200993 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100994 int ret, on = 1;
995
996 ipc = alloc_conn();
997 if (!ipc)
998 return NULL;
999
1000 ipc->bts_conn = data;
1001
1002 bfd = &ipc->fd;
1003 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1004 bfd->cb = ipaccess_fd_cb;
1005 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
1006 bfd->data = ipc;
1007 bfd->priv_nr = priv_nr;
1008
Holger Hans Peter Freythere18209c2013-12-12 16:16:35 +01001009 if (bfd->fd < 0) {
1010 LOGP(DLINP, LOGL_ERROR, "Could not create socket: %s\n",
1011 strerror(errno));
1012 talloc_free(ipc);
1013 return NULL;
1014 }
1015
Harald Welte2ca7c312009-12-23 22:44:04 +01001016 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1017
1018 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
1019 if (ret < 0) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001020 LOGP(DLINP, LOGL_ERROR, "Could not connect socket: %s\n",
Holger Hans Peter Freyther6cb9b232010-06-09 15:15:11 +08001021 inet_ntoa(sa->sin_addr));
Harald Welte2ca7c312009-12-23 22:44:04 +01001022 close(bfd->fd);
1023 talloc_free(ipc);
1024 return NULL;
1025 }
1026
1027 /* pre-fill tx_queue with identity request */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001028 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +01001029 if (ret < 0) {
1030 close(bfd->fd);
1031 talloc_free(ipc);
1032 return NULL;
1033 }
1034
1035 return ipc;
1036}
1037
1038static int ipaccess_proxy_setup(void)
1039{
1040 int ret;
1041
1042 ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy);
1043 if (!ipp)
1044 return -ENOMEM;
1045 INIT_LLIST_HEAD(&ipp->bts_list);
1046 ipp->reconn_timer.cb = reconn_tmr_cb;
1047 ipp->reconn_timer.data = ipp;
1048
1049 /* Listen for OML connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001050 ret = make_sock(&ipp->oml_listen_fd, IPPROTO_TCP, INADDR_ANY,
1051 IPA_TCP_PORT_OML, OML_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001052 if (ret < 0)
1053 return ret;
1054
1055 /* Listen for RSL connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001056 ret = make_sock(&ipp->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY,
1057 IPA_TCP_PORT_RSL, RSL_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001058
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001059 if (ret < 0)
1060 return ret;
1061
1062 /* Connect the GPRS NS Socket */
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001063 if (gprs_ns_ipaddr) {
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +08001064 inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr);
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001065 inet_aton(listen_ipaddr, &ipp->listen_addr);
1066 }
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001067
Harald Welte2ca7c312009-12-23 22:44:04 +01001068 return ret;
1069}
1070
1071static void signal_handler(int signal)
1072{
1073 fprintf(stdout, "signal %u received\n", signal);
1074
1075 switch (signal) {
1076 case SIGABRT:
1077 /* in case of abort, we want to obtain a talloc report
1078 * and then return to the caller, who will abort the process */
1079 case SIGUSR1:
1080 talloc_report_full(tall_bsc_ctx, stderr);
1081 break;
1082 default:
1083 break;
1084 }
1085}
1086
Harald Welted4ab13b2011-07-16 13:39:44 +02001087static void print_help(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001088{
1089 printf(" ipaccess-proxy is a proxy BTS.\n");
1090 printf(" -h --help. This help text.\n");
1091 printf(" -l --listen IP. The ip to listen to.\n");
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001092 printf(" -b --bsc IP. The BSC IP address.\n");
1093 printf(" -g --gprs IP. Take GPRS NS from that IP.\n");
1094 printf("\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001095 printf(" -s --disable-color. Disable the color inside the logging message.\n");
1096 printf(" -e --log-level number. Set the global loglevel.\n");
1097 printf(" -T --timestamp. Prefix every log message with a timestamp.\n");
1098 printf(" -V --version. Print the version of OpenBSC.\n");
1099}
1100
Harald Welted4ab13b2011-07-16 13:39:44 +02001101static void print_usage(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001102{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001103 printf("Usage: ipaccess-proxy [options]\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001104}
1105
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001106enum {
1107 IPA_PROXY_OPT_LISTEN_NONE = 0,
1108 IPA_PROXY_OPT_LISTEN_IP = (1 << 0),
1109 IPA_PROXY_OPT_BSC_IP = (1 << 1),
1110};
1111
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001112static void handle_options(int argc, char** argv)
1113{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001114 int options_mask = 0;
1115
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001116 /* disable explicit missing arguments error output from getopt_long */
1117 opterr = 0;
1118
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001119 while (1) {
1120 int option_index = 0, c;
1121 static struct option long_options[] = {
1122 {"help", 0, 0, 'h'},
1123 {"disable-color", 0, 0, 's'},
1124 {"timestamp", 0, 0, 'T'},
1125 {"log-level", 1, 0, 'e'},
1126 {"listen", 1, 0, 'l'},
1127 {"bsc", 1, 0, 'b'},
1128 {0, 0, 0, 0}
1129 };
1130
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001131 c = getopt_long(argc, argv, "hsTe:l:b:g:",
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001132 long_options, &option_index);
1133 if (c == -1)
1134 break;
1135
1136 switch (c) {
1137 case 'h':
1138 print_usage();
1139 print_help();
1140 exit(0);
1141 case 'l':
1142 listen_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001143 options_mask |= IPA_PROXY_OPT_LISTEN_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001144 break;
1145 case 'b':
1146 bsc_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001147 options_mask |= IPA_PROXY_OPT_BSC_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001148 break;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001149 case 'g':
1150 gprs_ns_ipaddr = optarg;
1151 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001152 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001153 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001154 break;
1155 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001156 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001157 break;
1158 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001159 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001160 break;
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001161 case '?':
1162 if (optopt) {
1163 printf("ERROR: missing mandatory argument "
1164 "for `%s' option\n", argv[optind-1]);
1165 } else {
1166 printf("ERROR: unknown option `%s'\n",
1167 argv[optind-1]);
1168 }
1169 print_usage();
1170 print_help();
1171 exit(EXIT_FAILURE);
1172 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001173 default:
1174 /* ignore */
1175 break;
1176 }
1177 }
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001178 if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP))
1179 != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) {
1180 printf("ERROR: You have to specify `--listen' and `--bsc' "
1181 "options at least.\n");
1182 print_usage();
1183 print_help();
1184 exit(EXIT_FAILURE);
1185 }
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001186}
1187
Harald Welte2ca7c312009-12-23 22:44:04 +01001188int main(int argc, char **argv)
1189{
1190 int rc;
1191
Harald Welte2ca7c312009-12-23 22:44:04 +01001192 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
1193
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001194 osmo_init_logging(&log_info);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +02001195 log_parse_category_mask(osmo_stderr_target, "DLINP:DLMI");
Harald Welte2ca7c312009-12-23 22:44:04 +01001196
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001197 handle_options(argc, argv);
1198
Harald Welte2ca7c312009-12-23 22:44:04 +01001199 rc = ipaccess_proxy_setup();
1200 if (rc < 0)
1201 exit(1);
1202
1203 signal(SIGUSR1, &signal_handler);
1204 signal(SIGABRT, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001205 osmo_init_ignore_signals();
Harald Welte2ca7c312009-12-23 22:44:04 +01001206
1207 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001208 osmo_select_main(0);
Harald Welte2ca7c312009-12-23 22:44:04 +01001209 }
1210}