blob: 7e77797964f17c68f6a0c3577ae9fb1acf5efda3 [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
Philipp Maierf8aeb2c2016-12-02 19:04:34 +0100289/* Confirm the sending of an immediate assignment to the pcu */
290int pcu_tx_imm_ass_sent(struct gsm_bts *bts, uint32_t tlli)
291{
292 struct msgb *msg;
293 struct gsm_pcu_if *pcu_prim;
294 struct gsm_pcu_if_data_cnf_dt *data_cnf_dt;
295
296 LOGP(DPCU, LOGL_INFO, "Sending PCH confirm with direct TLLI\n");
297
298 msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_CNF_DT, bts->nr);
299 if (!msg)
300 return -ENOMEM;
301 pcu_prim = (struct gsm_pcu_if *) msg->data;
302 data_cnf_dt = &pcu_prim->u.data_cnf_dt;
303
304 data_cnf_dt->sapi = PCU_IF_SAPI_PCH;
305 data_cnf_dt->tlli = tlli;
306
307 return pcu_sock_send(bts, msg);
308}
309
Harald Welte854bcc22016-11-17 20:54:47 +0100310/* we need to decode the raw RR paging messsage (see PCU code
311 * Encoding::write_paging_request) and extract the mobile identity
312 * (P-TMSI) from it */
313static int pcu_rx_rr_paging(struct gsm_bts *bts, uint8_t paging_group,
314 const uint8_t *raw_rr_msg)
315{
316 struct gsm48_hdr *gsmh = (struct gsm48_hdr *) raw_rr_msg;
317 struct gsm48_paging1 *p1 = (struct gsm48_paging1 *) gsmh;
318 uint8_t chan_needed;
319 unsigned int mi_len;
320 uint8_t *mi;
321 int rc;
322
323 switch (gsmh->msg_type) {
324 case GSM48_MT_RR_PAG_REQ_1:
325 chan_needed = (p1->cneed2 << 2) | p1->cneed1;
326 mi_len = p1->data[0];
327 mi = p1->data+1;
328 /* FIXME: why does rsl_paging_cmd add 2 to mi? */
329 rc = rsl_paging_cmd(bts, paging_group, mi_len, mi,
330 chan_needed, true);
331 break;
332 case GSM48_MT_RR_PAG_REQ_2:
333 case GSM48_MT_RR_PAG_REQ_3:
334 LOGP(DPCU, LOGL_ERROR, "PCU Sends unsupported paging "
335 "request type\n");
336 rc = -EINVAL;
337 break;
338 }
339
340 return rc;
341}
342
Philipp Maierb4999b62016-10-26 15:19:41 +0200343static int pcu_rx_data_req(struct gsm_bts *bts, uint8_t msg_type,
344 struct gsm_pcu_if_data *data_req)
345{
346 uint8_t is_ptcch;
347 struct gsm_bts_trx *trx;
348 struct gsm_bts_trx_ts *ts;
349 struct msgb *msg;
Harald Welte854bcc22016-11-17 20:54:47 +0100350 char imsi_digit_buf[4];
Alexander Couzensf14cb352016-12-02 18:27:01 +0100351 uint32_t tlli = -1;
Harald Welte854bcc22016-11-17 20:54:47 +0100352 uint8_t pag_grp;
Philipp Maierb4999b62016-10-26 15:19:41 +0200353 int rc = 0;
354
355 LOGP(DPCU, LOGL_DEBUG, "Data request received: sapi=%s arfcn=%d "
356 "block=%d data=%s\n", sapi_string[data_req->sapi],
357 data_req->arfcn, data_req->block_nr,
358 osmo_hexdump(data_req->data, data_req->len));
359
360 switch (data_req->sapi) {
361 case PCU_IF_SAPI_PCH:
Harald Welte854bcc22016-11-17 20:54:47 +0100362 /* the first three bytes are the last three digits of
363 * the IMSI, which we need to compute the paging group */
364 imsi_digit_buf[0] = data_req->data[0];
365 imsi_digit_buf[1] = data_req->data[1];
366 imsi_digit_buf[2] = data_req->data[2];
367 imsi_digit_buf[3] = '\0';
Alexander Couzensf14cb352016-12-02 18:27:01 +0100368 LOGP(DPCU, LOGL_DEBUG, "SAPI PCH imsi %s", imsi_digit_buf);
Harald Welte854bcc22016-11-17 20:54:47 +0100369 pag_grp = gsm0502_calc_paging_group(&bts->si_common.chan_desc,
370 str_to_imsi(imsi_digit_buf));
371 pcu_rx_rr_paging(bts, pag_grp, data_req->data+3);
Philipp Maierb4999b62016-10-26 15:19:41 +0200372 break;
373 case PCU_IF_SAPI_AGCH:
374 msg = msgb_alloc(data_req->len, "pcu_agch");
375 if (!msg) {
376 rc = -ENOMEM;
377 break;
378 }
379 msg->l3h = msgb_put(msg, data_req->len);
380 memcpy(msg->l3h, data_req->data, data_req->len);
381
382 if (rsl_imm_assign_cmd(bts, msg->len, msg->data)) {
383 msgb_free(msg);
384 rc = -EIO;
385 }
386 break;
Alexander Couzensf14cb352016-12-02 18:27:01 +0100387 case PCU_IF_SAPI_AGCH_DT:
388 /* DT = direct tlli. A tlli is prefixed */
389
390 if (data_req->len < 5) {
391 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
392 "invalid/small length %d\n", data_req->len);
393 break;
394 }
395 tlli = *((uint32_t *)data_req->data);
396
397 msg = msgb_alloc(data_req->len - 4, "pcu_agch");
398 if (!msg) {
399 rc = -ENOMEM;
400 break;
401 }
402 msg->l3h = msgb_put(msg, data_req->len - 4);
403 memcpy(msg->l3h, data_req->data + 4, data_req->len - 4);
404
405 if (bts->type == GSM_BTS_TYPE_RBS2000)
406 rc = rsl_ericsson_imm_assign_cmd(bts, tlli, msg->len, msg->data);
407 else
408 rc = rsl_imm_assign_cmd(bts, msg->len, msg->data);
409
410 if (rc) {
411 msgb_free(msg);
412 rc = -EIO;
413 }
414 break;
Philipp Maierb4999b62016-10-26 15:19:41 +0200415 default:
416 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
417 "unsupported sapi %d\n", data_req->sapi);
418 rc = -EINVAL;
419 }
420
421 return rc;
422}
423
424static int pcu_rx(struct gsm_network *net, uint8_t msg_type,
425 struct gsm_pcu_if *pcu_prim)
426{
427 int rc = 0;
428 struct gsm_bts *bts;
429
430 /* FIXME: allow multiple BTS */
431 bts = llist_entry(net->bts_list.next, struct gsm_bts, list);
432
433 switch (msg_type) {
434 case PCU_IF_MSG_DATA_REQ:
435 case PCU_IF_MSG_PAG_REQ:
436 rc = pcu_rx_data_req(bts, msg_type, &pcu_prim->u.data_req);
437 break;
438 default:
439 LOGP(DPCU, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
440 msg_type);
441 rc = -EINVAL;
442 }
443
444 return rc;
445}
446
447/*
448 * PCU socket interface
449 */
450
451static int pcu_sock_send(struct gsm_bts *bts, struct msgb *msg)
452{
453 struct pcu_sock_state *state = bts->pcu_state;
454 struct osmo_fd *conn_bfd;
455 struct gsm_pcu_if *pcu_prim = (struct gsm_pcu_if *) msg->data;
456
457 if (!state) {
458 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
459 LOGP(DPCU, LOGL_INFO, "PCU socket not created, "
460 "dropping message\n");
461 msgb_free(msg);
462 return -EINVAL;
463 }
464 conn_bfd = &state->conn_bfd;
465 if (conn_bfd->fd <= 0) {
466 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
467 LOGP(DPCU, LOGL_NOTICE, "PCU socket not connected, "
468 "dropping message\n");
469 msgb_free(msg);
470 return -EIO;
471 }
472 msgb_enqueue(&state->upqueue, msg);
473 conn_bfd->when |= BSC_FD_WRITE;
474
475 return 0;
476}
477
478static void pcu_sock_close(struct pcu_sock_state *state)
479{
480 struct osmo_fd *bfd = &state->conn_bfd;
481 struct gsm_bts *bts;
482 struct gsm_bts_trx *trx;
483 struct gsm_bts_trx_ts *ts;
484 int i, j;
485
486 /* FIXME: allow multiple BTS */
487 bts = llist_entry(state->net->bts_list.next, struct gsm_bts, list);
488
489 LOGP(DPCU, LOGL_NOTICE, "PCU socket has LOST connection\n");
490
491 close(bfd->fd);
492 bfd->fd = -1;
493 osmo_fd_unregister(bfd);
494
495 /* re-enable the generation of ACCEPT for new connections */
496 state->listen_bfd.when |= BSC_FD_READ;
497
498#if 0
499 /* remove si13, ... */
500 bts->si_valid &= ~(1 << SYSINFO_TYPE_13);
501 osmo_signal_dispatch(SS_GLOBAL, S_NEW_SYSINFO, bts);
502#endif
503
504 /* release PDCH */
505 for (i = 0; i < 8; i++) {
Harald Welte67798612016-11-17 18:10:10 +0100506 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200507 if (!trx)
508 break;
509 for (j = 0; j < 8; j++) {
510 ts = &trx->ts[j];
511 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
512 && ts->pchan == GSM_PCHAN_PDCH) {
513 printf("l1sap_chan_rel(trx,gsm_lchan2chan_nr(ts->lchan));\n");
514 }
515 }
516 }
517
518 /* flush the queue */
519 while (!llist_empty(&state->upqueue)) {
520 struct msgb *msg = msgb_dequeue(&state->upqueue);
521 msgb_free(msg);
522 }
523}
524
525static int pcu_sock_read(struct osmo_fd *bfd)
526{
527 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
528 struct gsm_pcu_if *pcu_prim;
529 struct msgb *msg;
530 int rc;
531
532 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
533 if (!msg)
534 return -ENOMEM;
535
536 pcu_prim = (struct gsm_pcu_if *) msg->tail;
537
538 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
539 if (rc == 0)
540 goto close;
541
542 if (rc < 0) {
543 if (errno == EAGAIN)
544 return 0;
545 goto close;
546 }
547
548 rc = pcu_rx(state->net, pcu_prim->msg_type, pcu_prim);
549
550 /* as we always synchronously process the message in pcu_rx() and
551 * its callbacks, we can free the message here. */
552 msgb_free(msg);
553
554 return rc;
555
556close:
557 msgb_free(msg);
558 pcu_sock_close(state);
559 return -1;
560}
561
562static int pcu_sock_write(struct osmo_fd *bfd)
563{
564 struct pcu_sock_state *state = bfd->data;
565 int rc;
566
567 while (!llist_empty(&state->upqueue)) {
568 struct msgb *msg, *msg2;
569 struct gsm_pcu_if *pcu_prim;
570
571 /* peek at the beginning of the queue */
572 msg = llist_entry(state->upqueue.next, struct msgb, list);
573 pcu_prim = (struct gsm_pcu_if *)msg->data;
574
575 bfd->when &= ~BSC_FD_WRITE;
576
577 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
578 if (!msgb_length(msg)) {
579 LOGP(DPCU, LOGL_ERROR, "message type (%d) with ZERO "
580 "bytes!\n", pcu_prim->msg_type);
581 goto dontsend;
582 }
583
584 /* try to send it over the socket */
585 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
586 if (rc == 0)
587 goto close;
588 if (rc < 0) {
589 if (errno == EAGAIN) {
590 bfd->when |= BSC_FD_WRITE;
591 break;
592 }
593 goto close;
594 }
595
596dontsend:
597 /* _after_ we send it, we can deueue */
598 msg2 = msgb_dequeue(&state->upqueue);
599 assert(msg == msg2);
600 msgb_free(msg);
601 }
602 return 0;
603
604close:
605 pcu_sock_close(state);
606
607 return -1;
608}
609
610static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
611{
612 int rc = 0;
613
614 if (flags & BSC_FD_READ)
615 rc = pcu_sock_read(bfd);
616 if (rc < 0)
617 return rc;
618
619 if (flags & BSC_FD_WRITE)
620 rc = pcu_sock_write(bfd);
621
622 return rc;
623}
624
625/* accept connection comming from PCU */
626static int pcu_sock_accept(struct osmo_fd *bfd, unsigned int flags)
627{
628 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
629 struct osmo_fd *conn_bfd = &state->conn_bfd;
630 struct sockaddr_un un_addr;
631 socklen_t len;
632 int rc;
633
634 len = sizeof(un_addr);
635 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
636 if (rc < 0) {
637 LOGP(DPCU, LOGL_ERROR, "Failed to accept a new connection\n");
638 return -1;
639 }
640
641 if (conn_bfd->fd >= 0) {
642 LOGP(DPCU, LOGL_NOTICE, "PCU connects but we already have "
643 "another active connection ?!?\n");
644 /* We already have one PCU connected, this is all we support */
645 state->listen_bfd.when &= ~BSC_FD_READ;
646 close(rc);
647 return 0;
648 }
649
650 conn_bfd->fd = rc;
651 conn_bfd->when = BSC_FD_READ;
652 conn_bfd->cb = pcu_sock_cb;
653 conn_bfd->data = state;
654
655 if (osmo_fd_register(conn_bfd) != 0) {
656 LOGP(DPCU, LOGL_ERROR, "Failed to register new connection "
657 "fd\n");
658 close(conn_bfd->fd);
659 conn_bfd->fd = -1;
660 return -1;
661 }
662
663 LOGP(DPCU, LOGL_NOTICE, "PCU socket connected to external PCU\n");
664
665 return 0;
666}
667
668/* Open connection to PCU */
669int pcu_sock_init(const char *path, struct gsm_bts *bts)
670{
671 struct pcu_sock_state *state;
672 struct osmo_fd *bfd;
673 int rc;
674
675 state = talloc_zero(NULL, struct pcu_sock_state);
676 if (!state)
677 return -ENOMEM;
678
679 INIT_LLIST_HEAD(&state->upqueue);
680 state->net = bts->network;
681 state->conn_bfd.fd = -1;
682
683 bfd = &state->listen_bfd;
684
685 bfd->fd = osmo_sock_unix_init(SOCK_SEQPACKET, 0, path,
686 OSMO_SOCK_F_BIND);
687 if (bfd->fd < 0) {
688 LOGP(DPCU, LOGL_ERROR, "Could not create unix socket: %s\n",
689 strerror(errno));
690 talloc_free(state);
691 return -1;
692 }
693
694 bfd->when = BSC_FD_READ;
695 bfd->cb = pcu_sock_accept;
696 bfd->data = state;
697
698 rc = osmo_fd_register(bfd);
699 if (rc < 0) {
700 LOGP(DPCU, LOGL_ERROR, "Could not register listen fd: %d\n",
701 rc);
702 close(bfd->fd);
703 talloc_free(state);
704 return rc;
705 }
706
707 bts->pcu_state = state;
708 return 0;
709}
710
711/* Close connection to PCU */
712void pcu_sock_exit(struct gsm_bts *bts)
713{
714 struct pcu_sock_state *state = bts->pcu_state;
715 struct osmo_fd *bfd, *conn_bfd;
716
717 if (!state)
718 return;
719
720 conn_bfd = &state->conn_bfd;
721 if (conn_bfd->fd > 0)
722 pcu_sock_close(state);
723 bfd = &state->listen_bfd;
724 close(bfd->fd);
725 osmo_fd_unregister(bfd);
726 talloc_free(state);
727 bts->pcu_state = NULL;
728}
729