blob: 3fa9dd6593feb37b6cca56f0c7a330904d5947a8 [file] [log] [blame]
Harald Welte33cb71a2011-05-21 18:54:32 +02001/* GSMTAP support code in libmsomcore */
Harald Weltee779c362010-06-29 20:51:13 +02002/*
Harald Welte33cb71a2011-05-21 18:54:32 +02003 * (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
Harald Weltee779c362010-06-29 20:51:13 +02004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include "../config.h"
24
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010025#include <osmocom/core/gsmtap_util.h>
26#include <osmocom/core/logging.h>
27#include <osmocom/core/gsmtap.h>
28#include <osmocom/core/msgb.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020029#include <osmocom/core/talloc.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/select.h>
Harald Welte33cb71a2011-05-21 18:54:32 +020031#include <osmocom/core/socket.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010032#include <osmocom/gsm/protocol/gsm_04_08.h>
33#include <osmocom/gsm/rsl.h>
Harald Weltee779c362010-06-29 20:51:13 +020034
Harald Welte33cb71a2011-05-21 18:54:32 +020035#include <sys/types.h>
Harald Weltee4764422011-05-22 12:25:57 +020036
37#include <arpa/inet.h>
Harald Weltee779c362010-06-29 20:51:13 +020038
39#include <stdio.h>
40#include <unistd.h>
41#include <stdint.h>
42#include <string.h>
43#include <errno.h>
44
Harald Welte47379ca2011-08-17 16:35:24 +020045/*! \addtogroup gsmtap
46 * @{
47 */
48/*! \file gsmtap_util.c */
49
50
51/*! \brief convert RSL channel number to GSMTAP channel type
52 * \param[in] rsl_cantype RSL channel type
53 * \param[in] link_id RSL link identifier
54 * \returns GSMTAP channel type
55 */
Harald Weltee779c362010-06-29 20:51:13 +020056uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id)
57{
58 uint8_t ret = GSMTAP_CHANNEL_UNKNOWN;
59
60 switch (rsl_chantype) {
61 case RSL_CHAN_Bm_ACCHs:
62 ret = GSMTAP_CHANNEL_TCH_F;
63 break;
64 case RSL_CHAN_Lm_ACCHs:
65 ret = GSMTAP_CHANNEL_TCH_H;
66 break;
67 case RSL_CHAN_SDCCH4_ACCH:
68 ret = GSMTAP_CHANNEL_SDCCH4;
69 break;
70 case RSL_CHAN_SDCCH8_ACCH:
71 ret = GSMTAP_CHANNEL_SDCCH8;
72 break;
73 case RSL_CHAN_BCCH:
74 ret = GSMTAP_CHANNEL_BCCH;
75 break;
76 case RSL_CHAN_RACH:
77 ret = GSMTAP_CHANNEL_RACH;
78 break;
79 case RSL_CHAN_PCH_AGCH:
80 /* it could also be AGCH... */
81 ret = GSMTAP_CHANNEL_PCH;
82 break;
83 }
84
85 if (link_id & 0x40)
86 ret |= GSMTAP_CHANNEL_ACCH;
87
88 return ret;
89}
90
Harald Welte47379ca2011-08-17 16:35:24 +020091/*! \brief create L1/L2 data and put it into GSMTAP
92 * \param[in] arfcn GSM ARFCN (Channel Number)
93 * \param[in] ts GSM time slot
94 * \param[in] chan_type Channel Type
95 * \param[in] ss Sub-slot
96 * \param[in] fn GSM Frame Number
97 * \param[in] signal_dbm Signal Strength (dBm)
98 * \param[in] snr Signal/Noise Ratio (SNR)
99 * \param[in] data Pointer to data buffer
100 * \param[in] len Length of \ref data
101 *
102 * This function will allocate a new msgb and fill it with a GSMTAP
103 * header containing the information
104 */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200105struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type,
Harald Weltee34a9402010-06-29 22:31:21 +0200106 uint8_t ss, uint32_t fn, int8_t signal_dbm,
107 uint8_t snr, const uint8_t *data, unsigned int len)
Harald Weltee779c362010-06-29 20:51:13 +0200108{
109 struct msgb *msg;
110 struct gsmtap_hdr *gh;
111 uint8_t *dst;
112
Harald Weltee779c362010-06-29 20:51:13 +0200113 msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx");
114 if (!msg)
Harald Weltee34a9402010-06-29 22:31:21 +0200115 return NULL;
Harald Weltee779c362010-06-29 20:51:13 +0200116
117 gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh));
118
119 gh->version = GSMTAP_VERSION;
120 gh->hdr_len = sizeof(*gh)/4;
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200121 gh->type = type;
Harald Weltee779c362010-06-29 20:51:13 +0200122 gh->timeslot = ts;
123 gh->sub_slot = ss;
124 gh->arfcn = htons(arfcn);
125 gh->snr_db = snr;
126 gh->signal_dbm = signal_dbm;
127 gh->frame_number = htonl(fn);
128 gh->sub_type = chan_type;
129 gh->antenna_nr = 0;
130
131 dst = msgb_put(msg, len);
132 memcpy(dst, data, len);
133
Harald Weltee34a9402010-06-29 22:31:21 +0200134 return msg;
135}
136
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200137struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type,
138 uint8_t ss, uint32_t fn, int8_t signal_dbm,
139 uint8_t snr, const uint8_t *data, unsigned int len)
140{
141 return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type,
142 ss, fn, signal_dbm, snr, data, len);
143}
144
Harald Weltee4764422011-05-22 12:25:57 +0200145#ifdef HAVE_SYS_SOCKET_H
146
147#include <sys/socket.h>
148#include <netinet/in.h>
149
Harald Welte47379ca2011-08-17 16:35:24 +0200150/*! \brief Create a new (sending) GSMTAP source socket
151 * \param[in] host host name or IP address in string format
152 * \param[in] port UDP port number in host byte order
153 *
154 * Opens a GSMTAP source (sending) socket, conncet it to host/port and
155 * return resulting fd. If \a host is NULL, the destination address
156 * will be localhost. If \a port is 0, the default \ref
157 * GSMTAP_UDP_PORT will be used.
158 * */
Harald Welte33cb71a2011-05-21 18:54:32 +0200159int gsmtap_source_init_fd(const char *host, uint16_t port)
160{
161 if (port == 0)
162 port = GSMTAP_UDP_PORT;
163 if (host == NULL)
164 host = "localhost";
165
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200166 return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port,
167 OSMO_SOCK_F_CONNECT);
Harald Welte33cb71a2011-05-21 18:54:32 +0200168}
169
Harald Welte47379ca2011-08-17 16:35:24 +0200170/*! \brief Add a local sink to an existing GSMTAP source and return fd */
Harald Welte33cb71a2011-05-21 18:54:32 +0200171int gsmtap_source_add_sink_fd(int gsmtap_fd)
172{
173 struct sockaddr_storage ss;
174 socklen_t ss_len = sizeof(ss);
175 int rc;
176
177 rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len);
178 if (rc < 0)
179 return rc;
180
181 if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) {
Pablo Neira Ayuso0849c9a2011-06-09 15:04:30 +0200182 rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM,
183 IPPROTO_UDP, OSMO_SOCK_F_BIND);
Harald Welte33cb71a2011-05-21 18:54:32 +0200184 if (rc >= 0)
185 return rc;
186 }
187
188 return -ENODEV;
189}
190
Harald Welte47379ca2011-08-17 16:35:24 +0200191/*! \brief Send a \ref msgb through a GSMTAP source
192 * \param[in] gti GSMTAP instance
193 * \param[in] msgb message buffer
194 */
Harald Welte33cb71a2011-05-21 18:54:32 +0200195int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg)
196{
Harald Welte13692a62011-05-22 20:06:11 +0200197 if (!gti)
198 return -ENODEV;
199
Harald Welte33cb71a2011-05-21 18:54:32 +0200200 if (gti->ofd_wq_mode)
201 return osmo_wqueue_enqueue(&gti->wq, msg);
202 else {
203 /* try immediate send and return error if any */
204 int rc;
205
206 rc = write(gsmtap_inst_fd(gti), msg->data, msg->len);
207 if (rc <= 0) {
208 return rc;
209 } else if (rc >= msg->len) {
210 msgb_free(msg);
211 return 0;
212 } else {
213 /* short write */
214 return -EIO;
215 }
216 }
217}
218
Harald Welte47379ca2011-08-17 16:35:24 +0200219/*! \brief receive a message from L1/L2 and put it in GSMTAP */
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200220int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts,
Harald Welte33cb71a2011-05-21 18:54:32 +0200221 uint8_t chan_type, uint8_t ss, uint32_t fn,
222 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
223 unsigned int len)
Harald Weltee34a9402010-06-29 22:31:21 +0200224{
225 struct msgb *msg;
226
Harald Welte13692a62011-05-22 20:06:11 +0200227 if (!gti)
228 return -ENODEV;
229
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200230 msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm,
Harald Weltee34a9402010-06-29 22:31:21 +0200231 snr, data, len);
232 if (!msg)
233 return -ENOMEM;
234
Harald Welte33cb71a2011-05-21 18:54:32 +0200235 return gsmtap_sendmsg(gti, msg);
Harald Weltee779c362010-06-29 20:51:13 +0200236}
237
Sylvain Munaut15ae7152011-09-26 13:05:07 +0200238int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts,
239 uint8_t chan_type, uint8_t ss, uint32_t fn,
240 int8_t signal_dbm, uint8_t snr, const uint8_t *data,
241 unsigned int len)
242{
243 return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn,
244 signal_dbm, snr, data, len);
245}
246
Harald Weltee779c362010-06-29 20:51:13 +0200247/* Callback from select layer if we can write to the socket */
Harald Welte33cb71a2011-05-21 18:54:32 +0200248static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg)
Harald Weltee779c362010-06-29 20:51:13 +0200249{
Harald Weltee779c362010-06-29 20:51:13 +0200250 int rc;
251
Harald Welte33cb71a2011-05-21 18:54:32 +0200252 rc = write(ofd->fd, msg->data, msg->len);
Harald Weltee779c362010-06-29 20:51:13 +0200253 if (rc < 0) {
254 perror("writing msgb to gsmtap fd");
Harald Weltee779c362010-06-29 20:51:13 +0200255 return rc;
256 }
257 if (rc != msg->len) {
258 perror("short write to gsmtap fd");
Harald Weltee779c362010-06-29 20:51:13 +0200259 return -EIO;
260 }
261
Harald Weltee779c362010-06-29 20:51:13 +0200262 return 0;
263}
264
Harald Welted58ba462011-04-27 10:57:49 +0200265/* Callback from select layer if we can read from the sink socket */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200266static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags)
Harald Welted58ba462011-04-27 10:57:49 +0200267{
268 int rc;
269 uint8_t buf[4096];
270
271 if (!(flags & BSC_FD_READ))
272 return 0;
273
274 rc = read(fd->fd, buf, sizeof(buf));
275 if (rc < 0) {
276 perror("reading from gsmtap sink fd");
277 return rc;
278 }
279 /* simply discard any data arriving on the socket */
280
281 return 0;
282}
283
Harald Welte47379ca2011-08-17 16:35:24 +0200284/*! \brief Add a local sink to an existing GSMTAP source instance */
Harald Welte33cb71a2011-05-21 18:54:32 +0200285int gsmtap_source_add_sink(struct gsmtap_inst *gti)
Harald Welted58ba462011-04-27 10:57:49 +0200286{
Harald Welte33cb71a2011-05-21 18:54:32 +0200287 int fd;
Harald Welted58ba462011-04-27 10:57:49 +0200288
Harald Welte33cb71a2011-05-21 18:54:32 +0200289 fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti));
290 if (fd < 0)
291 return fd;
Harald Welted58ba462011-04-27 10:57:49 +0200292
Harald Welte33cb71a2011-05-21 18:54:32 +0200293 if (gti->ofd_wq_mode) {
294 struct osmo_fd *sink_ofd;
295
296 sink_ofd = &gti->sink_ofd;
297 sink_ofd->fd = fd;
298 sink_ofd->when = BSC_FD_READ;
299 sink_ofd->cb = gsmtap_sink_fd_cb;
300
301 osmo_fd_register(sink_ofd);
Harald Welted58ba462011-04-27 10:57:49 +0200302 }
303
Harald Welte33cb71a2011-05-21 18:54:32 +0200304 return fd;
305}
Harald Welted58ba462011-04-27 10:57:49 +0200306
Harald Welte47379ca2011-08-17 16:35:24 +0200307
308/*! \brief Open GSMTAP source socket, connect and register osmo_fd
309 * \param[in] host host name or IP address in string format
310 * \param[in] port UDP port number in host byte order
311 * \param[in] osmo_wq_mode Register \ref osmo_wqueue (1) or not (0)
312 *
313 * Open GSMTAP source (sending) socket, connect it to host/port,
314 * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue
315 * registration. This means it is like \ref gsmtap_init2 but integrated
316 * with libosmocore \ref select */
Harald Welte33cb71a2011-05-21 18:54:32 +0200317struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port,
318 int ofd_wq_mode)
319{
320 struct gsmtap_inst *gti;
321 int fd;
Harald Welted58ba462011-04-27 10:57:49 +0200322
Harald Welte33cb71a2011-05-21 18:54:32 +0200323 fd = gsmtap_source_init_fd(host, port);
324 if (fd < 0)
325 return NULL;
326
327 gti = talloc_zero(NULL, struct gsmtap_inst);
328 gti->ofd_wq_mode = ofd_wq_mode;
329 gti->wq.bfd.fd = fd;
330 gti->sink_ofd.fd = -1;
331
332 if (ofd_wq_mode) {
333 osmo_wqueue_init(&gti->wq, 64);
334 gti->wq.write_cb = &gsmtap_wq_w_cb;
335
336 osmo_fd_register(&gti->wq.bfd);
337 }
338
339 return gti;
Harald Welted58ba462011-04-27 10:57:49 +0200340}
341
Harald Weltee4764422011-05-22 12:25:57 +0200342#endif /* HAVE_SYS_SOCKET_H */