Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1 | /* GSMTAP support code in libmsomcore */ |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 2 | /* |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 3 | * (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 4 | * |
| 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 Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 25 | #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 Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 29 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 30 | #include <osmocom/core/select.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 31 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 32 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 33 | #include <osmocom/gsm/rsl.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 34 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 35 | #include <sys/types.h> |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 36 | |
| 37 | #include <arpa/inet.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <unistd.h> |
| 41 | #include <stdint.h> |
| 42 | #include <string.h> |
| 43 | #include <errno.h> |
| 44 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 45 | /*! \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 Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 56 | uint8_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 Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 91 | /*! \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 | */ |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 105 | struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
| 106 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
| 107 | uint8_t snr, const uint8_t *data, unsigned int len) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 108 | { |
| 109 | struct msgb *msg; |
| 110 | struct gsmtap_hdr *gh; |
| 111 | uint8_t *dst; |
| 112 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 113 | msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); |
| 114 | if (!msg) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 115 | return NULL; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 116 | |
| 117 | gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); |
| 118 | |
| 119 | gh->version = GSMTAP_VERSION; |
| 120 | gh->hdr_len = sizeof(*gh)/4; |
| 121 | gh->type = GSMTAP_TYPE_UM; |
| 122 | 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 Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 134 | return msg; |
| 135 | } |
| 136 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 137 | #ifdef HAVE_SYS_SOCKET_H |
| 138 | |
| 139 | #include <sys/socket.h> |
| 140 | #include <netinet/in.h> |
| 141 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 142 | /*! \brief Create a new (sending) GSMTAP source socket |
| 143 | * \param[in] host host name or IP address in string format |
| 144 | * \param[in] port UDP port number in host byte order |
| 145 | * |
| 146 | * Opens a GSMTAP source (sending) socket, conncet it to host/port and |
| 147 | * return resulting fd. If \a host is NULL, the destination address |
| 148 | * will be localhost. If \a port is 0, the default \ref |
| 149 | * GSMTAP_UDP_PORT will be used. |
| 150 | * */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 151 | int gsmtap_source_init_fd(const char *host, uint16_t port) |
| 152 | { |
| 153 | if (port == 0) |
| 154 | port = GSMTAP_UDP_PORT; |
| 155 | if (host == NULL) |
| 156 | host = "localhost"; |
| 157 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 158 | return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, |
| 159 | OSMO_SOCK_F_CONNECT); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 162 | /*! \brief Add a local sink to an existing GSMTAP source and return fd */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 163 | int gsmtap_source_add_sink_fd(int gsmtap_fd) |
| 164 | { |
| 165 | struct sockaddr_storage ss; |
| 166 | socklen_t ss_len = sizeof(ss); |
| 167 | int rc; |
| 168 | |
| 169 | rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len); |
| 170 | if (rc < 0) |
| 171 | return rc; |
| 172 | |
| 173 | if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) { |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 174 | rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, |
| 175 | IPPROTO_UDP, OSMO_SOCK_F_BIND); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 176 | if (rc >= 0) |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | return -ENODEV; |
| 181 | } |
| 182 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 183 | /*! \brief Send a \ref msgb through a GSMTAP source |
| 184 | * \param[in] gti GSMTAP instance |
| 185 | * \param[in] msgb message buffer |
| 186 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 187 | int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg) |
| 188 | { |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 189 | if (!gti) |
| 190 | return -ENODEV; |
| 191 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 192 | if (gti->ofd_wq_mode) |
| 193 | return osmo_wqueue_enqueue(>i->wq, msg); |
| 194 | else { |
| 195 | /* try immediate send and return error if any */ |
| 196 | int rc; |
| 197 | |
| 198 | rc = write(gsmtap_inst_fd(gti), msg->data, msg->len); |
| 199 | if (rc <= 0) { |
| 200 | return rc; |
| 201 | } else if (rc >= msg->len) { |
| 202 | msgb_free(msg); |
| 203 | return 0; |
| 204 | } else { |
| 205 | /* short write */ |
| 206 | return -EIO; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 211 | /*! \brief receive a message from L1/L2 and put it in GSMTAP */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 212 | int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, |
| 213 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
| 214 | int8_t signal_dbm, uint8_t snr, const uint8_t *data, |
| 215 | unsigned int len) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 216 | { |
| 217 | struct msgb *msg; |
| 218 | |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 219 | if (!gti) |
| 220 | return -ENODEV; |
| 221 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 222 | msg = gsmtap_makemsg(arfcn, ts, chan_type, ss, fn, signal_dbm, |
| 223 | snr, data, len); |
| 224 | if (!msg) |
| 225 | return -ENOMEM; |
| 226 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 227 | return gsmtap_sendmsg(gti, msg); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* Callback from select layer if we can write to the socket */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 231 | static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 232 | { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 233 | int rc; |
| 234 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 235 | rc = write(ofd->fd, msg->data, msg->len); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 236 | if (rc < 0) { |
| 237 | perror("writing msgb to gsmtap fd"); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 238 | return rc; |
| 239 | } |
| 240 | if (rc != msg->len) { |
| 241 | perror("short write to gsmtap fd"); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 242 | return -EIO; |
| 243 | } |
| 244 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 248 | /* Callback from select layer if we can read from the sink socket */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 249 | static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 250 | { |
| 251 | int rc; |
| 252 | uint8_t buf[4096]; |
| 253 | |
| 254 | if (!(flags & BSC_FD_READ)) |
| 255 | return 0; |
| 256 | |
| 257 | rc = read(fd->fd, buf, sizeof(buf)); |
| 258 | if (rc < 0) { |
| 259 | perror("reading from gsmtap sink fd"); |
| 260 | return rc; |
| 261 | } |
| 262 | /* simply discard any data arriving on the socket */ |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 267 | /*! \brief Add a local sink to an existing GSMTAP source instance */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 268 | int gsmtap_source_add_sink(struct gsmtap_inst *gti) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 269 | { |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 270 | int fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 271 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 272 | fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti)); |
| 273 | if (fd < 0) |
| 274 | return fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 275 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 276 | if (gti->ofd_wq_mode) { |
| 277 | struct osmo_fd *sink_ofd; |
| 278 | |
| 279 | sink_ofd = >i->sink_ofd; |
| 280 | sink_ofd->fd = fd; |
| 281 | sink_ofd->when = BSC_FD_READ; |
| 282 | sink_ofd->cb = gsmtap_sink_fd_cb; |
| 283 | |
| 284 | osmo_fd_register(sink_ofd); |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 287 | return fd; |
| 288 | } |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 289 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 290 | |
| 291 | /*! \brief Open GSMTAP source socket, connect and register osmo_fd |
| 292 | * \param[in] host host name or IP address in string format |
| 293 | * \param[in] port UDP port number in host byte order |
| 294 | * \param[in] osmo_wq_mode Register \ref osmo_wqueue (1) or not (0) |
| 295 | * |
| 296 | * Open GSMTAP source (sending) socket, connect it to host/port, |
| 297 | * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue |
| 298 | * registration. This means it is like \ref gsmtap_init2 but integrated |
| 299 | * with libosmocore \ref select */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 300 | struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port, |
| 301 | int ofd_wq_mode) |
| 302 | { |
| 303 | struct gsmtap_inst *gti; |
| 304 | int fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 305 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 306 | fd = gsmtap_source_init_fd(host, port); |
| 307 | if (fd < 0) |
| 308 | return NULL; |
| 309 | |
| 310 | gti = talloc_zero(NULL, struct gsmtap_inst); |
| 311 | gti->ofd_wq_mode = ofd_wq_mode; |
| 312 | gti->wq.bfd.fd = fd; |
| 313 | gti->sink_ofd.fd = -1; |
| 314 | |
| 315 | if (ofd_wq_mode) { |
| 316 | osmo_wqueue_init(>i->wq, 64); |
| 317 | gti->wq.write_cb = &gsmtap_wq_w_cb; |
| 318 | |
| 319 | osmo_fd_register(>i->wq.bfd); |
| 320 | } |
| 321 | |
| 322 | return gti; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 325 | #endif /* HAVE_SYS_SOCKET_H */ |