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