blob: 64422c724196ca47328bf8d8bb78fc50c486c4ac [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
Neels Hofmeyrc0164792017-09-04 15:15:32 +020040#include <osmocom/bsc/gsm_data.h>
41#include <osmocom/bsc/pcu_if.h>
42#include <osmocom/bsc/pcuif_proto.h>
43#include <osmocom/bsc/signal.h>
44#include <osmocom/bsc/debug.h>
45#include <osmocom/bsc/abis_rsl.h>
Philipp Maierb4999b62016-10-26 15:19:41 +020046
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 */
Neels Hofmeyrf93970b2018-03-05 02:09:40 +0100155 info_ind->mcc = bts->network->plmn.mcc;
156 info_ind->mnc = bts->network->plmn.mnc;
Neels Hofmeyr4ac77632018-02-22 06:15:58 +0100157 info_ind->mnc_3_digits = bts->network->plmn.mnc_3_digits;
Philipp Maierb4999b62016-10-26 15:19:41 +0200158 info_ind->lac = bts->location_area_code;
159 info_ind->rac = bts->gprs.rac;
160
161 /* NSE */
162 info_ind->nsei = bts->gprs.nse.nsei;
163 memcpy(info_ind->nse_timer, bts->gprs.nse.timer, 7);
164 memcpy(info_ind->cell_timer, bts->gprs.cell.timer, 11);
165
166 /* cell attributes */
167 info_ind->cell_id = bts->cell_identity;
168 info_ind->repeat_time = rlcc->paging.repeat_time;
169 info_ind->repeat_count = rlcc->paging.repeat_count;
170 info_ind->bvci = bts->gprs.cell.bvci;
171 info_ind->t3142 = rlcc->parameter[RLC_T3142];
172 info_ind->t3169 = rlcc->parameter[RLC_T3169];
173 info_ind->t3191 = rlcc->parameter[RLC_T3191];
174 info_ind->t3193_10ms = rlcc->parameter[RLC_T3193];
175 info_ind->t3195 = rlcc->parameter[RLC_T3195];
176 info_ind->n3101 = rlcc->parameter[RLC_N3101];
177 info_ind->n3103 = rlcc->parameter[RLC_N3103];
178 info_ind->n3105 = rlcc->parameter[RLC_N3105];
179 info_ind->cv_countdown = rlcc->parameter[CV_COUNTDOWN];
180 if (rlcc->cs_mask & (1 << GPRS_CS1))
181 info_ind->flags |= PCU_IF_FLAG_CS1;
182 if (rlcc->cs_mask & (1 << GPRS_CS2))
183 info_ind->flags |= PCU_IF_FLAG_CS2;
184 if (rlcc->cs_mask & (1 << GPRS_CS3))
185 info_ind->flags |= PCU_IF_FLAG_CS3;
186 if (rlcc->cs_mask & (1 << GPRS_CS4))
187 info_ind->flags |= PCU_IF_FLAG_CS4;
188 if (bts->gprs.mode == BTS_GPRS_EGPRS) {
189 if (rlcc->cs_mask & (1 << GPRS_MCS1))
190 info_ind->flags |= PCU_IF_FLAG_MCS1;
191 if (rlcc->cs_mask & (1 << GPRS_MCS2))
192 info_ind->flags |= PCU_IF_FLAG_MCS2;
193 if (rlcc->cs_mask & (1 << GPRS_MCS3))
194 info_ind->flags |= PCU_IF_FLAG_MCS3;
195 if (rlcc->cs_mask & (1 << GPRS_MCS4))
196 info_ind->flags |= PCU_IF_FLAG_MCS4;
197 if (rlcc->cs_mask & (1 << GPRS_MCS5))
198 info_ind->flags |= PCU_IF_FLAG_MCS5;
199 if (rlcc->cs_mask & (1 << GPRS_MCS6))
200 info_ind->flags |= PCU_IF_FLAG_MCS6;
201 if (rlcc->cs_mask & (1 << GPRS_MCS7))
202 info_ind->flags |= PCU_IF_FLAG_MCS7;
203 if (rlcc->cs_mask & (1 << GPRS_MCS8))
204 info_ind->flags |= PCU_IF_FLAG_MCS8;
205 if (rlcc->cs_mask & (1 << GPRS_MCS9))
206 info_ind->flags |= PCU_IF_FLAG_MCS9;
207 }
208#warning "isn't dl_tbf_ext wrong?: * 10 and no ntohs"
209 info_ind->dl_tbf_ext = rlcc->parameter[T_DL_TBF_EXT];
210#warning "isn't ul_tbf_ext wrong?: * 10 and no ntohs"
211 info_ind->ul_tbf_ext = rlcc->parameter[T_UL_TBF_EXT];
212 info_ind->initial_cs = rlcc->initial_cs;
213 info_ind->initial_mcs = rlcc->initial_mcs;
214
215 /* NSVC */
Harald Weltee586f412016-11-17 18:39:36 +0100216 for (i = 0; i < ARRAY_SIZE(info_ind->nsvci); i++) {
Philipp Maierb4999b62016-10-26 15:19:41 +0200217 nsvc = &bts->gprs.nsvc[i];
218 info_ind->nsvci[i] = nsvc->nsvci;
219 info_ind->local_port[i] = nsvc->local_port;
220 info_ind->remote_port[i] = nsvc->remote_port;
221 info_ind->remote_ip[i] = nsvc->remote_ip;
222 }
223
Harald Weltee586f412016-11-17 18:39:36 +0100224 for (i = 0; i < ARRAY_SIZE(info_ind->trx); i++) {
Harald Welte67798612016-11-17 18:10:10 +0100225 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200226 if (!trx)
Alexander Couzens872671e2016-11-29 00:21:18 +0100227 continue;
Harald Welte54050a22016-11-21 01:33:22 +0100228 info_ind->trx[i].hlayer1 = 0x2342;
Philipp Maierb4999b62016-10-26 15:19:41 +0200229 info_ind->trx[i].pdch_mask = 0;
230 info_ind->trx[i].arfcn = trx->arfcn;
Harald Weltee586f412016-11-17 18:39:36 +0100231 for (j = 0; j < ARRAY_SIZE(trx->ts); j++) {
Philipp Maierb4999b62016-10-26 15:19:41 +0200232 ts = &trx->ts[j];
233 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
234 && ts_should_be_pdch(ts)) {
235 info_ind->trx[i].pdch_mask |= (1 << j);
236 info_ind->trx[i].tsc[j] =
237 (ts->tsc >= 0) ? ts->tsc : bts->bsic & 7;
238 LOGP(DPCU, LOGL_INFO, "trx=%d ts=%d: "
239 "available (tsc=%d arfcn=%d)\n",
240 trx->nr, ts->nr,
241 info_ind->trx[i].tsc[j],
242 info_ind->trx[i].arfcn);
243 }
244 }
245 }
246
247 return pcu_sock_send(bts, msg);
248}
249
250void pcu_info_update(struct gsm_bts *bts)
251{
252 if (pcu_connected(bts))
253 pcu_tx_info_ind(bts);
254}
255
256/* Forward rach indication to PCU */
257int pcu_tx_rach_ind(struct gsm_bts *bts, int16_t qta, uint16_t ra, uint32_t fn,
258 uint8_t is_11bit, enum ph_burst_type burst_type)
259{
260 struct msgb *msg;
261 struct gsm_pcu_if *pcu_prim;
262 struct gsm_pcu_if_rach_ind *rach_ind;
263
264 /* Bail if no PCU is connected */
265 if (!pcu_connected(bts)) {
266 LOGP(DRSL, LOGL_ERROR, "BTS %d CHAN RQD(GPRS) but PCU not "
267 "connected!\n", bts->nr);
268 return -ENODEV;
269 }
270
271 LOGP(DPCU, LOGL_INFO, "Sending RACH indication: qta=%d, ra=%d, "
272 "fn=%d\n", qta, ra, fn);
273
274 msg = pcu_msgb_alloc(PCU_IF_MSG_RACH_IND, bts->nr);
275 if (!msg)
276 return -ENOMEM;
277 pcu_prim = (struct gsm_pcu_if *) msg->data;
278 rach_ind = &pcu_prim->u.rach_ind;
279
280 rach_ind->sapi = PCU_IF_SAPI_RACH;
281 rach_ind->ra = ra;
282 rach_ind->qta = qta;
283 rach_ind->fn = fn;
284 rach_ind->is_11bit = is_11bit;
285 rach_ind->burst_type = burst_type;
286
287 return pcu_sock_send(bts, msg);
288}
289
Philipp Maierf8aeb2c2016-12-02 19:04:34 +0100290/* Confirm the sending of an immediate assignment to the pcu */
291int pcu_tx_imm_ass_sent(struct gsm_bts *bts, uint32_t tlli)
292{
293 struct msgb *msg;
294 struct gsm_pcu_if *pcu_prim;
295 struct gsm_pcu_if_data_cnf_dt *data_cnf_dt;
296
297 LOGP(DPCU, LOGL_INFO, "Sending PCH confirm with direct TLLI\n");
298
299 msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_CNF_DT, bts->nr);
300 if (!msg)
301 return -ENOMEM;
302 pcu_prim = (struct gsm_pcu_if *) msg->data;
303 data_cnf_dt = &pcu_prim->u.data_cnf_dt;
304
305 data_cnf_dt->sapi = PCU_IF_SAPI_PCH;
306 data_cnf_dt->tlli = tlli;
307
308 return pcu_sock_send(bts, msg);
309}
310
Harald Welte854bcc22016-11-17 20:54:47 +0100311/* we need to decode the raw RR paging messsage (see PCU code
312 * Encoding::write_paging_request) and extract the mobile identity
313 * (P-TMSI) from it */
314static int pcu_rx_rr_paging(struct gsm_bts *bts, uint8_t paging_group,
315 const uint8_t *raw_rr_msg)
316{
Philipp Maier722bbb42017-01-17 14:46:56 +0100317 struct gsm48_paging1 *p1 = (struct gsm48_paging1 *) raw_rr_msg;
Harald Welte854bcc22016-11-17 20:54:47 +0100318 uint8_t chan_needed;
319 unsigned int mi_len;
320 uint8_t *mi;
321 int rc;
322
Philipp Maier722bbb42017-01-17 14:46:56 +0100323 switch (p1->msg_type) {
Harald Welte854bcc22016-11-17 20:54:47 +0100324 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;
Philipp Maier722bbb42017-01-17 14:46:56 +0100328 LOGP(DPCU, LOGL_ERROR, "PCU Sends paging "
329 "request type %02x (chan_needed=%02x, mi_len=%u, mi=%s)\n",
330 p1->msg_type, chan_needed, mi_len,
331 osmo_hexdump_nospc(mi,mi_len));
332 /* NOTE: We will have to add 2 to mi_len and subtract 2 from
333 * the mi pointer because rsl_paging_cmd() will perform the
334 * reverse operations. This is because rsl_paging_cmd() is
335 * normally expected to chop off the element identifier (0xC0)
336 * and the length field. In our parameter, we do not have
337 * those fields included. */
338 rc = rsl_paging_cmd(bts, paging_group, mi_len+2, mi-2,
Harald Welte854bcc22016-11-17 20:54:47 +0100339 chan_needed, true);
340 break;
341 case GSM48_MT_RR_PAG_REQ_2:
342 case GSM48_MT_RR_PAG_REQ_3:
343 LOGP(DPCU, LOGL_ERROR, "PCU Sends unsupported paging "
Philipp Maier722bbb42017-01-17 14:46:56 +0100344 "request type %02x\n", p1->msg_type);
345 rc = -EINVAL;
346 break;
347 default:
348 LOGP(DPCU, LOGL_ERROR, "PCU Sends unknown paging "
349 "request type %02x\n", p1->msg_type);
Harald Welte854bcc22016-11-17 20:54:47 +0100350 rc = -EINVAL;
351 break;
352 }
353
354 return rc;
355}
356
Philipp Maierb4999b62016-10-26 15:19:41 +0200357static int pcu_rx_data_req(struct gsm_bts *bts, uint8_t msg_type,
358 struct gsm_pcu_if_data *data_req)
359{
Philipp Maierb4999b62016-10-26 15:19:41 +0200360 struct msgb *msg;
Harald Welte854bcc22016-11-17 20:54:47 +0100361 char imsi_digit_buf[4];
Alexander Couzensf14cb352016-12-02 18:27:01 +0100362 uint32_t tlli = -1;
Harald Welte854bcc22016-11-17 20:54:47 +0100363 uint8_t pag_grp;
Philipp Maierb4999b62016-10-26 15:19:41 +0200364 int rc = 0;
365
366 LOGP(DPCU, LOGL_DEBUG, "Data request received: sapi=%s arfcn=%d "
367 "block=%d data=%s\n", sapi_string[data_req->sapi],
368 data_req->arfcn, data_req->block_nr,
369 osmo_hexdump(data_req->data, data_req->len));
370
371 switch (data_req->sapi) {
372 case PCU_IF_SAPI_PCH:
Harald Welte854bcc22016-11-17 20:54:47 +0100373 /* the first three bytes are the last three digits of
374 * the IMSI, which we need to compute the paging group */
375 imsi_digit_buf[0] = data_req->data[0];
376 imsi_digit_buf[1] = data_req->data[1];
377 imsi_digit_buf[2] = data_req->data[2];
378 imsi_digit_buf[3] = '\0';
Philipp Maier722bbb42017-01-17 14:46:56 +0100379 LOGP(DPCU, LOGL_DEBUG, "SAPI PCH imsi %s\n", imsi_digit_buf);
Harald Welte854bcc22016-11-17 20:54:47 +0100380 pag_grp = gsm0502_calc_paging_group(&bts->si_common.chan_desc,
381 str_to_imsi(imsi_digit_buf));
382 pcu_rx_rr_paging(bts, pag_grp, data_req->data+3);
Philipp Maierb4999b62016-10-26 15:19:41 +0200383 break;
384 case PCU_IF_SAPI_AGCH:
385 msg = msgb_alloc(data_req->len, "pcu_agch");
386 if (!msg) {
387 rc = -ENOMEM;
388 break;
389 }
390 msg->l3h = msgb_put(msg, data_req->len);
391 memcpy(msg->l3h, data_req->data, data_req->len);
392
393 if (rsl_imm_assign_cmd(bts, msg->len, msg->data)) {
394 msgb_free(msg);
395 rc = -EIO;
396 }
397 break;
Alexander Couzensf14cb352016-12-02 18:27:01 +0100398 case PCU_IF_SAPI_AGCH_DT:
399 /* DT = direct tlli. A tlli is prefixed */
400
401 if (data_req->len < 5) {
402 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
403 "invalid/small length %d\n", data_req->len);
404 break;
405 }
Pau Espin Pedrol9d075ea2018-03-17 02:07:26 +0100406 memcpy(&tlli, data_req->data, 4);
Alexander Couzensf14cb352016-12-02 18:27:01 +0100407
408 msg = msgb_alloc(data_req->len - 4, "pcu_agch");
409 if (!msg) {
410 rc = -ENOMEM;
411 break;
412 }
413 msg->l3h = msgb_put(msg, data_req->len - 4);
414 memcpy(msg->l3h, data_req->data + 4, data_req->len - 4);
415
416 if (bts->type == GSM_BTS_TYPE_RBS2000)
417 rc = rsl_ericsson_imm_assign_cmd(bts, tlli, msg->len, msg->data);
418 else
419 rc = rsl_imm_assign_cmd(bts, msg->len, msg->data);
420
421 if (rc) {
422 msgb_free(msg);
423 rc = -EIO;
424 }
425 break;
Philipp Maierb4999b62016-10-26 15:19:41 +0200426 default:
427 LOGP(DPCU, LOGL_ERROR, "Received PCU data request with "
428 "unsupported sapi %d\n", data_req->sapi);
429 rc = -EINVAL;
430 }
431
432 return rc;
433}
434
435static int pcu_rx(struct gsm_network *net, uint8_t msg_type,
436 struct gsm_pcu_if *pcu_prim)
437{
438 int rc = 0;
439 struct gsm_bts *bts;
440
441 /* FIXME: allow multiple BTS */
442 bts = llist_entry(net->bts_list.next, struct gsm_bts, list);
443
444 switch (msg_type) {
445 case PCU_IF_MSG_DATA_REQ:
446 case PCU_IF_MSG_PAG_REQ:
447 rc = pcu_rx_data_req(bts, msg_type, &pcu_prim->u.data_req);
448 break;
449 default:
450 LOGP(DPCU, LOGL_ERROR, "Received unknwon PCU msg type %d\n",
451 msg_type);
452 rc = -EINVAL;
453 }
454
455 return rc;
456}
457
458/*
459 * PCU socket interface
460 */
461
462static int pcu_sock_send(struct gsm_bts *bts, struct msgb *msg)
463{
464 struct pcu_sock_state *state = bts->pcu_state;
465 struct osmo_fd *conn_bfd;
466 struct gsm_pcu_if *pcu_prim = (struct gsm_pcu_if *) msg->data;
467
468 if (!state) {
469 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
470 LOGP(DPCU, LOGL_INFO, "PCU socket not created, "
471 "dropping message\n");
472 msgb_free(msg);
473 return -EINVAL;
474 }
475 conn_bfd = &state->conn_bfd;
476 if (conn_bfd->fd <= 0) {
477 if (pcu_prim->msg_type != PCU_IF_MSG_TIME_IND)
478 LOGP(DPCU, LOGL_NOTICE, "PCU socket not connected, "
479 "dropping message\n");
480 msgb_free(msg);
481 return -EIO;
482 }
483 msgb_enqueue(&state->upqueue, msg);
484 conn_bfd->when |= BSC_FD_WRITE;
485
486 return 0;
487}
488
489static void pcu_sock_close(struct pcu_sock_state *state)
490{
491 struct osmo_fd *bfd = &state->conn_bfd;
492 struct gsm_bts *bts;
493 struct gsm_bts_trx *trx;
494 struct gsm_bts_trx_ts *ts;
495 int i, j;
496
497 /* FIXME: allow multiple BTS */
498 bts = llist_entry(state->net->bts_list.next, struct gsm_bts, list);
499
500 LOGP(DPCU, LOGL_NOTICE, "PCU socket has LOST connection\n");
501
502 close(bfd->fd);
503 bfd->fd = -1;
504 osmo_fd_unregister(bfd);
505
506 /* re-enable the generation of ACCEPT for new connections */
507 state->listen_bfd.when |= BSC_FD_READ;
508
509#if 0
510 /* remove si13, ... */
511 bts->si_valid &= ~(1 << SYSINFO_TYPE_13);
512 osmo_signal_dispatch(SS_GLOBAL, S_NEW_SYSINFO, bts);
513#endif
514
515 /* release PDCH */
516 for (i = 0; i < 8; i++) {
Harald Welte67798612016-11-17 18:10:10 +0100517 trx = gsm_bts_trx_num(bts, i);
Philipp Maierb4999b62016-10-26 15:19:41 +0200518 if (!trx)
519 break;
520 for (j = 0; j < 8; j++) {
521 ts = &trx->ts[j];
522 if (ts->mo.nm_state.operational == NM_OPSTATE_ENABLED
523 && ts->pchan == GSM_PCHAN_PDCH) {
524 printf("l1sap_chan_rel(trx,gsm_lchan2chan_nr(ts->lchan));\n");
525 }
526 }
527 }
528
529 /* flush the queue */
530 while (!llist_empty(&state->upqueue)) {
531 struct msgb *msg = msgb_dequeue(&state->upqueue);
532 msgb_free(msg);
533 }
534}
535
536static int pcu_sock_read(struct osmo_fd *bfd)
537{
538 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
539 struct gsm_pcu_if *pcu_prim;
540 struct msgb *msg;
541 int rc;
542
543 msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx");
544 if (!msg)
545 return -ENOMEM;
546
547 pcu_prim = (struct gsm_pcu_if *) msg->tail;
548
549 rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0);
550 if (rc == 0)
551 goto close;
552
553 if (rc < 0) {
554 if (errno == EAGAIN)
555 return 0;
556 goto close;
557 }
558
559 rc = pcu_rx(state->net, pcu_prim->msg_type, pcu_prim);
560
561 /* as we always synchronously process the message in pcu_rx() and
562 * its callbacks, we can free the message here. */
563 msgb_free(msg);
564
565 return rc;
566
567close:
568 msgb_free(msg);
569 pcu_sock_close(state);
570 return -1;
571}
572
573static int pcu_sock_write(struct osmo_fd *bfd)
574{
575 struct pcu_sock_state *state = bfd->data;
576 int rc;
577
578 while (!llist_empty(&state->upqueue)) {
579 struct msgb *msg, *msg2;
580 struct gsm_pcu_if *pcu_prim;
581
582 /* peek at the beginning of the queue */
583 msg = llist_entry(state->upqueue.next, struct msgb, list);
584 pcu_prim = (struct gsm_pcu_if *)msg->data;
585
586 bfd->when &= ~BSC_FD_WRITE;
587
588 /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */
589 if (!msgb_length(msg)) {
590 LOGP(DPCU, LOGL_ERROR, "message type (%d) with ZERO "
591 "bytes!\n", pcu_prim->msg_type);
592 goto dontsend;
593 }
594
595 /* try to send it over the socket */
596 rc = write(bfd->fd, msgb_data(msg), msgb_length(msg));
597 if (rc == 0)
598 goto close;
599 if (rc < 0) {
600 if (errno == EAGAIN) {
601 bfd->when |= BSC_FD_WRITE;
602 break;
603 }
604 goto close;
605 }
606
607dontsend:
608 /* _after_ we send it, we can deueue */
609 msg2 = msgb_dequeue(&state->upqueue);
610 assert(msg == msg2);
611 msgb_free(msg);
612 }
613 return 0;
614
615close:
616 pcu_sock_close(state);
617
618 return -1;
619}
620
621static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags)
622{
623 int rc = 0;
624
625 if (flags & BSC_FD_READ)
626 rc = pcu_sock_read(bfd);
627 if (rc < 0)
628 return rc;
629
630 if (flags & BSC_FD_WRITE)
631 rc = pcu_sock_write(bfd);
632
633 return rc;
634}
635
636/* accept connection comming from PCU */
637static int pcu_sock_accept(struct osmo_fd *bfd, unsigned int flags)
638{
639 struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data;
640 struct osmo_fd *conn_bfd = &state->conn_bfd;
641 struct sockaddr_un un_addr;
642 socklen_t len;
643 int rc;
644
645 len = sizeof(un_addr);
646 rc = accept(bfd->fd, (struct sockaddr *) &un_addr, &len);
647 if (rc < 0) {
648 LOGP(DPCU, LOGL_ERROR, "Failed to accept a new connection\n");
649 return -1;
650 }
651
652 if (conn_bfd->fd >= 0) {
653 LOGP(DPCU, LOGL_NOTICE, "PCU connects but we already have "
654 "another active connection ?!?\n");
655 /* We already have one PCU connected, this is all we support */
656 state->listen_bfd.when &= ~BSC_FD_READ;
657 close(rc);
658 return 0;
659 }
660
661 conn_bfd->fd = rc;
662 conn_bfd->when = BSC_FD_READ;
663 conn_bfd->cb = pcu_sock_cb;
664 conn_bfd->data = state;
665
666 if (osmo_fd_register(conn_bfd) != 0) {
667 LOGP(DPCU, LOGL_ERROR, "Failed to register new connection "
668 "fd\n");
669 close(conn_bfd->fd);
670 conn_bfd->fd = -1;
671 return -1;
672 }
673
674 LOGP(DPCU, LOGL_NOTICE, "PCU socket connected to external PCU\n");
675
676 return 0;
677}
678
679/* Open connection to PCU */
680int pcu_sock_init(const char *path, struct gsm_bts *bts)
681{
682 struct pcu_sock_state *state;
683 struct osmo_fd *bfd;
684 int rc;
685
686 state = talloc_zero(NULL, struct pcu_sock_state);
687 if (!state)
688 return -ENOMEM;
689
690 INIT_LLIST_HEAD(&state->upqueue);
691 state->net = bts->network;
692 state->conn_bfd.fd = -1;
693
694 bfd = &state->listen_bfd;
695
696 bfd->fd = osmo_sock_unix_init(SOCK_SEQPACKET, 0, path,
697 OSMO_SOCK_F_BIND);
698 if (bfd->fd < 0) {
699 LOGP(DPCU, LOGL_ERROR, "Could not create unix socket: %s\n",
700 strerror(errno));
701 talloc_free(state);
702 return -1;
703 }
704
705 bfd->when = BSC_FD_READ;
706 bfd->cb = pcu_sock_accept;
707 bfd->data = state;
708
709 rc = osmo_fd_register(bfd);
710 if (rc < 0) {
711 LOGP(DPCU, LOGL_ERROR, "Could not register listen fd: %d\n",
712 rc);
713 close(bfd->fd);
714 talloc_free(state);
715 return rc;
716 }
717
718 bts->pcu_state = state;
719 return 0;
720}
721
722/* Close connection to PCU */
723void pcu_sock_exit(struct gsm_bts *bts)
724{
725 struct pcu_sock_state *state = bts->pcu_state;
726 struct osmo_fd *bfd, *conn_bfd;
727
728 if (!state)
729 return;
730
731 conn_bfd = &state->conn_bfd;
732 if (conn_bfd->fd > 0)
733 pcu_sock_close(state);
734 bfd = &state->listen_bfd;
735 close(bfd->fd);
736 osmo_fd_unregister(bfd);
737 talloc_free(state);
738 bts->pcu_state = NULL;
739}
740