blob: 4555b31a47bd540e5b250629d921d92b90e11db0 [file] [log] [blame]
Philipp Maierb4999b62016-10-26 15:19:41 +02001/* pcu_sock.c: Connect from PCU via unix domain socket */
2
3/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2009-2012 by Andreas Eversberg <jolly@eversberg.eu>
5 * (C) 2012 by Holger Hans Peter Freyther
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <stdio.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
29#include <assert.h>
30#include <sys/socket.h>
31#include <sys/un.h>
32
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/select.h>
35#include <osmocom/core/socket.h>
36#include <osmocom/core/logging.h>
37#include <osmocom/gsm/l1sap.h>
38#include <osmocom/gsm/gsm0502.h>
39
40#include <openbsc/gsm_data.h>
41#include <openbsc/pcu_if.h>
42#include <openbsc/pcuif_proto.h>
43#include <openbsc/signal.h>
44#include <openbsc/debug.h>
45#include <openbsc/abis_rsl.h>
46
47static int pcu_sock_send(struct gsm_bts *bts, struct msgb *msg);
48uint32_t trx_get_hlayer1(struct gsm_bts_trx *trx);
49int pcu_direct = 0;
50
51static const char *sapi_string[] = {
52 [PCU_IF_SAPI_RACH] = "RACH",
53 [PCU_IF_SAPI_AGCH] = "AGCH",
54 [PCU_IF_SAPI_PCH] = "PCH",
55 [PCU_IF_SAPI_BCCH] = "BCCH",
56 [PCU_IF_SAPI_PDTCH] = "PDTCH",
57 [PCU_IF_SAPI_PRACH] = "PRACH",
58 [PCU_IF_SAPI_PTCCH] = "PTCCH",
59};
60
Philipp Maierb4999b62016-10-26 15:19:41 +020061/* Check if BTS has a PCU connection */
62static bool pcu_connected(struct gsm_bts *bts)
63{
64 struct pcu_sock_state *state = bts->pcu_state;
65
66 if (!state)
67 return false;
68 if (state->conn_bfd.fd <= 0)
69 return false;
70 return true;
71}
72
73/*
74 * PCU messages
75 */
76
77/* Set up an message buffer to package an pcu interface message */
78struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
79{
80 struct msgb *msg;
81 struct gsm_pcu_if *pcu_prim;
82
83 msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
84 if (!msg)
85 return NULL;
86
87 msgb_put(msg, sizeof(struct gsm_pcu_if));
88 pcu_prim = (struct gsm_pcu_if *) msg->data;
89 pcu_prim->msg_type = msg_type;
90 pcu_prim->bts_nr = bts_nr;
91
92 return msg;
93}
94
95/* Helper function exclusivly used by pcu_if_signal_cb() */
96static bool ts_should_be_pdch(struct gsm_bts_trx_ts *ts) {
97 if (ts->pchan == GSM_PCHAN_PDCH)
98 return true;
99 if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) {
100 /* When we're busy deactivating the PDCH, we first set
101 * DEACT_PENDING, tell the PCU about it and wait for a
102 * response. So DEACT_PENDING means "no PDCH" to the PCU.
103 * Similarly, when we're activating PDCH, we set the
104 * ACT_PENDING and wait for an activation response from the
105 * PCU, so ACT_PENDING means "is PDCH". */
106 if (ts->flags & TS_F_PDCH_ACTIVE)
107 return !(ts->flags & TS_F_PDCH_DEACT_PENDING);
108 else
109 return (ts->flags & TS_F_PDCH_ACT_PENDING);
110 }
111 if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
112 /*
113 * When we're busy de-/activating the PDCH, we first set
114 * ts->dyn.pchan_want, tell the PCU about it and wait for a
115 * response. So only care about dyn.pchan_want here.
116 */
117 return ts->dyn.pchan_want == GSM_PCHAN_PDCH;
118 }
119 return false;
120}
121
122/* Send BTS properties to the PCU */
123static int pcu_tx_info_ind(struct gsm_bts *bts)
124{
125 struct msgb *msg;
126 struct gsm_pcu_if *pcu_prim;
127 struct gsm_pcu_if_info_ind *info_ind;
128 struct gprs_rlc_cfg *rlcc;
129 struct gsm_bts_gprs_nsvc *nsvc;
130 struct gsm_bts_trx *trx;
131 struct gsm_bts_trx_ts *ts;
132 int i, j;
133
134 OSMO_ASSERT(bts);
135 OSMO_ASSERT(bts->network);
136
137 LOGP(DPCU, LOGL_INFO, "Sending info for BTS %d\n",bts->nr);
138
139 rlcc = &bts->gprs.cell.rlc_cfg;
140
141 msg = pcu_msgb_alloc(PCU_IF_MSG_INFO_IND, bts->nr);
142 if (!msg)
143 return -ENOMEM;
144
145 pcu_prim = (struct gsm_pcu_if *) msg->data;
146 info_ind = &pcu_prim->u.info_ind;
147 info_ind->version = PCU_IF_VERSION;
148 info_ind->flags |= PCU_IF_FLAG_ACTIVE;
149
150 if (pcu_direct)
151 info_ind->flags |= PCU_IF_FLAG_SYSMO;
152
153 /* RAI */
154 info_ind->mcc = bts->network->country_code;
155 info_ind->mnc = bts->network->network_code;
156 info_ind->lac = bts->location_area_code;
157 info_ind->rac = bts->gprs.rac;
158
159 /* NSE */
160 info_ind->nsei = bts->gprs.nse.nsei;
161 memcpy(info_ind->nse_timer, bts->gprs.nse.timer, 7);
162 memcpy(info_ind->cell_timer, bts->gprs.cell.timer, 11);
163
164 /* cell attributes */
165 info_ind->cell_id = bts->cell_identity;
166 info_ind->repeat_time = rlcc->paging.repeat_time;
167 info_ind->repeat_count = rlcc->paging.repeat_count;
168 info_ind->bvci = bts->gprs.cell.bvci;
169 info_ind->t3142 = rlcc->parameter[RLC_T3142];
170 info_ind->t3169 = rlcc->parameter[RLC_T3169];
171 info_ind->t3191 = rlcc->parameter[RLC_T3191];
172 info_ind->t3193_10ms = rlcc->parameter[RLC_T3193];
173 info_ind->t3195 = rlcc->parameter[RLC_T3195];
174 info_ind->n3101 = rlcc->parameter[RLC_N3101];
175 info_ind->n3103 = rlcc->parameter[RLC_N3103];
176 info_ind->n3105 = rlcc->parameter[RLC_N3105];
177 info_ind->cv_countdown = rlcc->parameter[CV_COUNTDOWN];
178 if (rlcc->cs_mask & (1 << GPRS_CS1))
179 info_ind->flags |= PCU_IF_FLAG_CS1;
180 if (rlcc->cs_mask & (1 << GPRS_CS2))
181 info_ind->flags |= PCU_IF_FLAG_CS2;
182 if (rlcc->cs_mask & (1 << GPRS_CS3))
183 info_ind->flags |= PCU_IF_FLAG_CS3;
184 if (rlcc->cs_mask & (1 << GPRS_CS4))
185 info_ind->flags |= PCU_IF_FLAG_CS4;
186 if (bts->gprs.mode == BTS_GPRS_EGPRS) {
187 if (rlcc->cs_mask & (1 << GPRS_MCS1))
188 info_ind->flags |= PCU_IF_FLAG_MCS1;
189 if (rlcc->cs_mask & (1 << GPRS_MCS2))
190 info_ind->flags |= PCU_IF_FLAG_MCS2;
191 if (rlcc->cs_mask & (1 << GPRS_MCS3))
192 info_ind->flags |= PCU_IF_FLAG_MCS3;
193 if (rlcc->cs_mask & (1 << GPRS_MCS4))
194 info_ind->flags |= PCU_IF_FLAG_MCS4;
195 if (rlcc->cs_mask & (1 << GPRS_MCS5))
196 info_ind->flags |= PCU_IF_FLAG_MCS5;
197 if (rlcc->cs_mask & (1 << GPRS_MCS6))
198 info_ind->flags |= PCU_IF_FLAG_MCS6;
199 if (rlcc->cs_mask & (1 << GPRS_MCS7))
200 info_ind->flags |= PCU_IF_FLAG_MCS7;
201 if (rlcc->cs_mask & (1 << GPRS_MCS8))
202 info_ind->flags |= PCU_IF_FLAG_MCS8;
203 if (rlcc->cs_mask & (1 << GPRS_MCS9))
204 info_ind->flags |= PCU_IF_FLAG_MCS9;
205 }
206#warning "isn't dl_tbf_ext wrong?: * 10 and no ntohs"
207 info_ind->dl_tbf_ext = rlcc->parameter[T_DL_TBF_EXT];
208#warning "isn't ul_tbf_ext wrong?: * 10 and no ntohs"
209 info_ind->ul_tbf_ext = rlcc->parameter[T_UL_TBF_EXT];
210 info_ind->initial_cs = rlcc->initial_cs;
211 info_ind->initial_mcs = rlcc->initial_mcs;
212
213 /* NSVC */
Harald Weltee586f412016-11-17 18:39:36 +0100214 for (i = 0; i < ARRAY_SIZE(info_ind->nsvci); i++) {
Philipp Maierb4999b62016-10-26 15:19:41 +0200215 nsvc = &bts->gprs.nsvc[i];
216 info_ind->nsvci[i] = nsvc->nsvci;
217 info_ind->local_port[i] = nsvc->local_port;
218 info_ind->remote_port[i] = nsvc->remote_port;
219 info_ind->remote_ip[i] = nsvc->remote_ip;
220 }
221
Harald Weltee586f412016-11-17 18:39:36 +0100222 for (i = 0; i < ARRAY_SIZE(info_ind->trx); i++) {
Harald Welte67798612016-11-17 18:10:10 +0100223 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200224 if (!trx)
225 break;
226 info_ind->trx[i].pdch_mask = 0;
227 info_ind->trx[i].arfcn = trx->arfcn;
Harald Weltee586f412016-11-17 18:39:36 +0100228 for (j = 0; j < ARRAY_SIZE(trx->ts); j++) {
Philipp Maierb4999b62016-10-26 15:19:41 +0200229 ts = &trx->ts[j];
230 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
231 && ts_should_be_pdch(ts)) {
232 info_ind->trx[i].pdch_mask |= (1 << j);
233 info_ind->trx[i].tsc[j] =
234 (ts->tsc >= 0) ? ts->tsc : bts->bsic & 7;
235 LOGP(DPCU, LOGL_INFO, "trx=%d ts=%d: "
236 "available (tsc=%d arfcn=%d)\n",
237 trx->nr, ts->nr,
238 info_ind->trx[i].tsc[j],
239 info_ind->trx[i].arfcn);
240 }
241 }
242 }
243
244 return pcu_sock_send(bts, msg);
245}
246
247void pcu_info_update(struct gsm_bts *bts)
248{
249 if (pcu_connected(bts))
250 pcu_tx_info_ind(bts);
251}
252
253/* Forward rach indication to PCU */
254int pcu_tx_rach_ind(struct gsm_bts *bts, int16_t qta, uint16_t ra, uint32_t fn,
255 uint8_t is_11bit, enum ph_burst_type burst_type)
256{
257 struct msgb *msg;
258 struct gsm_pcu_if *pcu_prim;
259 struct gsm_pcu_if_rach_ind *rach_ind;
260
261 /* Bail if no PCU is connected */
262 if (!pcu_connected(bts)) {
263 LOGP(DRSL, LOGL_ERROR, "BTS %d CHAN RQD(GPRS) but PCU not "
264 "connected!\n", bts->nr);
265 return -ENODEV;
266 }
267
268 LOGP(DPCU, LOGL_INFO, "Sending RACH indication: qta=%d, ra=%d, "
269 "fn=%d\n", qta, ra, fn);
270
271 msg = pcu_msgb_alloc(PCU_IF_MSG_RACH_IND, bts->nr);
272 if (!msg)
273 return -ENOMEM;
274 pcu_prim = (struct gsm_pcu_if *) msg->data;
275 rach_ind = &pcu_prim->u.rach_ind;
276
277 rach_ind->sapi = PCU_IF_SAPI_RACH;
278 rach_ind->ra = ra;
279 rach_ind->qta = qta;
280 rach_ind->fn = fn;
281 rach_ind->is_11bit = is_11bit;
282 rach_ind->burst_type = burst_type;
283
284 return pcu_sock_send(bts, msg);
285}
286
Harald Welte854bcc22016-11-17 20:54:47 +0100287/* we need to decode the raw RR paging messsage (see PCU code
288 * Encoding::write_paging_request) and extract the mobile identity
289 * (P-TMSI) from it */
290static int pcu_rx_rr_paging(struct gsm_bts *bts, uint8_t paging_group,
291 const uint8_t *raw_rr_msg)
292{
293 struct gsm48_hdr *gsmh = (struct gsm48_hdr *) raw_rr_msg;
294 struct gsm48_paging1 *p1 = (struct gsm48_paging1 *) gsmh;
295 uint8_t chan_needed;
296 unsigned int mi_len;
297 uint8_t *mi;
298 int rc;
299
300 switch (gsmh->msg_type) {
301 case GSM48_MT_RR_PAG_REQ_1:
302 chan_needed = (p1->cneed2 << 2) | p1->cneed1;
303 mi_len = p1->data[0];
304 mi = p1->data+1;
305 /* FIXME: why does rsl_paging_cmd add 2 to mi? */
306 rc = rsl_paging_cmd(bts, paging_group, mi_len, mi,
307 chan_needed, true);
308 break;
309 case GSM48_MT_RR_PAG_REQ_2:
310 case GSM48_MT_RR_PAG_REQ_3:
311 LOGP(DPCU, LOGL_ERROR, "PCU Sends unsupported paging "
312 "request type\n");
313 rc = -EINVAL;
314 break;
315 }
316
317 return rc;
318}
319
Philipp Maierb4999b62016-10-26 15:19:41 +0200320static int pcu_rx_data_req(struct gsm_bts *bts, uint8_t msg_type,
321 struct gsm_pcu_if_data *data_req)
322{
323 uint8_t is_ptcch;
324 struct gsm_bts_trx *trx;
325 struct gsm_bts_trx_ts *ts;
326 struct msgb *msg;
Harald Welte854bcc22016-11-17 20:54:47 +0100327 char imsi_digit_buf[4];
328 uint8_t pag_grp;
Philipp Maierb4999b62016-10-26 15:19:41 +0200329 int rc = 0;
330
331 LOGP(DPCU, LOGL_DEBUG, "Data request received: sapi=%s arfcn=%d "
332 "block=%d data=%s\n", sapi_string[data_req->sapi],
333 data_req->arfcn, data_req->block_nr,
334 osmo_hexdump(data_req->data, data_req->len));
335
336 switch (data_req->sapi) {
337 case PCU_IF_SAPI_PCH:
Harald Welte854bcc22016-11-17 20:54:47 +0100338 /* the first three bytes are the last three digits of
339 * the IMSI, which we need to compute the paging group */
340 imsi_digit_buf[0] = data_req->data[0];
341 imsi_digit_buf[1] = data_req->data[1];
342 imsi_digit_buf[2] = data_req->data[2];
343 imsi_digit_buf[3] = '\0';
344 pag_grp = gsm0502_calc_paging_group(&bts->si_common.chan_desc,
345 str_to_imsi(imsi_digit_buf));
346 pcu_rx_rr_paging(bts, pag_grp, data_req->data+3);
Philipp Maierb4999b62016-10-26 15:19:41 +0200347 break;
348 case PCU_IF_SAPI_AGCH:
349 msg = msgb_alloc(data_req->len, "pcu_agch");
350 if (!msg) {
351 rc = -ENOMEM;
352 break;
353 }
354 msg->l3h = msgb_put(msg, data_req->len);
355 memcpy(msg->l3h, data_req->data, data_req->len);
356
357 if (rsl_imm_assign_cmd(bts, msg->len, msg->data)) {
358 msgb_free(msg);
359 rc = -EIO;
360 }
361 break;
362 default:
363 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
364 "unsupported sapi %d\n", data_req->sapi);
365 rc = -EINVAL;
366 }
367
368 return rc;
369}
370
371static int pcu_rx(struct gsm_network *net, uint8_t msg_type,
372 struct gsm_pcu_if *pcu_prim)
373{
374 int rc = 0;
375 struct gsm_bts *bts;
376
377 /* FIXME: allow multiple BTS */
378 bts = llist_entry(net->bts_list.next, struct gsm_bts, list);
379
380 switch (msg_type) {
381 case PCU_IF_MSG_DATA_REQ:
382 case PCU_IF_MSG_PAG_REQ:
383 rc = pcu_rx_data_req(bts, msg_type, &pcu_prim->u.data_req);
384 break;
385 default:
386 LOGP(DPCU, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
387 msg_type);
388 rc = -EINVAL;
389 }
390
391 return rc;
392}
393
394/*
395 * PCU socket interface
396 */
397
398static int pcu_sock_send(struct gsm_bts *bts, struct msgb *msg)
399{
400 struct pcu_sock_state *state = bts->pcu_state;
401 struct osmo_fd *conn_bfd;
402 struct gsm_pcu_if *pcu_prim = (struct gsm_pcu_if *) msg->data;
403
404 if (!state) {
405 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
406 LOGP(DPCU, LOGL_INFO, "PCU socket not created, "
407 "dropping message\n");
408 msgb_free(msg);
409 return -EINVAL;
410 }
411 conn_bfd = &state->conn_bfd;
412 if (conn_bfd->fd <= 0) {
413 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
414 LOGP(DPCU, LOGL_NOTICE, "PCU socket not connected, "
415 "dropping message\n");
416 msgb_free(msg);
417 return -EIO;
418 }
419 msgb_enqueue(&state->upqueue, msg);
420 conn_bfd->when |= BSC_FD_WRITE;
421
422 return 0;
423}
424
425static void pcu_sock_close(struct pcu_sock_state *state)
426{
427 struct osmo_fd *bfd = &state->conn_bfd;
428 struct gsm_bts *bts;
429 struct gsm_bts_trx *trx;
430 struct gsm_bts_trx_ts *ts;
431 int i, j;
432
433 /* FIXME: allow multiple BTS */
434 bts = llist_entry(state->net->bts_list.next, struct gsm_bts, list);
435
436 LOGP(DPCU, LOGL_NOTICE, "PCU socket has LOST connection\n");
437
438 close(bfd->fd);
439 bfd->fd = -1;
440 osmo_fd_unregister(bfd);
441
442 /* re-enable the generation of ACCEPT for new connections */
443 state->listen_bfd.when |= BSC_FD_READ;
444
445#if 0
446 /* remove si13, ... */
447 bts->si_valid &= ~(1 << SYSINFO_TYPE_13);
448 osmo_signal_dispatch(SS_GLOBAL, S_NEW_SYSINFO, bts);
449#endif
450
451 /* release PDCH */
452 for (i = 0; i < 8; i++) {
Harald Welte67798612016-11-17 18:10:10 +0100453 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200454 if (!trx)
455 break;
456 for (j = 0; j < 8; j++) {
457 ts = &trx->ts[j];
458 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
459 && ts->pchan == GSM_PCHAN_PDCH) {
460 printf("l1sap_chan_rel(trx,gsm_lchan2chan_nr(ts->lchan));\n");
461 }
462 }
463 }
464
465 /* flush the queue */
466 while (!llist_empty(&state->upqueue)) {
467 struct msgb *msg = msgb_dequeue(&state->upqueue);
468 msgb_free(msg);
469 }
470}
471
472static int pcu_sock_read(struct osmo_fd *bfd)
473{
474 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
475 struct gsm_pcu_if *pcu_prim;
476 struct msgb *msg;
477 int rc;
478
479 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
480 if (!msg)
481 return -ENOMEM;
482
483 pcu_prim = (struct gsm_pcu_if *) msg->tail;
484
485 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
486 if (rc == 0)
487 goto close;
488
489 if (rc < 0) {
490 if (errno == EAGAIN)
491 return 0;
492 goto close;
493 }
494
495 rc = pcu_rx(state->net, pcu_prim->msg_type, pcu_prim);
496
497 /* as we always synchronously process the message in pcu_rx() and
498 * its callbacks, we can free the message here. */
499 msgb_free(msg);
500
501 return rc;
502
503close:
504 msgb_free(msg);
505 pcu_sock_close(state);
506 return -1;
507}
508
509static int pcu_sock_write(struct osmo_fd *bfd)
510{
511 struct pcu_sock_state *state = bfd->data;
512 int rc;
513
514 while (!llist_empty(&state->upqueue)) {
515 struct msgb *msg, *msg2;
516 struct gsm_pcu_if *pcu_prim;
517
518 /* peek at the beginning of the queue */
519 msg = llist_entry(state->upqueue.next, struct msgb, list);
520 pcu_prim = (struct gsm_pcu_if *)msg->data;
521
522 bfd->when &= ~BSC_FD_WRITE;
523
524 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
525 if (!msgb_length(msg)) {
526 LOGP(DPCU, LOGL_ERROR, "message type (%d) with ZERO "
527 "bytes!\n", pcu_prim->msg_type);
528 goto dontsend;
529 }
530
531 /* try to send it over the socket */
532 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
533 if (rc == 0)
534 goto close;
535 if (rc < 0) {
536 if (errno == EAGAIN) {
537 bfd->when |= BSC_FD_WRITE;
538 break;
539 }
540 goto close;
541 }
542
543dontsend:
544 /* _after_ we send it, we can deueue */
545 msg2 = msgb_dequeue(&state->upqueue);
546 assert(msg == msg2);
547 msgb_free(msg);
548 }
549 return 0;
550
551close:
552 pcu_sock_close(state);
553
554 return -1;
555}
556
557static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
558{
559 int rc = 0;
560
561 if (flags & BSC_FD_READ)
562 rc = pcu_sock_read(bfd);
563 if (rc < 0)
564 return rc;
565
566 if (flags & BSC_FD_WRITE)
567 rc = pcu_sock_write(bfd);
568
569 return rc;
570}
571
572/* accept connection comming from PCU */
573static int pcu_sock_accept(struct osmo_fd *bfd, unsigned int flags)
574{
575 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
576 struct osmo_fd *conn_bfd = &state->conn_bfd;
577 struct sockaddr_un un_addr;
578 socklen_t len;
579 int rc;
580
581 len = sizeof(un_addr);
582 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
583 if (rc < 0) {
584 LOGP(DPCU, LOGL_ERROR, "Failed to accept a new connection\n");
585 return -1;
586 }
587
588 if (conn_bfd->fd >= 0) {
589 LOGP(DPCU, LOGL_NOTICE, "PCU connects but we already have "
590 "another active connection ?!?\n");
591 /* We already have one PCU connected, this is all we support */
592 state->listen_bfd.when &= ~BSC_FD_READ;
593 close(rc);
594 return 0;
595 }
596
597 conn_bfd->fd = rc;
598 conn_bfd->when = BSC_FD_READ;
599 conn_bfd->cb = pcu_sock_cb;
600 conn_bfd->data = state;
601
602 if (osmo_fd_register(conn_bfd) != 0) {
603 LOGP(DPCU, LOGL_ERROR, "Failed to register new connection "
604 "fd\n");
605 close(conn_bfd->fd);
606 conn_bfd->fd = -1;
607 return -1;
608 }
609
610 LOGP(DPCU, LOGL_NOTICE, "PCU socket connected to external PCU\n");
611
612 return 0;
613}
614
615/* Open connection to PCU */
616int pcu_sock_init(const char *path, struct gsm_bts *bts)
617{
618 struct pcu_sock_state *state;
619 struct osmo_fd *bfd;
620 int rc;
621
622 state = talloc_zero(NULL, struct pcu_sock_state);
623 if (!state)
624 return -ENOMEM;
625
626 INIT_LLIST_HEAD(&state->upqueue);
627 state->net = bts->network;
628 state->conn_bfd.fd = -1;
629
630 bfd = &state->listen_bfd;
631
632 bfd->fd = osmo_sock_unix_init(SOCK_SEQPACKET, 0, path,
633 OSMO_SOCK_F_BIND);
634 if (bfd->fd < 0) {
635 LOGP(DPCU, LOGL_ERROR, "Could not create unix socket: %s\n",
636 strerror(errno));
637 talloc_free(state);
638 return -1;
639 }
640
641 bfd->when = BSC_FD_READ;
642 bfd->cb = pcu_sock_accept;
643 bfd->data = state;
644
645 rc = osmo_fd_register(bfd);
646 if (rc < 0) {
647 LOGP(DPCU, LOGL_ERROR, "Could not register listen fd: %d\n",
648 rc);
649 close(bfd->fd);
650 talloc_free(state);
651 return rc;
652 }
653
654 bts->pcu_state = state;
655 return 0;
656}
657
658/* Close connection to PCU */
659void pcu_sock_exit(struct gsm_bts *bts)
660{
661 struct pcu_sock_state *state = bts->pcu_state;
662 struct osmo_fd *bfd, *conn_bfd;
663
664 if (!state)
665 return;
666
667 conn_bfd = &state->conn_bfd;
668 if (conn_bfd->fd > 0)
669 pcu_sock_close(state);
670 bfd = &state->listen_bfd;
671 close(bfd->fd);
672 osmo_fd_unregister(bfd);
673 talloc_free(state);
674 bts->pcu_state = NULL;
675}
676