1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/* GSMTAP support code in libmsomcore */
/*
* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "../config.h"
#include <osmocom/core/gsmtap_util.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/gsmtap.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/select.h>
#include <osmocom/core/socket.h>
#include <osmocom/gsm/protocol/gsm_04_08.h>
#include <osmocom/gsm/rsl.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
/*! \addtogroup gsmtap
* @{
*/
/*! \file gsmtap_util.c */
/*! \brief convert RSL channel number to GSMTAP channel type
* \param[in] rsl_chantype RSL channel type
* \param[in] link_id RSL link identifier
* \returns GSMTAP channel type
*/
uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
{
uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
switch (rsl_chantype) {
case RSL_CHAN_Bm_ACCHs:
ret = GSMTAP_CHANNEL_TCH_F;
break;
case RSL_CHAN_Lm_ACCHs:
ret = GSMTAP_CHANNEL_TCH_H;
break;
case RSL_CHAN_SDCCH4_ACCH:
ret = GSMTAP_CHANNEL_SDCCH4;
break;
case RSL_CHAN_SDCCH8_ACCH:
ret = GSMTAP_CHANNEL_SDCCH8;
break;
case RSL_CHAN_BCCH:
ret = GSMTAP_CHANNEL_BCCH;
break;
case RSL_CHAN_RACH:
ret = GSMTAP_CHANNEL_RACH;
break;
case RSL_CHAN_PCH_AGCH:
/* it could also be AGCH... */
ret = GSMTAP_CHANNEL_PCH;
break;
}
if (link_id & 0x40)
ret |= GSMTAP_CHANNEL_ACCH;
return ret;
}
/*! \brief create an arbitrary type GSMTAP message
* \param[in] type The GSMTAP_TYPE_xxx constant of the message to create
* \param[in] arfcn GSM ARFCN (Channel Number)
* \param[in] ts GSM time slot
* \param[in] chan_type Channel Type
* \param[in] ss Sub-slot
* \param[in] fn GSM Frame Number
* \param[in] signal_dbm Signal Strength (dBm)
* \param[in] snr Signal/Noise Ratio (SNR)
* \param[in] data Pointer to data buffer
* \param[in] len Length of \ref data
* \return dynamically allocated message buffer containing data
*
* This function will allocate a new msgb and fill it with a GSMTAP
* header containing the information
*/
struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
uint8_t ss, uint32_t fn, int8_t signal_dbm,
uint8_t snr, const uint8_t *data, unsigned int len)
{
struct msgb *msg;
struct gsmtap_hdr *gh;
uint8_t *dst;
msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
if (!msg)
return NULL;
gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
gh->version = GSMTAP_VERSION;
gh->hdr_len = sizeof(*gh)/4;
gh->type = type;
gh->timeslot = ts;
gh->sub_slot = ss;
gh->arfcn = htons(arfcn);
gh->snr_db = snr;
gh->signal_dbm = signal_dbm;
gh->frame_number = htonl(fn);
gh->sub_type = chan_type;
gh->antenna_nr = 0;
dst = msgb_put(msg, len);
memcpy(dst, data, len);
return msg;
}
/*! \brief create L1/L2 data and put it into GSMTAP
* \param[in] arfcn GSM ARFCN (Channel Number)
* \param[in] ts GSM time slot
* \param[in] chan_type Channel Type
* \param[in] ss Sub-slot
* \param[in] fn GSM Frame Number
* \param[in] signal_dbm Signal Strength (dBm)
* \param[in] snr Signal/Noise Ratio (SNR)
* \param[in] data Pointer to data buffer
* \param[in] len Length of \ref data
* \return message buffer or NULL in case of error
*
* This function will allocate a new msgb and fill it with a GSMTAP
* header containing the information
*/
struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
uint8_t ss, uint32_t fn, int8_t signal_dbm,
uint8_t snr, const uint8_t *data, unsigned int len)
{
return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
ss, fn, signal_dbm, snr, data, len);
}
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#include <netinet/in.h>
/*! \brief Create a new (sending) GSMTAP source socket
* \param[in] host host name or IP address in string format
* \param[in] port UDP port number in host byte order
* \return file descriptor of the new socket
*
* Opens a GSMTAP source (sending) socket, conncet it to host/port and
* return resulting fd. If \a host is NULL, the destination address
* will be localhost. If \a port is 0, the default \ref
* GSMTAP_UDP_PORT will be used.
* */
int gsmtap_source_init_fd(const char *host, uint16_t port)
{
if (port == 0)
port = GSMTAP_UDP_PORT;
if (host == NULL)
host = "localhost";
return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
OSMO_SOCK_F_CONNECT);
}
/*! \brief Add a local sink to an existing GSMTAP source and return fd
* \param[in] gsmtap_fd file descriptor of the gsmtap socket
* \returns file descriptor of locally bound receive socket
*
* In case the GSMTAP socket is connected to a local destination
* IP/port, this function creates a corresponding receiving socket
* bound to that destination IP + port.
*
* In case the gsmtap socket is not connected to a local IP/port, or
* creation of the receiving socket fails, a negative error code is
* returned.
*/
int gsmtap_source_add_sink_fd(int gsmtap_fd)
{
struct sockaddr_storage ss;
socklen_t ss_len = sizeof(ss);
int rc;
rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
if (rc < 0)
return rc;
if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
IPPROTO_UDP, OSMO_SOCK_F_BIND);
if (rc >= 0)
return rc;
}
return -ENODEV;
}
/*! \brief Send a \ref msgb through a GSMTAP source
* \param[in] gti GSMTAP instance
* \param[in] msg message buffer
* \return 0 in case of success; negative in case of error
*/
int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
{
if (!gti)
return -ENODEV;
if (gti->ofd_wq_mode)
return osmo_wqueue_enqueue(>i->wq, msg);
else {
/* try immediate send and return error if any */
int rc;
rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
if (rc <= 0) {
return rc;
} else if (rc >= msg->len) {
msgb_free(msg);
return 0;
} else {
/* short write */
return -EIO;
}
}
}
/*! \brief send an arbitrary type through GSMTAP.
* See \ref gsmtap_makemsg_ex for arguments
*/
int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
uint8_t chan_type, uint8_t ss, uint32_t fn,
int8_t signal_dbm, uint8_t snr, const uint8_t *data,
unsigned int len)
{
struct msgb *msg;
if (!gti)
return -ENODEV;
msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
snr, data, len);
if (!msg)
return -ENOMEM;
return gsmtap_sendmsg(gti, msg);
}
/*! \brief send a message from L1/L2 through GSMTAP.
* See \ref gsmtap_makemsg for arguments
*/
int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
uint8_t chan_type, uint8_t ss, uint32_t fn,
int8_t signal_dbm, uint8_t snr, const uint8_t *data,
unsigned int len)
{
return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
signal_dbm, snr, data, len);
}
/* Callback from select layer if we can write to the socket */
static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
{
int rc;
rc = write(ofd->fd, msg->data, msg->len);
if (rc < 0) {
return rc;
}
if (rc != msg->len) {
return -EIO;
}
return 0;
}
/* Callback from select layer if we can read from the sink socket */
static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
{
int rc;
uint8_t buf[4096];
if (!(flags & BSC_FD_READ))
return 0;
rc = read(fd->fd, buf, sizeof(buf));
if (rc < 0) {
return rc;
}
/* simply discard any data arriving on the socket */
return 0;
}
/*! \brief Add a local sink to an existing GSMTAP source and return fd
* \param[in] gsmtap_fd file descriptor of the gsmtap socket
* \returns file descriptor of locally bound receive socket
*
* In case the GSMTAP socket is connected to a local destination
* IP/port, this function creates a corresponding receiving socket
* bound to that destination IP + port.
*
* In case the gsmtap socket is not connected to a local IP/port, or
* creation of the receiving socket fails, a negative error code is
* returned.
*
* The file descriptor of the receiving socket is automatically added
* to the libosmocore select() handling.
*/
int gsmtap_source_add_sink(struct gsmtap_inst *gti)
{
int fd, rc;
fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
if (fd < 0)
return fd;
if (gti->ofd_wq_mode) {
struct osmo_fd *sink_ofd;
sink_ofd = >i->sink_ofd;
sink_ofd->fd = fd;
sink_ofd->when = BSC_FD_READ;
sink_ofd->cb = gsmtap_sink_fd_cb;
rc = osmo_fd_register(sink_ofd);
if (rc < 0) {
close(fd);
return rc;
}
}
return fd;
}
/*! \brief Open GSMTAP source socket, connect and register osmo_fd
* \param[in] host host name or IP address in string format
* \param[in] port UDP port number in host byte order
* \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0)
* \return callee-allocated \ref gsmtap_inst
*
* Open GSMTAP source (sending) socket, connect it to host/port,
* allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
* registration. This means it is like \ref gsmtap_init2 but integrated
* with libosmocore \ref select */
struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
int ofd_wq_mode)
{
struct gsmtap_inst *gti;
int fd, rc;
fd = gsmtap_source_init_fd(host, port);
if (fd < 0)
return NULL;
gti = talloc_zero(NULL, struct gsmtap_inst);
gti->ofd_wq_mode = ofd_wq_mode;
gti->wq.bfd.fd = fd;
gti->sink_ofd.fd = -1;
if (ofd_wq_mode) {
osmo_wqueue_init(>i->wq, 64);
gti->wq.write_cb = &gsmtap_wq_w_cb;
rc = osmo_fd_register(>i->wq.bfd);
if (rc < 0) {
close(fd);
return NULL;
}
}
return gti;
}
#endif /* HAVE_SYS_SOCKET_H */
/*! @} */
|