blob: 7712878c62be0ee89f3381fe1ca6b4277b3a6f2d [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);
Alexander Couzensa2297562016-11-29 00:18:26 +010049int pcu_direct = 1;
Philipp Maierb4999b62016-10-26 15:19:41 +020050
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)
Alexander Couzens872671e2016-11-29 00:21:18 +0100225 continue;
Harald Welte54050a22016-11-21 01:33:22 +0100226 info_ind->trx[i].hlayer1 = 0x2342;
Philipp Maierb4999b62016-10-26 15:19:41 +0200227 info_ind->trx[i].pdch_mask = 0;
228 info_ind->trx[i].arfcn = trx->arfcn;
Harald Weltee586f412016-11-17 18:39:36 +0100229 for (j = 0; j < ARRAY_SIZE(trx->ts); j++) {
Philipp Maierb4999b62016-10-26 15:19:41 +0200230 ts = &trx->ts[j];
231 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
232 && ts_should_be_pdch(ts)) {
233 info_ind->trx[i].pdch_mask |= (1 << j);
234 info_ind->trx[i].tsc[j] =
235 (ts->tsc >= 0) ? ts->tsc : bts->bsic & 7;
236 LOGP(DPCU, LOGL_INFO, "trx=%d ts=%d: "
237 "available (tsc=%d arfcn=%d)\n",
238 trx->nr, ts->nr,
239 info_ind->trx[i].tsc[j],
240 info_ind->trx[i].arfcn);
241 }
242 }
243 }
244
245 return pcu_sock_send(bts, msg);
246}
247
248void pcu_info_update(struct gsm_bts *bts)
249{
250 if (pcu_connected(bts))
251 pcu_tx_info_ind(bts);
252}
253
254/* Forward rach indication to PCU */
255int pcu_tx_rach_ind(struct gsm_bts *bts, int16_t qta, uint16_t ra, uint32_t fn,
256 uint8_t is_11bit, enum ph_burst_type burst_type)
257{
258 struct msgb *msg;
259 struct gsm_pcu_if *pcu_prim;
260 struct gsm_pcu_if_rach_ind *rach_ind;
261
262 /* Bail if no PCU is connected */
263 if (!pcu_connected(bts)) {
264 LOGP(DRSL, LOGL_ERROR, "BTS %d CHAN RQD(GPRS) but PCU not "
265 "connected!\n", bts->nr);
266 return -ENODEV;
267 }
268
269 LOGP(DPCU, LOGL_INFO, "Sending RACH indication: qta=%d, ra=%d, "
270 "fn=%d\n", qta, ra, fn);
271
272 msg = pcu_msgb_alloc(PCU_IF_MSG_RACH_IND, bts->nr);
273 if (!msg)
274 return -ENOMEM;
275 pcu_prim = (struct gsm_pcu_if *) msg->data;
276 rach_ind = &pcu_prim->u.rach_ind;
277
278 rach_ind->sapi = PCU_IF_SAPI_RACH;
279 rach_ind->ra = ra;
280 rach_ind->qta = qta;
281 rach_ind->fn = fn;
282 rach_ind->is_11bit = is_11bit;
283 rach_ind->burst_type = burst_type;
284
285 return pcu_sock_send(bts, msg);
286}
287
Harald Welte854bcc22016-11-17 20:54:47 +0100288/* we need to decode the raw RR paging messsage (see PCU code
289 * Encoding::write_paging_request) and extract the mobile identity
290 * (P-TMSI) from it */
291static int pcu_rx_rr_paging(struct gsm_bts *bts, uint8_t paging_group,
292 const uint8_t *raw_rr_msg)
293{
294 struct gsm48_hdr *gsmh = (struct gsm48_hdr *) raw_rr_msg;
295 struct gsm48_paging1 *p1 = (struct gsm48_paging1 *) gsmh;
296 uint8_t chan_needed;
297 unsigned int mi_len;
298 uint8_t *mi;
299 int rc;
300
301 switch (gsmh->msg_type) {
302 case GSM48_MT_RR_PAG_REQ_1:
303 chan_needed = (p1->cneed2 << 2) | p1->cneed1;
304 mi_len = p1->data[0];
305 mi = p1->data+1;
306 /* FIXME: why does rsl_paging_cmd add 2 to mi? */
307 rc = rsl_paging_cmd(bts, paging_group, mi_len, mi,
308 chan_needed, true);
309 break;
310 case GSM48_MT_RR_PAG_REQ_2:
311 case GSM48_MT_RR_PAG_REQ_3:
312 LOGP(DPCU, LOGL_ERROR, "PCU Sends unsupported paging "
313 "request type\n");
314 rc = -EINVAL;
315 break;
316 }
317
318 return rc;
319}
320
Philipp Maierb4999b62016-10-26 15:19:41 +0200321static int pcu_rx_data_req(struct gsm_bts *bts, uint8_t msg_type,
322 struct gsm_pcu_if_data *data_req)
323{
324 uint8_t is_ptcch;
325 struct gsm_bts_trx *trx;
326 struct gsm_bts_trx_ts *ts;
327 struct msgb *msg;
Harald Welte854bcc22016-11-17 20:54:47 +0100328 char imsi_digit_buf[4];
329 uint8_t pag_grp;
Philipp Maierb4999b62016-10-26 15:19:41 +0200330 int rc = 0;
331
332 LOGP(DPCU, LOGL_DEBUG, "Data request received: sapi=%s arfcn=%d "
333 "block=%d data=%s\n", sapi_string[data_req->sapi],
334 data_req->arfcn, data_req->block_nr,
335 osmo_hexdump(data_req->data, data_req->len));
336
337 switch (data_req->sapi) {
338 case PCU_IF_SAPI_PCH:
Harald Welte854bcc22016-11-17 20:54:47 +0100339 /* the first three bytes are the last three digits of
340 * the IMSI, which we need to compute the paging group */
341 imsi_digit_buf[0] = data_req->data[0];
342 imsi_digit_buf[1] = data_req->data[1];
343 imsi_digit_buf[2] = data_req->data[2];
344 imsi_digit_buf[3] = '\0';
345 pag_grp = gsm0502_calc_paging_group(&bts->si_common.chan_desc,
346 str_to_imsi(imsi_digit_buf));
347 pcu_rx_rr_paging(bts, pag_grp, data_req->data+3);
Philipp Maierb4999b62016-10-26 15:19:41 +0200348 break;
349 case PCU_IF_SAPI_AGCH:
350 msg = msgb_alloc(data_req->len, "pcu_agch");
351 if (!msg) {
352 rc = -ENOMEM;
353 break;
354 }
355 msg->l3h = msgb_put(msg, data_req->len);
356 memcpy(msg->l3h, data_req->data, data_req->len);
357
358 if (rsl_imm_assign_cmd(bts, msg->len, msg->data)) {
359 msgb_free(msg);
360 rc = -EIO;
361 }
362 break;
363 default:
364 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
365 "unsupported sapi %d\n", data_req->sapi);
366 rc = -EINVAL;
367 }
368
369 return rc;
370}
371
372static int pcu_rx(struct gsm_network *net, uint8_t msg_type,
373 struct gsm_pcu_if *pcu_prim)
374{
375 int rc = 0;
376 struct gsm_bts *bts;
377
378 /* FIXME: allow multiple BTS */
379 bts = llist_entry(net->bts_list.next, struct gsm_bts, list);
380
381 switch (msg_type) {
382 case PCU_IF_MSG_DATA_REQ:
383 case PCU_IF_MSG_PAG_REQ:
384 rc = pcu_rx_data_req(bts, msg_type, &pcu_prim->u.data_req);
385 break;
386 default:
387 LOGP(DPCU, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
388 msg_type);
389 rc = -EINVAL;
390 }
391
392 return rc;
393}
394
395/*
396 * PCU socket interface
397 */
398
399static int pcu_sock_send(struct gsm_bts *bts, struct msgb *msg)
400{
401 struct pcu_sock_state *state = bts->pcu_state;
402 struct osmo_fd *conn_bfd;
403 struct gsm_pcu_if *pcu_prim = (struct gsm_pcu_if *) msg->data;
404
405 if (!state) {
406 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
407 LOGP(DPCU, LOGL_INFO, "PCU socket not created, "
408 "dropping message\n");
409 msgb_free(msg);
410 return -EINVAL;
411 }
412 conn_bfd = &state->conn_bfd;
413 if (conn_bfd->fd <= 0) {
414 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
415 LOGP(DPCU, LOGL_NOTICE, "PCU socket not connected, "
416 "dropping message\n");
417 msgb_free(msg);
418 return -EIO;
419 }
420 msgb_enqueue(&state->upqueue, msg);
421 conn_bfd->when |= BSC_FD_WRITE;
422
423 return 0;
424}
425
426static void pcu_sock_close(struct pcu_sock_state *state)
427{
428 struct osmo_fd *bfd = &state->conn_bfd;
429 struct gsm_bts *bts;
430 struct gsm_bts_trx *trx;
431 struct gsm_bts_trx_ts *ts;
432 int i, j;
433
434 /* FIXME: allow multiple BTS */
435 bts = llist_entry(state->net->bts_list.next, struct gsm_bts, list);
436
437 LOGP(DPCU, LOGL_NOTICE, "PCU socket has LOST connection\n");
438
439 close(bfd->fd);
440 bfd->fd = -1;
441 osmo_fd_unregister(bfd);
442
443 /* re-enable the generation of ACCEPT for new connections */
444 state->listen_bfd.when |= BSC_FD_READ;
445
446#if 0
447 /* remove si13, ... */
448 bts->si_valid &= ~(1 << SYSINFO_TYPE_13);
449 osmo_signal_dispatch(SS_GLOBAL, S_NEW_SYSINFO, bts);
450#endif
451
452 /* release PDCH */
453 for (i = 0; i < 8; i++) {
Harald Welte67798612016-11-17 18:10:10 +0100454 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200455 if (!trx)
456 break;
457 for (j = 0; j < 8; j++) {
458 ts = &trx->ts[j];
459 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
460 && ts->pchan == GSM_PCHAN_PDCH) {
461 printf("l1sap_chan_rel(trx,gsm_lchan2chan_nr(ts->lchan));\n");
462 }
463 }
464 }
465
466 /* flush the queue */
467 while (!llist_empty(&state->upqueue)) {
468 struct msgb *msg = msgb_dequeue(&state->upqueue);
469 msgb_free(msg);
470 }
471}
472
473static int pcu_sock_read(struct osmo_fd *bfd)
474{
475 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
476 struct gsm_pcu_if *pcu_prim;
477 struct msgb *msg;
478 int rc;
479
480 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
481 if (!msg)
482 return -ENOMEM;
483
484 pcu_prim = (struct gsm_pcu_if *) msg->tail;
485
486 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
487 if (rc == 0)
488 goto close;
489
490 if (rc < 0) {
491 if (errno == EAGAIN)
492 return 0;
493 goto close;
494 }
495
496 rc = pcu_rx(state->net, pcu_prim->msg_type, pcu_prim);
497
498 /* as we always synchronously process the message in pcu_rx() and
499 * its callbacks, we can free the message here. */
500 msgb_free(msg);
501
502 return rc;
503
504close:
505 msgb_free(msg);
506 pcu_sock_close(state);
507 return -1;
508}
509
510static int pcu_sock_write(struct osmo_fd *bfd)
511{
512 struct pcu_sock_state *state = bfd->data;
513 int rc;
514
515 while (!llist_empty(&state->upqueue)) {
516 struct msgb *msg, *msg2;
517 struct gsm_pcu_if *pcu_prim;
518
519 /* peek at the beginning of the queue */
520 msg = llist_entry(state->upqueue.next, struct msgb, list);
521 pcu_prim = (struct gsm_pcu_if *)msg->data;
522
523 bfd->when &= ~BSC_FD_WRITE;
524
525 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
526 if (!msgb_length(msg)) {
527 LOGP(DPCU, LOGL_ERROR, "message type (%d) with ZERO "
528 "bytes!\n", pcu_prim->msg_type);
529 goto dontsend;
530 }
531
532 /* try to send it over the socket */
533 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
534 if (rc == 0)
535 goto close;
536 if (rc < 0) {
537 if (errno == EAGAIN) {
538 bfd->when |= BSC_FD_WRITE;
539 break;
540 }
541 goto close;
542 }
543
544dontsend:
545 /* _after_ we send it, we can deueue */
546 msg2 = msgb_dequeue(&state->upqueue);
547 assert(msg == msg2);
548 msgb_free(msg);
549 }
550 return 0;
551
552close:
553 pcu_sock_close(state);
554
555 return -1;
556}
557
558static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
559{
560 int rc = 0;
561
562 if (flags & BSC_FD_READ)
563 rc = pcu_sock_read(bfd);
564 if (rc < 0)
565 return rc;
566
567 if (flags & BSC_FD_WRITE)
568 rc = pcu_sock_write(bfd);
569
570 return rc;
571}
572
573/* accept connection comming from PCU */
574static int pcu_sock_accept(struct osmo_fd *bfd, unsigned int flags)
575{
576 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
577 struct osmo_fd *conn_bfd = &state->conn_bfd;
578 struct sockaddr_un un_addr;
579 socklen_t len;
580 int rc;
581
582 len = sizeof(un_addr);
583 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
584 if (rc < 0) {
585 LOGP(DPCU, LOGL_ERROR, "Failed to accept a new connection\n");
586 return -1;
587 }
588
589 if (conn_bfd->fd >= 0) {
590 LOGP(DPCU, LOGL_NOTICE, "PCU connects but we already have "
591 "another active connection ?!?\n");
592 /* We already have one PCU connected, this is all we support */
593 state->listen_bfd.when &= ~BSC_FD_READ;
594 close(rc);
595 return 0;
596 }
597
598 conn_bfd->fd = rc;
599 conn_bfd->when = BSC_FD_READ;
600 conn_bfd->cb = pcu_sock_cb;
601 conn_bfd->data = state;
602
603 if (osmo_fd_register(conn_bfd) != 0) {
604 LOGP(DPCU, LOGL_ERROR, "Failed to register new connection "
605 "fd\n");
606 close(conn_bfd->fd);
607 conn_bfd->fd = -1;
608 return -1;
609 }
610
611 LOGP(DPCU, LOGL_NOTICE, "PCU socket connected to external PCU\n");
612
613 return 0;
614}
615
616/* Open connection to PCU */
617int pcu_sock_init(const char *path, struct gsm_bts *bts)
618{
619 struct pcu_sock_state *state;
620 struct osmo_fd *bfd;
621 int rc;
622
623 state = talloc_zero(NULL, struct pcu_sock_state);
624 if (!state)
625 return -ENOMEM;
626
627 INIT_LLIST_HEAD(&state->upqueue);
628 state->net = bts->network;
629 state->conn_bfd.fd = -1;
630
631 bfd = &state->listen_bfd;
632
633 bfd->fd = osmo_sock_unix_init(SOCK_SEQPACKET, 0, path,
634 OSMO_SOCK_F_BIND);
635 if (bfd->fd < 0) {
636 LOGP(DPCU, LOGL_ERROR, "Could not create unix socket: %s\n",
637 strerror(errno));
638 talloc_free(state);
639 return -1;
640 }
641
642 bfd->when = BSC_FD_READ;
643 bfd->cb = pcu_sock_accept;
644 bfd->data = state;
645
646 rc = osmo_fd_register(bfd);
647 if (rc < 0) {
648 LOGP(DPCU, LOGL_ERROR, "Could not register listen fd: %d\n",
649 rc);
650 close(bfd->fd);
651 talloc_free(state);
652 return rc;
653 }
654
655 bts->pcu_state = state;
656 return 0;
657}
658
659/* Close connection to PCU */
660void pcu_sock_exit(struct gsm_bts *bts)
661{
662 struct pcu_sock_state *state = bts->pcu_state;
663 struct osmo_fd *bfd, *conn_bfd;
664
665 if (!state)
666 return;
667
668 conn_bfd = &state->conn_bfd;
669 if (conn_bfd->fd > 0)
670 pcu_sock_close(state);
671 bfd = &state->listen_bfd;
672 close(bfd->fd);
673 osmo_fd_unregister(bfd);
674 talloc_free(state);
675 bts->pcu_state = NULL;
676}
677