blob: 1dd5b84564ebdfc7679785d8b9d92657ecfa7591 [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)
213 LOGP(DINP, 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) {
218 DEBUGP(DINP, "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)) {
226 DEBUGP(DINP, "could not even read header!?!\n");
227 msgb_free(msg);
228 return -EIO;
229 }
230 msgb_put(msg, ret);
231 msg->l2h = msg->data + sizeof(*hh);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200232 DEBUGP(DMI, "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)) {
235 DEBUGP(DINP, "length (%u/%u) disagrees with header(%u)\n",
236 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:
250 DEBUGP(DINP, "Unknown protocol 0x%02x, sending to "
251 "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:
267 DEBUGP(DINP, "Unknown protocol 0x%02x, sending to "
268 "OML FD\n", hh->proto);
269 case IPAC_PROTO_IPACCESS:
270 case IPAC_PROTO_OML:
271 other_conn = ipbc->bsc_oml_conn;
272 break;
273 }
274 break;
275 default:
276 DEBUGP(DINP, "Unknown filedescriptor priv_nr=%04x\n", bfd->priv_nr);
277 break;
278 }
279
280 if (other_conn) {
281 /* enqueue the message for TX on the respective FD */
282 msgb_enqueue(&other_conn->tx_queue, msg);
283 other_conn->fd.when |= BSC_FD_WRITE;
284 } else
285 msgb_free(msg);
286
287 return 0;
288}
289
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200290static int handle_udp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100291{
292 /* not implemented yet */
293 bfd->when &= ~BSC_FD_WRITE;
294
295 return -EIO;
296}
297
298/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200299static int udp_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100300{
301 int rc = 0;
302
303 if (what & BSC_FD_READ)
304 rc = handle_udp_read(bfd);
305 if (what & BSC_FD_WRITE)
306 rc = handle_udp_write(bfd);
307
308 return rc;
309}
310
311
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200312static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200313 uint16_t site_id, uint16_t bts_id,
314 uint16_t trx_id, struct tlv_parsed *tlvp,
Harald Welte2ca7c312009-12-23 22:44:04 +0100315 struct msgb *msg)
316{
317 struct ipa_bts_conn *ipbc;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200318 uint16_t udp_port;
Harald Welte2ca7c312009-12-23 22:44:04 +0100319 int ret = 0;
320 struct sockaddr_in sin;
321
322 memset(&sin, 0, sizeof(sin));
323 sin.sin_family = AF_INET;
324 inet_aton(bsc_ipaddr, &sin.sin_addr);
325
326 DEBUGP(DINP, "(%u/%u/%u) New BTS connection: ",
327 site_id, bts_id, trx_id);
328
329 /* OML needs to be established before RSL */
330 if ((bfd->priv_nr & 0xff) != OML_FROM_BTS) {
331 DEBUGPC(DINP, "Not a OML connection ?!?\n");
332 return -EIO;
333 }
334
335 /* allocate new BTS connection data structure */
336 ipbc = talloc_zero(tall_bsc_ctx, struct ipa_bts_conn);
337 if (!ipbc) {
338 ret = -ENOMEM;
339 goto err_out;
340 }
341
342 DEBUGPC(DINP, "Created BTS Conn data structure\n");
343 ipbc->ipp = ipp;
344 ipbc->unit_id.site_id = site_id;
345 ipbc->unit_id.bts_id = bts_id;
346 ipbc->oml_conn = ipc;
347 ipc->bts_conn = ipbc;
348
349 /* store the content of the ID TAGS for later reference */
350 store_idtags(ipbc, tlvp);
351 ipbc->id_resp_len = msg->len;
352 ipbc->id_resp = talloc_size(tall_bsc_ctx, ipbc->id_resp_len);
353 memcpy(ipbc->id_resp, msg->data, ipbc->id_resp_len);
354
355 /* Create OML TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100356 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100357 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
358 if (!ipbc->bsc_oml_conn) {
359 ret = -EIO;
360 goto err_bsc_conn;
361 }
362
363 DEBUGP(DINP, "(%u/%u/%u) OML Connected to BSC\n",
364 site_id, bts_id, trx_id);
365
366 /* Create UDP socket for BTS packet injection */
367 udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200368 ret = make_sock(&ipbc->udp_bts_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100369 UDP_TO_BTS, udp_fd_cb, ipbc);
370 if (ret < 0)
371 goto err_udp_bts;
372 DEBUGP(DINP, "(%u/%u/%u) Created UDP socket for injection "
373 "towards BTS at port %u\n", site_id, bts_id, trx_id, udp_port);
374
375 /* Create UDP socket for BSC packet injection */
376 udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100);
Pablo Neira Ayusoda2f7692011-04-05 18:33:28 +0200377 ret = make_sock(&ipbc->udp_bsc_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
Harald Welte2ca7c312009-12-23 22:44:04 +0100378 UDP_TO_BSC, udp_fd_cb, ipbc);
379 if (ret < 0)
380 goto err_udp_bsc;
381 DEBUGP(DINP, "(%u/%u/%u) Created UDP socket for injection "
382 "towards BSC at port %u\n", site_id, bts_id, trx_id, udp_port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800383
384
385 /* GPRS NS related code */
386 if (gprs_ns_ipaddr) {
387 struct sockaddr_in sock;
388 socklen_t len = sizeof(sock);
Pablo Neira Ayuso7e737002011-04-11 16:33:17 +0200389 struct in_addr addr;
390 uint32_t ip;
391
392 inet_aton(listen_ipaddr, &addr);
393 ip = ntohl(addr.s_addr); /* make_sock() needs host byte order */
394 ret = make_sock(&ipbc->gprs_ns_fd, IPPROTO_UDP, ip, 0, 0,
395 gprs_ns_cb, ipbc);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800396 if (ret < 0) {
397 LOGP(DINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
398 goto err_udp_bsc;
399 }
400
401 ret = getsockname(ipbc->gprs_ns_fd.fd, (struct sockaddr* ) &sock, &len);
402 ipbc->gprs_local_port = ntohs(sock.sin_port);
403 LOGP(DINP, LOGL_NOTICE,
404 "Created GPRS NS Socket. Listening on: %s:%d\n",
405 inet_ntoa(sock.sin_addr), ipbc->gprs_local_port);
406
407 ret = getpeername(bfd->fd, (struct sockaddr* ) &sock, &len);
408 ipbc->bts_addr = sock.sin_addr;
409 }
410
Harald Welte2ca7c312009-12-23 22:44:04 +0100411 llist_add(&ipbc->list, &ipp->bts_list);
412
413 return 0;
414
415err_udp_bsc:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200416 osmo_fd_unregister(&ipbc->udp_bts_fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100417err_udp_bts:
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200418 osmo_fd_unregister(&ipbc->bsc_oml_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100419 close(ipbc->bsc_oml_conn->fd.fd);
420 talloc_free(ipbc->bsc_oml_conn);
421 ipbc->bsc_oml_conn = NULL;
422err_bsc_conn:
423 talloc_free(ipbc->id_resp);
424 talloc_free(ipbc);
425#if 0
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200426 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100427 close(bfd->fd);
428 talloc_free(bfd);
429#endif
430err_out:
431 return ret;
432}
433
434static int ipaccess_rcvmsg(struct ipa_proxy_conn *ipc, struct msgb *msg,
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200435 struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100436{
437 struct tlv_parsed tlvp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200438 uint8_t msg_type = *(msg->l2h);
439 uint16_t site_id, bts_id, trx_id;
Harald Welte2ca7c312009-12-23 22:44:04 +0100440 struct ipa_bts_conn *ipbc;
441 int ret = 0;
442
443 switch (msg_type) {
444 case IPAC_MSGT_PING:
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200445 ret = ipaccess_send_pong(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100446 break;
447 case IPAC_MSGT_PONG:
448 DEBUGP(DMI, "PONG!\n");
449 break;
450 case IPAC_MSGT_ID_RESP:
451 DEBUGP(DMI, "ID_RESP ");
452 /* parse tags, search for Unit ID */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200453 ipaccess_idtag_parse(&tlvp, (uint8_t *)msg->l2h + 2,
Pablo Neira Ayuso625295b2011-04-07 14:15:16 +0200454 msgb_l2len(msg)-2);
Harald Welte2ca7c312009-12-23 22:44:04 +0100455 DEBUGP(DMI, "\n");
456
457 if (!TLVP_PRESENT(&tlvp, IPAC_IDTAG_UNIT)) {
458 LOGP(DINP, LOGL_ERROR, "No Unit ID in ID RESPONSE !?!\n");
459 return -EIO;
460 }
461
462 /* lookup BTS, create sign_link, ... */
Holger Hans Peter Freyther82ae7162010-03-30 15:20:46 +0200463 site_id = bts_id = trx_id = 0;
Pablo Neira Ayusoc281b4e2011-04-07 14:15:20 +0200464 ipaccess_parse_unitid((char *)TLVP_VAL(&tlvp, IPAC_IDTAG_UNIT),
465 &site_id, &bts_id, &trx_id);
Harald Welte2ca7c312009-12-23 22:44:04 +0100466 ipbc = find_bts_by_unitid(ipp, site_id, bts_id);
467 if (!ipbc) {
468 /* We have not found an ipbc (per-bts proxy instance)
469 * for this BTS yet. The first connection of a new BTS must
470 * be a OML connection. We allocate the associated data structures,
471 * and try to connect to the remote end */
472
473 return ipbc_alloc_connect(ipc, bfd, site_id, bts_id,
474 trx_id, &tlvp, msg);
475 /* if this fails, the caller will clean up bfd */
476 } else {
477 struct sockaddr_in sin;
478 memset(&sin, 0, sizeof(sin));
479 sin.sin_family = AF_INET;
480 inet_aton(bsc_ipaddr, &sin.sin_addr);
481
482 DEBUGP(DINP, "Identified BTS %u/%u/%u\n",
483 site_id, bts_id, trx_id);
484
485 if ((bfd->priv_nr & 0xff) != RSL_FROM_BTS) {
486 LOGP(DINP, LOGL_ERROR, "Second OML connection from "
487 "same BTS ?!?\n");
488 return 0;
489 }
490
Harald Welte36ac7752011-07-16 13:38:48 +0200491 if (trx_id >= MAX_TRX) {
Harald Welte2ca7c312009-12-23 22:44:04 +0100492 LOGP(DINP, LOGL_ERROR, "We don't support more "
493 "than %u TRX\n", MAX_TRX);
494 return -EINVAL;
495 }
496
497 ipc->bts_conn = ipbc;
498 /* store TRX number in higher 8 bit of the bfd private number */
499 bfd->priv_nr |= trx_id << 8;
500 ipbc->rsl_conn[trx_id] = ipc;
501
502 /* Create RSL TCP connection towards BSC */
Harald Welte87ed5cd2009-12-23 22:47:53 +0100503 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welte2ca7c312009-12-23 22:44:04 +0100504 ipbc->bsc_rsl_conn[trx_id] =
505 connect_bsc(&sin, RSL_TO_BSC | (trx_id << 8), ipbc);
506 if (!ipbc->bsc_oml_conn)
507 return -EIO;
508 DEBUGP(DINP, "(%u/%u/%u) Connected RSL to BSC\n",
509 site_id, bts_id, trx_id);
510 }
511 break;
512 case IPAC_MSGT_ID_GET:
513 DEBUGP(DMI, "ID_GET\n");
514 if ((bfd->priv_nr & 0xff) != OML_TO_BSC &&
515 (bfd->priv_nr & 0xff) != RSL_TO_BSC) {
516 DEBUGP(DINP, "IDentity REQuest from BTS ?!?\n");
517 return -EIO;
518 }
519 ipbc = ipc->bts_conn;
520 if (!ipbc) {
521 DEBUGP(DINP, "ID_GET from BSC before we have ID_RESP from BTS\n");
522 return -EIO;
523 }
524 ret = write(bfd->fd, ipbc->id_resp, ipbc->id_resp_len);
525 break;
526 case IPAC_MSGT_ID_ACK:
527 DEBUGP(DMI, "ID_ACK? -> ACK!\n");
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200528 ret = ipaccess_send_id_ack(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100529 break;
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800530 default:
531 LOGP(DMI, LOGL_ERROR, "Unhandled IPA type; %d\n", msg_type);
532 return 1;
533 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100534 }
535 return 0;
536}
537
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200538struct msgb *ipaccess_proxy_read_msg(struct osmo_fd *bfd, int *error)
Harald Welte2ca7c312009-12-23 22:44:04 +0100539{
540 struct msgb *msg = msgb_alloc(PROXY_ALLOC_SIZE, "Abis/IP");
541 struct ipaccess_head *hh;
542 int len, ret = 0;
543
544 if (!msg) {
545 *error = -ENOMEM;
546 return NULL;
547 }
548
549 /* first read our 3-byte header */
550 hh = (struct ipaccess_head *) msg->data;
551 ret = recv(bfd->fd, msg->data, 3, 0);
552 if (ret < 0) {
Harald Weltefb339572009-12-24 13:35:18 +0100553 if (errno != EAGAIN)
Harald Welteda956932009-12-24 12:49:43 +0100554 LOGP(DINP, LOGL_ERROR, "recv error: %s\n", strerror(errno));
Harald Welte2ca7c312009-12-23 22:44:04 +0100555 msgb_free(msg);
556 *error = ret;
557 return NULL;
558 } else if (ret == 0) {
559 msgb_free(msg);
560 *error = ret;
561 return NULL;
562 }
563
564 msgb_put(msg, ret);
565
566 /* then read te length as specified in header */
567 msg->l2h = msg->data + sizeof(*hh);
568 len = ntohs(hh->len);
569 ret = recv(bfd->fd, msg->l2h, len, 0);
570 if (ret < len) {
571 LOGP(DINP, LOGL_ERROR, "short read!\n");
572 msgb_free(msg);
573 *error = -EIO;
574 return NULL;
575 }
576 msgb_put(msg, ret);
577
578 return msg;
579}
580
581static struct ipa_proxy_conn *ipc_by_priv_nr(struct ipa_bts_conn *ipbc,
582 unsigned int priv_nr)
583{
584 struct ipa_proxy_conn *bsc_conn;
585 unsigned int trx_id = priv_nr >> 8;
586
587 switch (priv_nr & 0xff) {
588 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
589 bsc_conn = ipbc->bsc_oml_conn;
590 break;
591 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
592 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
593 break;
594 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
595 bsc_conn = ipbc->oml_conn;
596 break;
597 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
598 bsc_conn = ipbc->rsl_conn[trx_id];
599 break;
600 default:
601 bsc_conn = NULL;
602 break;
603 }
604 return bsc_conn;
605}
606
607static void reconn_tmr_cb(void *data)
608{
609 struct ipa_proxy *ipp = data;
610 struct ipa_bts_conn *ipbc;
611 struct sockaddr_in sin;
612 int i;
613
614 DEBUGP(DINP, "Running reconnect timer\n");
615
616 memset(&sin, 0, sizeof(sin));
617 sin.sin_family = AF_INET;
618 inet_aton(bsc_ipaddr, &sin.sin_addr);
619
620 llist_for_each_entry(ipbc, &ipp->bts_list, list) {
621 /* if OML to BSC is dead, try to restore it */
622 if (ipbc->oml_conn && !ipbc->bsc_oml_conn) {
Harald Welte87ed5cd2009-12-23 22:47:53 +0100623 sin.sin_port = htons(IPA_TCP_PORT_OML);
Harald Welte2ca7c312009-12-23 22:44:04 +0100624 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, 0);
625 LOGPC(DINP, LOGL_NOTICE, "OML Trying to reconnect\n");
626 ipbc->bsc_oml_conn = connect_bsc(&sin, OML_TO_BSC, ipbc);
627 if (!ipbc->bsc_oml_conn)
628 goto reschedule;
629 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, 0);
630 LOGPC(DINP, LOGL_NOTICE, "OML Reconnected\n");
631 }
632 /* if we (still) don't have a OML connection, skip RSL */
633 if (!ipbc->oml_conn || !ipbc->bsc_oml_conn)
634 continue;
635
636 for (i = 0; i < ARRAY_SIZE(ipbc->rsl_conn); i++) {
637 unsigned int priv_nr;
638 /* don't establish RSL links which we don't have */
639 if (!ipbc->rsl_conn[i])
640 continue;
641 if (ipbc->bsc_rsl_conn[i])
642 continue;
643 priv_nr = ipbc->rsl_conn[i]->fd.priv_nr;
644 priv_nr &= ~0xff;
645 priv_nr |= RSL_TO_BSC;
Harald Welte87ed5cd2009-12-23 22:47:53 +0100646 sin.sin_port = htons(IPA_TCP_PORT_RSL);
Harald Welte2ca7c312009-12-23 22:44:04 +0100647 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
648 LOGPC(DINP, LOGL_NOTICE, "RSL Trying to reconnect\n");
649 ipbc->bsc_rsl_conn[i] = connect_bsc(&sin, priv_nr, ipbc);
650 if (!ipbc->bsc_rsl_conn)
651 goto reschedule;
652 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, priv_nr >> 8);
653 LOGPC(DINP, LOGL_NOTICE, "RSL Reconnected\n");
654 }
655 }
656 return;
657
658reschedule:
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200659 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100660}
661
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200662static void handle_dead_socket(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100663{
664 struct ipa_proxy_conn *ipc = bfd->data; /* local conn */
665 struct ipa_proxy_conn *bsc_conn; /* remote conn */
666 struct ipa_bts_conn *ipbc = ipc->bts_conn;
667 unsigned int trx_id = bfd->priv_nr >> 8;
668 struct msgb *msg, *msg2;
669
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200670 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100671 close(bfd->fd);
672 bfd->fd = -1;
673
674 /* FIXME: clear tx_queue, remove all references, etc. */
675 llist_for_each_entry_safe(msg, msg2, &ipc->tx_queue, list)
676 msgb_free(msg);
677
678 switch (bfd->priv_nr & 0xff) {
679 case OML_FROM_BTS: /* incoming OML data from BTS, forward to BSC OML */
Pablo Neira Ayuso3c409c22011-03-27 21:31:48 +0200680 /* The BTS started a connection with us but we got no
681 * IPAC_MSGT_ID_RESP message yet, in that scenario we did not
682 * allocate the ipa_bts_conn structure. */
683 if (ipbc == NULL)
684 break;
Harald Welte2ca7c312009-12-23 22:44:04 +0100685 ipbc->oml_conn = NULL;
686 bsc_conn = ipbc->bsc_oml_conn;
687 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200688 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100689 close(bsc_conn->fd.fd);
690 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
691 msgb_free(msg);
692 talloc_free(bsc_conn);
693 ipbc->bsc_oml_conn = NULL;
694 /* FIXME: do we need to delete the entire ipbc ? */
695 break;
696 case RSL_FROM_BTS: /* incoming RSL data from BTS, forward to BSC RSL */
697 ipbc->rsl_conn[trx_id] = NULL;
698 bsc_conn = ipbc->bsc_rsl_conn[trx_id];
699 /* close the connection to the BSC */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200700 osmo_fd_unregister(&bsc_conn->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100701 close(bsc_conn->fd.fd);
702 llist_for_each_entry_safe(msg, msg2, &bsc_conn->tx_queue, list)
703 msgb_free(msg);
704 talloc_free(bsc_conn);
705 ipbc->bsc_rsl_conn[trx_id] = NULL;
706 break;
707 case OML_TO_BSC: /* incoming OML data from BSC, forward to BTS OML */
708 ipbc->bsc_oml_conn = NULL;
709 bsc_conn = ipbc->oml_conn;
710 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200711 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100712 break;
713 case RSL_TO_BSC: /* incoming RSL data from BSC, forward to BTS RSL */
714 ipbc->bsc_rsl_conn[trx_id] = NULL;
715 bsc_conn = ipbc->rsl_conn[trx_id];
716 /* start reconnect timer */
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200717 osmo_timer_schedule(&ipp->reconn_timer, 5, 0);
Harald Welte2ca7c312009-12-23 22:44:04 +0100718 break;
719 default:
720 bsc_conn = NULL;
721 break;
722 }
723
724 talloc_free(ipc);
725}
726
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800727static void patch_gprs_msg(struct ipa_bts_conn *ipbc, int priv_nr, struct msgb *msg)
728{
729 uint8_t *nsvci;
730
731 if ((priv_nr & 0xff) != OML_FROM_BTS && (priv_nr & 0xff) != OML_TO_BSC)
732 return;
733
734 if (msgb_l2len(msg) != 39)
735 return;
736
737 /*
738 * Check if this is a IPA Set Attribute or IPA Set Attribute ACK
739 * and if the FOM Class is GPRS NSVC0 and then we will patch it.
740 *
741 * The patch assumes the message looks like the one from the trace
742 * but we only match messages with a specific size anyway... So
743 * this hack should work just fine.
744 */
745
746 if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
747 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
748 msg->l2h[18] == 0xf5 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800749 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200750 ipbc->gprs_orig_port = *(uint16_t *)(nsvci+8);
751 ipbc->gprs_orig_ip = *(uint32_t *)(nsvci+10);
752 *(uint16_t *)(nsvci+8) = htons(ipbc->gprs_local_port);
753 *(uint32_t *)(nsvci+10) = ipbc->ipp->listen_addr.s_addr;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800754 } else if (msg->l2h[0] == 0x10 && msg->l2h[1] == 0x80 &&
755 msg->l2h[2] == 0x00 && msg->l2h[3] == 0x15 &&
756 msg->l2h[18] == 0xf6 && msg->l2h[19] == 0xf2) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800757 nsvci = &msg->l2h[23];
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200758 *(uint16_t *)(nsvci+8) = ipbc->gprs_orig_port;
759 *(uint32_t *)(nsvci+10) = ipbc->gprs_orig_ip;
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800760 }
761}
762
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200763static int handle_tcp_read(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100764{
765 struct ipa_proxy_conn *ipc = bfd->data;
766 struct ipa_bts_conn *ipbc = ipc->bts_conn;
767 struct ipa_proxy_conn *bsc_conn;
Harald Weltea16ef3d2009-12-23 23:35:51 +0100768 struct msgb *msg;
Harald Welte2ca7c312009-12-23 22:44:04 +0100769 struct ipaccess_head *hh;
770 int ret = 0;
771 char *btsbsc;
772
Harald Welte2ca7c312009-12-23 22:44:04 +0100773 if ((bfd->priv_nr & 0xff) <= 2)
774 btsbsc = "BTS";
775 else
776 btsbsc = "BSC";
777
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200778 msg = ipaccess_proxy_read_msg(bfd, &ret);
Harald Welte2ca7c312009-12-23 22:44:04 +0100779 if (!msg) {
780 if (ret == 0) {
781 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
782 LOGPC(DINP, LOGL_NOTICE, "%s disappeared, "
783 "dead socket\n", btsbsc);
784 handle_dead_socket(bfd);
785 }
786 return ret;
787 }
788
789 msgb_put(msg, ret);
790 logp_ipbc_uid(DMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200791 DEBUGPC(DMI, "RX<-%s: %s\n", btsbsc, osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100792
793 hh = (struct ipaccess_head *) msg->data;
794 if (hh->proto == IPAC_PROTO_IPACCESS) {
795 ret = ipaccess_rcvmsg(ipc, msg, bfd);
796 if (ret < 0) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200797 osmo_fd_unregister(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100798 close(bfd->fd);
799 bfd->fd = -1;
800 talloc_free(bfd);
Holger Hans Peter Freyther0897dad2010-06-09 17:38:59 +0800801 msgb_free(msg);
802 return ret;
803 } else if (ret == 0) {
804 /* we do not forward parts of the CCM protocol
805 * through the proxy but rather terminate it ourselves. */
806 msgb_free(msg);
807 return ret;
Harald Welte2ca7c312009-12-23 22:44:04 +0100808 }
Harald Welte2ca7c312009-12-23 22:44:04 +0100809 }
810
811 if (!ipbc) {
812 LOGP(DINP, LOGL_ERROR,
813 "received %s packet but no ipc->bts_conn?!?\n", btsbsc);
814 msgb_free(msg);
815 return -EIO;
816 }
817
818 bsc_conn = ipc_by_priv_nr(ipbc, bfd->priv_nr);
819 if (bsc_conn) {
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +0800820 if (gprs_ns_ipaddr)
821 patch_gprs_msg(ipbc, bfd->priv_nr, msg);
Harald Welte2ca7c312009-12-23 22:44:04 +0100822 /* enqueue packet towards BSC */
823 msgb_enqueue(&bsc_conn->tx_queue, msg);
824 /* mark respective filedescriptor as 'we want to write' */
825 bsc_conn->fd.when |= BSC_FD_WRITE;
826 } else {
827 logp_ipbc_uid(DINP, LOGL_INFO, ipbc, bfd->priv_nr >> 8);
828 LOGPC(DINP, LOGL_INFO, "Dropping packet from %s, "
829 "since remote connection is dead\n", btsbsc);
830 msgb_free(msg);
831 }
832
833 return ret;
834}
835
836/* a TCP socket is ready to be written to */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200837static int handle_tcp_write(struct osmo_fd *bfd)
Harald Welte2ca7c312009-12-23 22:44:04 +0100838{
839 struct ipa_proxy_conn *ipc = bfd->data;
840 struct ipa_bts_conn *ipbc = ipc->bts_conn;
841 struct llist_head *lh;
842 struct msgb *msg;
843 char *btsbsc;
844 int ret;
845
846 if ((bfd->priv_nr & 0xff) <= 2)
847 btsbsc = "BTS";
848 else
849 btsbsc = "BSC";
850
851
852 /* get the next msg for this timeslot */
853 if (llist_empty(&ipc->tx_queue)) {
854 bfd->when &= ~BSC_FD_WRITE;
855 return 0;
856 }
857 lh = ipc->tx_queue.next;
858 llist_del(lh);
859 msg = llist_entry(lh, struct msgb, list);
860
861 logp_ipbc_uid(DMI, LOGL_DEBUG, ipbc, bfd->priv_nr >> 8);
862 DEBUGPC(DMI, "TX %04x: %s\n", bfd->priv_nr,
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200863 osmo_hexdump(msg->data, msg->len));
Harald Welte2ca7c312009-12-23 22:44:04 +0100864
865 ret = send(bfd->fd, msg->data, msg->len, 0);
866 msgb_free(msg);
867
868 if (ret == 0) {
869 logp_ipbc_uid(DINP, LOGL_NOTICE, ipbc, bfd->priv_nr >> 8);
870 LOGP(DINP, LOGL_NOTICE, "%s disappeared, dead socket\n", btsbsc);
871 handle_dead_socket(bfd);
872 }
873
874 return ret;
875}
876
877/* callback from select.c in case one of the fd's can be read/written */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200878static int ipaccess_fd_cb(struct osmo_fd *bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100879{
880 int rc = 0;
881
882 if (what & BSC_FD_READ) {
883 rc = handle_tcp_read(bfd);
884 if (rc < 0)
885 return rc;
886 }
887 if (what & BSC_FD_WRITE)
888 rc = handle_tcp_write(bfd);
889
890 return rc;
891}
892
893/* callback of the listening filedescriptor */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200894static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
Harald Welte2ca7c312009-12-23 22:44:04 +0100895{
896 int ret;
897 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200898 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100899 struct sockaddr_in sa;
900 socklen_t sa_len = sizeof(sa);
901
902 if (!(what & BSC_FD_READ))
903 return 0;
904
905 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
906 if (ret < 0) {
907 perror("accept");
908 return ret;
909 }
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +0200910 DEBUGP(DINP, "accept()ed new %s link from %s\n",
Harald Welte2ca7c312009-12-23 22:44:04 +0100911 (listen_bfd->priv_nr & 0xff) == OML_FROM_BTS ? "OML" : "RSL",
912 inet_ntoa(sa.sin_addr));
913
914 ipc = alloc_conn();
915 if (!ipc) {
916 close(ret);
917 return -ENOMEM;
918 }
919
920 bfd = &ipc->fd;
921 bfd->fd = ret;
922 bfd->data = ipc;
923 bfd->priv_nr = listen_bfd->priv_nr;
924 bfd->cb = ipaccess_fd_cb;
925 bfd->when = BSC_FD_READ;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200926 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100927 if (ret < 0) {
928 LOGP(DINP, LOGL_ERROR, "could not register FD\n");
929 close(bfd->fd);
930 talloc_free(ipc);
931 return ret;
932 }
933
934 /* Request ID. FIXME: request LOCATION, HW/SW VErsion, Unit Name, Serno */
Pablo Neira Ayuso22f58a92011-04-07 14:15:06 +0200935 ret = ipaccess_send_id_req(bfd->fd);
Harald Welte2ca7c312009-12-23 22:44:04 +0100936
937 return 0;
938}
939
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800940static void send_ns(int fd, const char *buf, int size, struct in_addr ip, int port)
941{
942 int ret;
943 struct sockaddr_in addr;
944 socklen_t len = sizeof(addr);
945 memset(&addr, 0, sizeof(addr));
946
947 addr.sin_family = AF_INET;
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800948 addr.sin_port = htons(port);
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800949 addr.sin_addr = ip;
950
951 ret = sendto(fd, buf, size, 0, (struct sockaddr *) &addr, len);
952 if (ret < 0) {
953 LOGP(DINP, LOGL_ERROR, "Failed to forward GPRS message.\n");
954 }
955}
956
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200957static int gprs_ns_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800958{
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800959 struct ipa_bts_conn *bts;
960 char buf[4096];
961 int ret;
962 struct sockaddr_in sock;
963 socklen_t len = sizeof(sock);
964
965 /* 1. get the data... */
966 ret = recvfrom(bfd->fd, buf, sizeof(buf), 0, (struct sockaddr *) &sock, &len);
967 if (ret < 0) {
968 LOGP(DINP, LOGL_ERROR, "Failed to recv GPRS NS msg: %s.\n", strerror(errno));
969 return -1;
970 }
971
972 bts = bfd->data;
973
974 /* 2. figure out where to send it to */
975 if (memcmp(&sock.sin_addr, &ipp->gprs_addr, sizeof(sock.sin_addr)) == 0) {
976 LOGP(DINP, LOGL_DEBUG, "GPRS NS msg from network.\n");
977 send_ns(bfd->fd, buf, ret, bts->bts_addr, 23000);
978 } else if (memcmp(&sock.sin_addr, &bts->bts_addr, sizeof(sock.sin_addr)) == 0) {
979 LOGP(DINP, LOGL_DEBUG, "GPRS NS msg from BTS.\n");
980 send_ns(bfd->fd, buf, ret, ipp->gprs_addr, 23000);
Holger Hans Peter Freyther21e1c0d2010-06-10 15:56:26 +0800981 } else {
982 LOGP(DINP, LOGL_ERROR, "Unknown GPRS source: %s\n", inet_ntoa(sock.sin_addr));
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +0800983 }
984
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +0800985 return 0;
986}
987
Harald Welte2ca7c312009-12-23 22:44:04 +0100988/* Actively connect to a BSC. */
989static struct ipa_proxy_conn *connect_bsc(struct sockaddr_in *sa, int priv_nr, void *data)
990{
991 struct ipa_proxy_conn *ipc;
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200992 struct osmo_fd *bfd;
Harald Welte2ca7c312009-12-23 22:44:04 +0100993 int ret, on = 1;
994
995 ipc = alloc_conn();
996 if (!ipc)
997 return NULL;
998
999 ipc->bts_conn = data;
1000
1001 bfd = &ipc->fd;
1002 bfd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1003 bfd->cb = ipaccess_fd_cb;
1004 bfd->when = BSC_FD_READ | BSC_FD_WRITE;
1005 bfd->data = ipc;
1006 bfd->priv_nr = priv_nr;
1007
1008 setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1009
1010 ret = connect(bfd->fd, (struct sockaddr *) sa, sizeof(*sa));
1011 if (ret < 0) {
Holger Hans Peter Freyther6cb9b232010-06-09 15:15:11 +08001012 LOGP(DINP, LOGL_ERROR, "Could not connect socket: %s\n",
1013 inet_ntoa(sa->sin_addr));
Harald Welte2ca7c312009-12-23 22:44:04 +01001014 close(bfd->fd);
1015 talloc_free(ipc);
1016 return NULL;
1017 }
1018
1019 /* pre-fill tx_queue with identity request */
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001020 ret = osmo_fd_register(bfd);
Harald Welte2ca7c312009-12-23 22:44:04 +01001021 if (ret < 0) {
1022 close(bfd->fd);
1023 talloc_free(ipc);
1024 return NULL;
1025 }
1026
1027 return ipc;
1028}
1029
1030static int ipaccess_proxy_setup(void)
1031{
1032 int ret;
1033
1034 ipp = talloc_zero(tall_bsc_ctx, struct ipa_proxy);
1035 if (!ipp)
1036 return -ENOMEM;
1037 INIT_LLIST_HEAD(&ipp->bts_list);
1038 ipp->reconn_timer.cb = reconn_tmr_cb;
1039 ipp->reconn_timer.data = ipp;
1040
1041 /* Listen for OML connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001042 ret = make_sock(&ipp->oml_listen_fd, IPPROTO_TCP, INADDR_ANY,
1043 IPA_TCP_PORT_OML, OML_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001044 if (ret < 0)
1045 return ret;
1046
1047 /* Listen for RSL connections */
Pablo Neira Ayuso3ab864a2011-04-07 14:15:02 +02001048 ret = make_sock(&ipp->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY,
1049 IPA_TCP_PORT_RSL, RSL_FROM_BTS, listen_fd_cb, NULL);
Harald Welte2ca7c312009-12-23 22:44:04 +01001050
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001051 if (ret < 0)
1052 return ret;
1053
1054 /* Connect the GPRS NS Socket */
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001055 if (gprs_ns_ipaddr) {
Holger Hans Peter Freyther952aba72010-06-09 17:07:43 +08001056 inet_aton(gprs_ns_ipaddr, &ipp->gprs_addr);
Holger Hans Peter Freyther4fe22cc2010-06-10 15:57:08 +08001057 inet_aton(listen_ipaddr, &ipp->listen_addr);
1058 }
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001059
Harald Welte2ca7c312009-12-23 22:44:04 +01001060 return ret;
1061}
1062
1063static void signal_handler(int signal)
1064{
1065 fprintf(stdout, "signal %u received\n", signal);
1066
1067 switch (signal) {
1068 case SIGABRT:
1069 /* in case of abort, we want to obtain a talloc report
1070 * and then return to the caller, who will abort the process */
1071 case SIGUSR1:
1072 talloc_report_full(tall_bsc_ctx, stderr);
1073 break;
1074 default:
1075 break;
1076 }
1077}
1078
Harald Welted4ab13b2011-07-16 13:39:44 +02001079static void print_help(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001080{
1081 printf(" ipaccess-proxy is a proxy BTS.\n");
1082 printf(" -h --help. This help text.\n");
1083 printf(" -l --listen IP. The ip to listen to.\n");
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001084 printf(" -b --bsc IP. The BSC IP address.\n");
1085 printf(" -g --gprs IP. Take GPRS NS from that IP.\n");
1086 printf("\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001087 printf(" -s --disable-color. Disable the color inside the logging message.\n");
1088 printf(" -e --log-level number. Set the global loglevel.\n");
1089 printf(" -T --timestamp. Prefix every log message with a timestamp.\n");
1090 printf(" -V --version. Print the version of OpenBSC.\n");
1091}
1092
Harald Welted4ab13b2011-07-16 13:39:44 +02001093static void print_usage(void)
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001094{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001095 printf("Usage: ipaccess-proxy [options]\n");
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001096}
1097
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001098enum {
1099 IPA_PROXY_OPT_LISTEN_NONE = 0,
1100 IPA_PROXY_OPT_LISTEN_IP = (1 << 0),
1101 IPA_PROXY_OPT_BSC_IP = (1 << 1),
1102};
1103
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001104static void handle_options(int argc, char** argv)
1105{
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001106 int options_mask = 0;
1107
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001108 /* disable explicit missing arguments error output from getopt_long */
1109 opterr = 0;
1110
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001111 while (1) {
1112 int option_index = 0, c;
1113 static struct option long_options[] = {
1114 {"help", 0, 0, 'h'},
1115 {"disable-color", 0, 0, 's'},
1116 {"timestamp", 0, 0, 'T'},
1117 {"log-level", 1, 0, 'e'},
1118 {"listen", 1, 0, 'l'},
1119 {"bsc", 1, 0, 'b'},
1120 {0, 0, 0, 0}
1121 };
1122
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001123 c = getopt_long(argc, argv, "hsTe:l:b:g:",
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001124 long_options, &option_index);
1125 if (c == -1)
1126 break;
1127
1128 switch (c) {
1129 case 'h':
1130 print_usage();
1131 print_help();
1132 exit(0);
1133 case 'l':
1134 listen_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001135 options_mask |= IPA_PROXY_OPT_LISTEN_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001136 break;
1137 case 'b':
1138 bsc_ipaddr = optarg;
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001139 options_mask |= IPA_PROXY_OPT_BSC_IP;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001140 break;
Holger Hans Peter Freyther6147c5d2010-06-09 16:20:39 +08001141 case 'g':
1142 gprs_ns_ipaddr = optarg;
1143 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001144 case 's':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001145 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001146 break;
1147 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001148 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001149 break;
1150 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001151 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001152 break;
Pablo Neira Ayuso25ffe542011-04-11 16:33:03 +02001153 case '?':
1154 if (optopt) {
1155 printf("ERROR: missing mandatory argument "
1156 "for `%s' option\n", argv[optind-1]);
1157 } else {
1158 printf("ERROR: unknown option `%s'\n",
1159 argv[optind-1]);
1160 }
1161 print_usage();
1162 print_help();
1163 exit(EXIT_FAILURE);
1164 break;
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001165 default:
1166 /* ignore */
1167 break;
1168 }
1169 }
Pablo Neira Ayuso9f1294d2011-04-07 18:47:39 +02001170 if ((options_mask & (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP))
1171 != (IPA_PROXY_OPT_LISTEN_IP | IPA_PROXY_OPT_BSC_IP)) {
1172 printf("ERROR: You have to specify `--listen' and `--bsc' "
1173 "options at least.\n");
1174 print_usage();
1175 print_help();
1176 exit(EXIT_FAILURE);
1177 }
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001178}
1179
Harald Welte2ca7c312009-12-23 22:44:04 +01001180int main(int argc, char **argv)
1181{
1182 int rc;
1183
Harald Welte2ca7c312009-12-23 22:44:04 +01001184 tall_bsc_ctx = talloc_named_const(NULL, 1, "ipaccess-proxy");
1185
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001186 osmo_init_logging(&log_info);
1187 log_parse_category_mask(osmo_stderr_target, "DINP:DMI");
Harald Welte2ca7c312009-12-23 22:44:04 +01001188
Holger Hans Peter Freyther1a0c2b72010-06-09 14:59:09 +08001189 handle_options(argc, argv);
1190
Harald Welte2ca7c312009-12-23 22:44:04 +01001191 rc = ipaccess_proxy_setup();
1192 if (rc < 0)
1193 exit(1);
1194
1195 signal(SIGUSR1, &signal_handler);
1196 signal(SIGABRT, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001197 osmo_init_ignore_signals();
Harald Welte2ca7c312009-12-23 22:44:04 +01001198
1199 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +02001200 osmo_select_main(0);
Harald Welte2ca7c312009-12-23 22:44:04 +01001201 }
1202}