blob: 0ae3a03e41f03778fe4658f0c421ede6f51cec89 [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",
Alexander Couzensf14cb352016-12-02 18:27:01 +010059 [PCU_IF_SAPI_AGCH_DT] = "AGCH_DT",
Philipp Maierb4999b62016-10-26 15:19:41 +020060};
61
Philipp Maierb4999b62016-10-26 15:19:41 +020062/* Check if BTS has a PCU connection */
63static bool pcu_connected(struct gsm_bts *bts)
64{
65 struct pcu_sock_state *state = bts->pcu_state;
66
67 if (!state)
68 return false;
69 if (state->conn_bfd.fd <= 0)
70 return false;
71 return true;
72}
73
74/*
75 * PCU messages
76 */
77
78/* Set up an message buffer to package an pcu interface message */
79struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr)
80{
81 struct msgb *msg;
82 struct gsm_pcu_if *pcu_prim;
83
84 msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx");
85 if (!msg)
86 return NULL;
87
88 msgb_put(msg, sizeof(struct gsm_pcu_if));
89 pcu_prim = (struct gsm_pcu_if *) msg->data;
90 pcu_prim->msg_type = msg_type;
91 pcu_prim->bts_nr = bts_nr;
92
93 return msg;
94}
95
96/* Helper function exclusivly used by pcu_if_signal_cb() */
97static bool ts_should_be_pdch(struct gsm_bts_trx_ts *ts) {
98 if (ts->pchan == GSM_PCHAN_PDCH)
99 return true;
100 if (ts->pchan == GSM_PCHAN_TCH_F_PDCH) {
101 /* When we're busy deactivating the PDCH, we first set
102 * DEACT_PENDING, tell the PCU about it and wait for a
103 * response. So DEACT_PENDING means "no PDCH" to the PCU.
104 * Similarly, when we're activating PDCH, we set the
105 * ACT_PENDING and wait for an activation response from the
106 * PCU, so ACT_PENDING means "is PDCH". */
107 if (ts->flags & TS_F_PDCH_ACTIVE)
108 return !(ts->flags & TS_F_PDCH_DEACT_PENDING);
109 else
110 return (ts->flags & TS_F_PDCH_ACT_PENDING);
111 }
112 if (ts->pchan == GSM_PCHAN_TCH_F_TCH_H_PDCH) {
113 /*
114 * When we're busy de-/activating the PDCH, we first set
115 * ts->dyn.pchan_want, tell the PCU about it and wait for a
116 * response. So only care about dyn.pchan_want here.
117 */
118 return ts->dyn.pchan_want == GSM_PCHAN_PDCH;
119 }
120 return false;
121}
122
123/* Send BTS properties to the PCU */
124static int pcu_tx_info_ind(struct gsm_bts *bts)
125{
126 struct msgb *msg;
127 struct gsm_pcu_if *pcu_prim;
128 struct gsm_pcu_if_info_ind *info_ind;
129 struct gprs_rlc_cfg *rlcc;
130 struct gsm_bts_gprs_nsvc *nsvc;
131 struct gsm_bts_trx *trx;
132 struct gsm_bts_trx_ts *ts;
133 int i, j;
134
135 OSMO_ASSERT(bts);
136 OSMO_ASSERT(bts->network);
137
138 LOGP(DPCU, LOGL_INFO, "Sending info for BTS %d\n",bts->nr);
139
140 rlcc = &bts->gprs.cell.rlc_cfg;
141
142 msg = pcu_msgb_alloc(PCU_IF_MSG_INFO_IND, bts->nr);
143 if (!msg)
144 return -ENOMEM;
145
146 pcu_prim = (struct gsm_pcu_if *) msg->data;
147 info_ind = &pcu_prim->u.info_ind;
148 info_ind->version = PCU_IF_VERSION;
149 info_ind->flags |= PCU_IF_FLAG_ACTIVE;
150
151 if (pcu_direct)
152 info_ind->flags |= PCU_IF_FLAG_SYSMO;
153
154 /* RAI */
155 info_ind->mcc = bts->network->country_code;
156 info_ind->mnc = bts->network->network_code;
157 info_ind->lac = bts->location_area_code;
158 info_ind->rac = bts->gprs.rac;
159
160 /* NSE */
161 info_ind->nsei = bts->gprs.nse.nsei;
162 memcpy(info_ind->nse_timer, bts->gprs.nse.timer, 7);
163 memcpy(info_ind->cell_timer, bts->gprs.cell.timer, 11);
164
165 /* cell attributes */
166 info_ind->cell_id = bts->cell_identity;
167 info_ind->repeat_time = rlcc->paging.repeat_time;
168 info_ind->repeat_count = rlcc->paging.repeat_count;
169 info_ind->bvci = bts->gprs.cell.bvci;
170 info_ind->t3142 = rlcc->parameter[RLC_T3142];
171 info_ind->t3169 = rlcc->parameter[RLC_T3169];
172 info_ind->t3191 = rlcc->parameter[RLC_T3191];
173 info_ind->t3193_10ms = rlcc->parameter[RLC_T3193];
174 info_ind->t3195 = rlcc->parameter[RLC_T3195];
175 info_ind->n3101 = rlcc->parameter[RLC_N3101];
176 info_ind->n3103 = rlcc->parameter[RLC_N3103];
177 info_ind->n3105 = rlcc->parameter[RLC_N3105];
178 info_ind->cv_countdown = rlcc->parameter[CV_COUNTDOWN];
179 if (rlcc->cs_mask & (1 << GPRS_CS1))
180 info_ind->flags |= PCU_IF_FLAG_CS1;
181 if (rlcc->cs_mask & (1 << GPRS_CS2))
182 info_ind->flags |= PCU_IF_FLAG_CS2;
183 if (rlcc->cs_mask & (1 << GPRS_CS3))
184 info_ind->flags |= PCU_IF_FLAG_CS3;
185 if (rlcc->cs_mask & (1 << GPRS_CS4))
186 info_ind->flags |= PCU_IF_FLAG_CS4;
187 if (bts->gprs.mode == BTS_GPRS_EGPRS) {
188 if (rlcc->cs_mask & (1 << GPRS_MCS1))
189 info_ind->flags |= PCU_IF_FLAG_MCS1;
190 if (rlcc->cs_mask & (1 << GPRS_MCS2))
191 info_ind->flags |= PCU_IF_FLAG_MCS2;
192 if (rlcc->cs_mask & (1 << GPRS_MCS3))
193 info_ind->flags |= PCU_IF_FLAG_MCS3;
194 if (rlcc->cs_mask & (1 << GPRS_MCS4))
195 info_ind->flags |= PCU_IF_FLAG_MCS4;
196 if (rlcc->cs_mask & (1 << GPRS_MCS5))
197 info_ind->flags |= PCU_IF_FLAG_MCS5;
198 if (rlcc->cs_mask & (1 << GPRS_MCS6))
199 info_ind->flags |= PCU_IF_FLAG_MCS6;
200 if (rlcc->cs_mask & (1 << GPRS_MCS7))
201 info_ind->flags |= PCU_IF_FLAG_MCS7;
202 if (rlcc->cs_mask & (1 << GPRS_MCS8))
203 info_ind->flags |= PCU_IF_FLAG_MCS8;
204 if (rlcc->cs_mask & (1 << GPRS_MCS9))
205 info_ind->flags |= PCU_IF_FLAG_MCS9;
206 }
207#warning "isn't dl_tbf_ext wrong?: * 10 and no ntohs"
208 info_ind->dl_tbf_ext = rlcc->parameter[T_DL_TBF_EXT];
209#warning "isn't ul_tbf_ext wrong?: * 10 and no ntohs"
210 info_ind->ul_tbf_ext = rlcc->parameter[T_UL_TBF_EXT];
211 info_ind->initial_cs = rlcc->initial_cs;
212 info_ind->initial_mcs = rlcc->initial_mcs;
213
214 /* NSVC */
Harald Weltee586f412016-11-17 18:39:36 +0100215 for (i = 0; i < ARRAY_SIZE(info_ind->nsvci); i++) {
Philipp Maierb4999b62016-10-26 15:19:41 +0200216 nsvc = &bts->gprs.nsvc[i];
217 info_ind->nsvci[i] = nsvc->nsvci;
218 info_ind->local_port[i] = nsvc->local_port;
219 info_ind->remote_port[i] = nsvc->remote_port;
220 info_ind->remote_ip[i] = nsvc->remote_ip;
221 }
222
Harald Weltee586f412016-11-17 18:39:36 +0100223 for (i = 0; i < ARRAY_SIZE(info_ind->trx); i++) {
Harald Welte67798612016-11-17 18:10:10 +0100224 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200225 if (!trx)
Alexander Couzens872671e2016-11-29 00:21:18 +0100226 continue;
Harald Welte54050a22016-11-21 01:33:22 +0100227 info_ind->trx[i].hlayer1 = 0x2342;
Philipp Maierb4999b62016-10-26 15:19:41 +0200228 info_ind->trx[i].pdch_mask = 0;
229 info_ind->trx[i].arfcn = trx->arfcn;
Harald Weltee586f412016-11-17 18:39:36 +0100230 for (j = 0; j < ARRAY_SIZE(trx->ts); j++) {
Philipp Maierb4999b62016-10-26 15:19:41 +0200231 ts = &trx->ts[j];
232 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
233 && ts_should_be_pdch(ts)) {
234 info_ind->trx[i].pdch_mask |= (1 << j);
235 info_ind->trx[i].tsc[j] =
236 (ts->tsc >= 0) ? ts->tsc : bts->bsic & 7;
237 LOGP(DPCU, LOGL_INFO, "trx=%d ts=%d: "
238 "available (tsc=%d arfcn=%d)\n",
239 trx->nr, ts->nr,
240 info_ind->trx[i].tsc[j],
241 info_ind->trx[i].arfcn);
242 }
243 }
244 }
245
246 return pcu_sock_send(bts, msg);
247}
248
249void pcu_info_update(struct gsm_bts *bts)
250{
251 if (pcu_connected(bts))
252 pcu_tx_info_ind(bts);
253}
254
255/* Forward rach indication to PCU */
256int pcu_tx_rach_ind(struct gsm_bts *bts, int16_t qta, uint16_t ra, uint32_t fn,
257 uint8_t is_11bit, enum ph_burst_type burst_type)
258{
259 struct msgb *msg;
260 struct gsm_pcu_if *pcu_prim;
261 struct gsm_pcu_if_rach_ind *rach_ind;
262
263 /* Bail if no PCU is connected */
264 if (!pcu_connected(bts)) {
265 LOGP(DRSL, LOGL_ERROR, "BTS %d CHAN RQD(GPRS) but PCU not "
266 "connected!\n", bts->nr);
267 return -ENODEV;
268 }
269
270 LOGP(DPCU, LOGL_INFO, "Sending RACH indication: qta=%d, ra=%d, "
271 "fn=%d\n", qta, ra, fn);
272
273 msg = pcu_msgb_alloc(PCU_IF_MSG_RACH_IND, bts->nr);
274 if (!msg)
275 return -ENOMEM;
276 pcu_prim = (struct gsm_pcu_if *) msg->data;
277 rach_ind = &pcu_prim->u.rach_ind;
278
279 rach_ind->sapi = PCU_IF_SAPI_RACH;
280 rach_ind->ra = ra;
281 rach_ind->qta = qta;
282 rach_ind->fn = fn;
283 rach_ind->is_11bit = is_11bit;
284 rach_ind->burst_type = burst_type;
285
286 return pcu_sock_send(bts, msg);
287}
288
Harald Welte854bcc22016-11-17 20:54:47 +0100289/* we need to decode the raw RR paging messsage (see PCU code
290 * Encoding::write_paging_request) and extract the mobile identity
291 * (P-TMSI) from it */
292static int pcu_rx_rr_paging(struct gsm_bts *bts, uint8_t paging_group,
293 const uint8_t *raw_rr_msg)
294{
295 struct gsm48_hdr *gsmh = (struct gsm48_hdr *) raw_rr_msg;
296 struct gsm48_paging1 *p1 = (struct gsm48_paging1 *) gsmh;
297 uint8_t chan_needed;
298 unsigned int mi_len;
299 uint8_t *mi;
300 int rc;
301
302 switch (gsmh->msg_type) {
303 case GSM48_MT_RR_PAG_REQ_1:
304 chan_needed = (p1->cneed2 << 2) | p1->cneed1;
305 mi_len = p1->data[0];
306 mi = p1->data+1;
307 /* FIXME: why does rsl_paging_cmd add 2 to mi? */
308 rc = rsl_paging_cmd(bts, paging_group, mi_len, mi,
309 chan_needed, true);
310 break;
311 case GSM48_MT_RR_PAG_REQ_2:
312 case GSM48_MT_RR_PAG_REQ_3:
313 LOGP(DPCU, LOGL_ERROR, "PCU Sends unsupported paging "
314 "request type\n");
315 rc = -EINVAL;
316 break;
317 }
318
319 return rc;
320}
321
Philipp Maierb4999b62016-10-26 15:19:41 +0200322static int pcu_rx_data_req(struct gsm_bts *bts, uint8_t msg_type,
323 struct gsm_pcu_if_data *data_req)
324{
325 uint8_t is_ptcch;
326 struct gsm_bts_trx *trx;
327 struct gsm_bts_trx_ts *ts;
328 struct msgb *msg;
Harald Welte854bcc22016-11-17 20:54:47 +0100329 char imsi_digit_buf[4];
Alexander Couzensf14cb352016-12-02 18:27:01 +0100330 uint32_t tlli = -1;
Harald Welte854bcc22016-11-17 20:54:47 +0100331 uint8_t pag_grp;
Philipp Maierb4999b62016-10-26 15:19:41 +0200332 int rc = 0;
333
334 LOGP(DPCU, LOGL_DEBUG, "Data request received: sapi=%s arfcn=%d "
335 "block=%d data=%s\n", sapi_string[data_req->sapi],
336 data_req->arfcn, data_req->block_nr,
337 osmo_hexdump(data_req->data, data_req->len));
338
339 switch (data_req->sapi) {
340 case PCU_IF_SAPI_PCH:
Harald Welte854bcc22016-11-17 20:54:47 +0100341 /* the first three bytes are the last three digits of
342 * the IMSI, which we need to compute the paging group */
343 imsi_digit_buf[0] = data_req->data[0];
344 imsi_digit_buf[1] = data_req->data[1];
345 imsi_digit_buf[2] = data_req->data[2];
346 imsi_digit_buf[3] = '\0';
Alexander Couzensf14cb352016-12-02 18:27:01 +0100347 LOGP(DPCU, LOGL_DEBUG, "SAPI PCH imsi %s", imsi_digit_buf);
Harald Welte854bcc22016-11-17 20:54:47 +0100348 pag_grp = gsm0502_calc_paging_group(&bts->si_common.chan_desc,
349 str_to_imsi(imsi_digit_buf));
350 pcu_rx_rr_paging(bts, pag_grp, data_req->data+3);
Philipp Maierb4999b62016-10-26 15:19:41 +0200351 break;
352 case PCU_IF_SAPI_AGCH:
353 msg = msgb_alloc(data_req->len, "pcu_agch");
354 if (!msg) {
355 rc = -ENOMEM;
356 break;
357 }
358 msg->l3h = msgb_put(msg, data_req->len);
359 memcpy(msg->l3h, data_req->data, data_req->len);
360
361 if (rsl_imm_assign_cmd(bts, msg->len, msg->data)) {
362 msgb_free(msg);
363 rc = -EIO;
364 }
365 break;
Alexander Couzensf14cb352016-12-02 18:27:01 +0100366 case PCU_IF_SAPI_AGCH_DT:
367 /* DT = direct tlli. A tlli is prefixed */
368
369 if (data_req->len < 5) {
370 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
371 "invalid/small length %d\n", data_req->len);
372 break;
373 }
374 tlli = *((uint32_t *)data_req->data);
375
376 msg = msgb_alloc(data_req->len - 4, "pcu_agch");
377 if (!msg) {
378 rc = -ENOMEM;
379 break;
380 }
381 msg->l3h = msgb_put(msg, data_req->len - 4);
382 memcpy(msg->l3h, data_req->data + 4, data_req->len - 4);
383
384 if (bts->type == GSM_BTS_TYPE_RBS2000)
385 rc = rsl_ericsson_imm_assign_cmd(bts, tlli, msg->len, msg->data);
386 else
387 rc = rsl_imm_assign_cmd(bts, msg->len, msg->data);
388
389 if (rc) {
390 msgb_free(msg);
391 rc = -EIO;
392 }
393 break;
Philipp Maierb4999b62016-10-26 15:19:41 +0200394 default:
395 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
396 "unsupported sapi %d\n", data_req->sapi);
397 rc = -EINVAL;
398 }
399
400 return rc;
401}
402
403static int pcu_rx(struct gsm_network *net, uint8_t msg_type,
404 struct gsm_pcu_if *pcu_prim)
405{
406 int rc = 0;
407 struct gsm_bts *bts;
408
409 /* FIXME: allow multiple BTS */
410 bts = llist_entry(net->bts_list.next, struct gsm_bts, list);
411
412 switch (msg_type) {
413 case PCU_IF_MSG_DATA_REQ:
414 case PCU_IF_MSG_PAG_REQ:
415 rc = pcu_rx_data_req(bts, msg_type, &pcu_prim->u.data_req);
416 break;
417 default:
418 LOGP(DPCU, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
419 msg_type);
420 rc = -EINVAL;
421 }
422
423 return rc;
424}
425
426/*
427 * PCU socket interface
428 */
429
430static int pcu_sock_send(struct gsm_bts *bts, struct msgb *msg)
431{
432 struct pcu_sock_state *state = bts->pcu_state;
433 struct osmo_fd *conn_bfd;
434 struct gsm_pcu_if *pcu_prim = (struct gsm_pcu_if *) msg->data;
435
436 if (!state) {
437 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
438 LOGP(DPCU, LOGL_INFO, "PCU socket not created, "
439 "dropping message\n");
440 msgb_free(msg);
441 return -EINVAL;
442 }
443 conn_bfd = &state->conn_bfd;
444 if (conn_bfd->fd <= 0) {
445 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
446 LOGP(DPCU, LOGL_NOTICE, "PCU socket not connected, "
447 "dropping message\n");
448 msgb_free(msg);
449 return -EIO;
450 }
451 msgb_enqueue(&state->upqueue, msg);
452 conn_bfd->when |= BSC_FD_WRITE;
453
454 return 0;
455}
456
457static void pcu_sock_close(struct pcu_sock_state *state)
458{
459 struct osmo_fd *bfd = &state->conn_bfd;
460 struct gsm_bts *bts;
461 struct gsm_bts_trx *trx;
462 struct gsm_bts_trx_ts *ts;
463 int i, j;
464
465 /* FIXME: allow multiple BTS */
466 bts = llist_entry(state->net->bts_list.next, struct gsm_bts, list);
467
468 LOGP(DPCU, LOGL_NOTICE, "PCU socket has LOST connection\n");
469
470 close(bfd->fd);
471 bfd->fd = -1;
472 osmo_fd_unregister(bfd);
473
474 /* re-enable the generation of ACCEPT for new connections */
475 state->listen_bfd.when |= BSC_FD_READ;
476
477#if 0
478 /* remove si13, ... */
479 bts->si_valid &= ~(1 << SYSINFO_TYPE_13);
480 osmo_signal_dispatch(SS_GLOBAL, S_NEW_SYSINFO, bts);
481#endif
482
483 /* release PDCH */
484 for (i = 0; i < 8; i++) {
Harald Welte67798612016-11-17 18:10:10 +0100485 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200486 if (!trx)
487 break;
488 for (j = 0; j < 8; j++) {
489 ts = &trx->ts[j];
490 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
491 && ts->pchan == GSM_PCHAN_PDCH) {
492 printf("l1sap_chan_rel(trx,gsm_lchan2chan_nr(ts->lchan));\n");
493 }
494 }
495 }
496
497 /* flush the queue */
498 while (!llist_empty(&state->upqueue)) {
499 struct msgb *msg = msgb_dequeue(&state->upqueue);
500 msgb_free(msg);
501 }
502}
503
504static int pcu_sock_read(struct osmo_fd *bfd)
505{
506 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
507 struct gsm_pcu_if *pcu_prim;
508 struct msgb *msg;
509 int rc;
510
511 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
512 if (!msg)
513 return -ENOMEM;
514
515 pcu_prim = (struct gsm_pcu_if *) msg->tail;
516
517 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
518 if (rc == 0)
519 goto close;
520
521 if (rc < 0) {
522 if (errno == EAGAIN)
523 return 0;
524 goto close;
525 }
526
527 rc = pcu_rx(state->net, pcu_prim->msg_type, pcu_prim);
528
529 /* as we always synchronously process the message in pcu_rx() and
530 * its callbacks, we can free the message here. */
531 msgb_free(msg);
532
533 return rc;
534
535close:
536 msgb_free(msg);
537 pcu_sock_close(state);
538 return -1;
539}
540
541static int pcu_sock_write(struct osmo_fd *bfd)
542{
543 struct pcu_sock_state *state = bfd->data;
544 int rc;
545
546 while (!llist_empty(&state->upqueue)) {
547 struct msgb *msg, *msg2;
548 struct gsm_pcu_if *pcu_prim;
549
550 /* peek at the beginning of the queue */
551 msg = llist_entry(state->upqueue.next, struct msgb, list);
552 pcu_prim = (struct gsm_pcu_if *)msg->data;
553
554 bfd->when &= ~BSC_FD_WRITE;
555
556 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
557 if (!msgb_length(msg)) {
558 LOGP(DPCU, LOGL_ERROR, "message type (%d) with ZERO "
559 "bytes!\n", pcu_prim->msg_type);
560 goto dontsend;
561 }
562
563 /* try to send it over the socket */
564 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
565 if (rc == 0)
566 goto close;
567 if (rc < 0) {
568 if (errno == EAGAIN) {
569 bfd->when |= BSC_FD_WRITE;
570 break;
571 }
572 goto close;
573 }
574
575dontsend:
576 /* _after_ we send it, we can deueue */
577 msg2 = msgb_dequeue(&state->upqueue);
578 assert(msg == msg2);
579 msgb_free(msg);
580 }
581 return 0;
582
583close:
584 pcu_sock_close(state);
585
586 return -1;
587}
588
589static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
590{
591 int rc = 0;
592
593 if (flags & BSC_FD_READ)
594 rc = pcu_sock_read(bfd);
595 if (rc < 0)
596 return rc;
597
598 if (flags & BSC_FD_WRITE)
599 rc = pcu_sock_write(bfd);
600
601 return rc;
602}
603
604/* accept connection comming from PCU */
605static int pcu_sock_accept(struct osmo_fd *bfd, unsigned int flags)
606{
607 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
608 struct osmo_fd *conn_bfd = &state->conn_bfd;
609 struct sockaddr_un un_addr;
610 socklen_t len;
611 int rc;
612
613 len = sizeof(un_addr);
614 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
615 if (rc < 0) {
616 LOGP(DPCU, LOGL_ERROR, "Failed to accept a new connection\n");
617 return -1;
618 }
619
620 if (conn_bfd->fd >= 0) {
621 LOGP(DPCU, LOGL_NOTICE, "PCU connects but we already have "
622 "another active connection ?!?\n");
623 /* We already have one PCU connected, this is all we support */
624 state->listen_bfd.when &= ~BSC_FD_READ;
625 close(rc);
626 return 0;
627 }
628
629 conn_bfd->fd = rc;
630 conn_bfd->when = BSC_FD_READ;
631 conn_bfd->cb = pcu_sock_cb;
632 conn_bfd->data = state;
633
634 if (osmo_fd_register(conn_bfd) != 0) {
635 LOGP(DPCU, LOGL_ERROR, "Failed to register new connection "
636 "fd\n");
637 close(conn_bfd->fd);
638 conn_bfd->fd = -1;
639 return -1;
640 }
641
642 LOGP(DPCU, LOGL_NOTICE, "PCU socket connected to external PCU\n");
643
644 return 0;
645}
646
647/* Open connection to PCU */
648int pcu_sock_init(const char *path, struct gsm_bts *bts)
649{
650 struct pcu_sock_state *state;
651 struct osmo_fd *bfd;
652 int rc;
653
654 state = talloc_zero(NULL, struct pcu_sock_state);
655 if (!state)
656 return -ENOMEM;
657
658 INIT_LLIST_HEAD(&state->upqueue);
659 state->net = bts->network;
660 state->conn_bfd.fd = -1;
661
662 bfd = &state->listen_bfd;
663
664 bfd->fd = osmo_sock_unix_init(SOCK_SEQPACKET, 0, path,
665 OSMO_SOCK_F_BIND);
666 if (bfd->fd < 0) {
667 LOGP(DPCU, LOGL_ERROR, "Could not create unix socket: %s\n",
668 strerror(errno));
669 talloc_free(state);
670 return -1;
671 }
672
673 bfd->when = BSC_FD_READ;
674 bfd->cb = pcu_sock_accept;
675 bfd->data = state;
676
677 rc = osmo_fd_register(bfd);
678 if (rc < 0) {
679 LOGP(DPCU, LOGL_ERROR, "Could not register listen fd: %d\n",
680 rc);
681 close(bfd->fd);
682 talloc_free(state);
683 return rc;
684 }
685
686 bts->pcu_state = state;
687 return 0;
688}
689
690/* Close connection to PCU */
691void pcu_sock_exit(struct gsm_bts *bts)
692{
693 struct pcu_sock_state *state = bts->pcu_state;
694 struct osmo_fd *bfd, *conn_bfd;
695
696 if (!state)
697 return;
698
699 conn_bfd = &state->conn_bfd;
700 if (conn_bfd->fd > 0)
701 pcu_sock_close(state);
702 bfd = &state->listen_bfd;
703 close(bfd->fd);
704 osmo_fd_unregister(bfd);
705 talloc_free(state);
706 bts->pcu_state = NULL;
707}
708