Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gsmtap_util.c |
| 2 | * GSMTAP support code in libosmocore. */ |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 3 | /* |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 4 | * (C) 2010-2017 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include "../config.h" |
| 27 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 28 | #include <osmocom/core/gsmtap_util.h> |
| 29 | #include <osmocom/core/logging.h> |
| 30 | #include <osmocom/core/gsmtap.h> |
| 31 | #include <osmocom/core/msgb.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 33 | #include <osmocom/core/select.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 34 | #include <osmocom/core/socket.h> |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 35 | #include <osmocom/core/byteswap.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 37 | #include <osmocom/gsm/rsl.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 38 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 39 | #include <sys/types.h> |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 40 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <unistd.h> |
| 43 | #include <stdint.h> |
| 44 | #include <string.h> |
| 45 | #include <errno.h> |
| 46 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 47 | /*! \addtogroup gsmtap |
| 48 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 49 | * GSMTAP utility routines. Encapsulates GSM messages over UDP. |
| 50 | * |
| 51 | * \file gsmtap_util.c */ |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 52 | |
| 53 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! convert RSL channel number to GSMTAP channel type |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 55 | * \param[in] rsl_chantype RSL channel type |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 56 | * \param[in] link_id RSL link identifier |
| 57 | * \returns GSMTAP channel type |
| 58 | */ |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 59 | uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) |
| 60 | { |
| 61 | uint8_t ret = GSMTAP_CHANNEL_UNKNOWN; |
| 62 | |
| 63 | switch (rsl_chantype) { |
| 64 | case RSL_CHAN_Bm_ACCHs: |
| 65 | ret = GSMTAP_CHANNEL_TCH_F; |
| 66 | break; |
| 67 | case RSL_CHAN_Lm_ACCHs: |
| 68 | ret = GSMTAP_CHANNEL_TCH_H; |
| 69 | break; |
| 70 | case RSL_CHAN_SDCCH4_ACCH: |
| 71 | ret = GSMTAP_CHANNEL_SDCCH4; |
| 72 | break; |
| 73 | case RSL_CHAN_SDCCH8_ACCH: |
| 74 | ret = GSMTAP_CHANNEL_SDCCH8; |
| 75 | break; |
| 76 | case RSL_CHAN_BCCH: |
| 77 | ret = GSMTAP_CHANNEL_BCCH; |
| 78 | break; |
| 79 | case RSL_CHAN_RACH: |
| 80 | ret = GSMTAP_CHANNEL_RACH; |
| 81 | break; |
| 82 | case RSL_CHAN_PCH_AGCH: |
| 83 | /* it could also be AGCH... */ |
| 84 | ret = GSMTAP_CHANNEL_PCH; |
| 85 | break; |
Harald Welte | 3b7cd0b | 2017-07-27 14:12:15 +0200 | [diff] [blame] | 86 | case RSL_CHAN_OSMO_PDCH: |
| 87 | ret = GSMTAP_CHANNEL_PDCH; |
| 88 | break; |
Harald Welte | ac7fabe | 2020-02-26 17:24:02 +0100 | [diff] [blame] | 89 | case RSL_CHAN_OSMO_CBCH4: |
| 90 | ret = GSMTAP_CHANNEL_CBCH51; |
| 91 | break; |
| 92 | case RSL_CHAN_OSMO_CBCH8: |
| 93 | ret = GSMTAP_CHANNEL_CBCH52; |
| 94 | break; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (link_id & 0x40) |
| 98 | ret |= GSMTAP_CHANNEL_ACCH; |
| 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 103 | /*! convert GSMTAP channel type to RSL channel number + Link ID |
| 104 | * \param[in] gsmtap_chantype GSMTAP channel type |
| 105 | * \param[out] rsl_chantype RSL channel mumber |
| 106 | * \param[out] link_id RSL link identifier |
| 107 | */ |
| 108 | void chantype_gsmtap2rsl(uint8_t gsmtap_chantype, uint8_t *rsl_chantype, |
| 109 | uint8_t *link_id) |
| 110 | { |
| 111 | switch (gsmtap_chantype & ~GSMTAP_CHANNEL_ACCH & 0xff) { |
| 112 | case GSMTAP_CHANNEL_TCH_F: // TCH/F, FACCH/F |
| 113 | *rsl_chantype = RSL_CHAN_Bm_ACCHs; |
| 114 | break; |
| 115 | case GSMTAP_CHANNEL_TCH_H: // TCH/H, FACCH/H |
| 116 | *rsl_chantype = RSL_CHAN_Lm_ACCHs; |
| 117 | break; |
| 118 | case GSMTAP_CHANNEL_SDCCH4: // SDCCH/4 |
| 119 | *rsl_chantype = RSL_CHAN_SDCCH4_ACCH; |
| 120 | break; |
| 121 | case GSMTAP_CHANNEL_SDCCH8: // SDCCH/8 |
| 122 | *rsl_chantype = RSL_CHAN_SDCCH8_ACCH; |
| 123 | break; |
| 124 | case GSMTAP_CHANNEL_BCCH: // BCCH |
| 125 | *rsl_chantype = RSL_CHAN_BCCH; |
| 126 | break; |
| 127 | case GSMTAP_CHANNEL_RACH: // RACH |
| 128 | *rsl_chantype = RSL_CHAN_RACH; |
| 129 | break; |
| 130 | case GSMTAP_CHANNEL_PCH: // PCH |
| 131 | case GSMTAP_CHANNEL_AGCH: // AGCH |
| 132 | *rsl_chantype = RSL_CHAN_PCH_AGCH; |
| 133 | break; |
| 134 | case GSMTAP_CHANNEL_PDCH: |
Harald Welte | 3b7cd0b | 2017-07-27 14:12:15 +0200 | [diff] [blame] | 135 | *rsl_chantype = RSL_CHAN_OSMO_PDCH; |
Harald Welte | 93713a5 | 2017-07-12 23:43:40 +0200 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | |
| 139 | *link_id = gsmtap_chantype & GSMTAP_CHANNEL_ACCH ? 0x40 : 0x00; |
| 140 | } |
| 141 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 142 | /*! create an arbitrary type GSMTAP message |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 143 | * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 144 | * \param[in] arfcn GSM ARFCN (Channel Number) |
| 145 | * \param[in] ts GSM time slot |
| 146 | * \param[in] chan_type Channel Type |
| 147 | * \param[in] ss Sub-slot |
| 148 | * \param[in] fn GSM Frame Number |
| 149 | * \param[in] signal_dbm Signal Strength (dBm) |
| 150 | * \param[in] snr Signal/Noise Ratio (SNR) |
| 151 | * \param[in] data Pointer to data buffer |
| 152 | * \param[in] len Length of \ref data |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 153 | * \return dynamically allocated message buffer containing data |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 154 | * |
| 155 | * This function will allocate a new msgb and fill it with a GSMTAP |
| 156 | * header containing the information |
| 157 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 158 | struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 159 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
| 160 | uint8_t snr, const uint8_t *data, unsigned int len) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 161 | { |
| 162 | struct msgb *msg; |
| 163 | struct gsmtap_hdr *gh; |
| 164 | uint8_t *dst; |
| 165 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 166 | msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); |
| 167 | if (!msg) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 168 | return NULL; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 169 | |
| 170 | gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); |
| 171 | |
| 172 | gh->version = GSMTAP_VERSION; |
| 173 | gh->hdr_len = sizeof(*gh)/4; |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 174 | gh->type = type; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 175 | gh->timeslot = ts; |
| 176 | gh->sub_slot = ss; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 177 | gh->arfcn = osmo_htons(arfcn); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 178 | gh->snr_db = snr; |
| 179 | gh->signal_dbm = signal_dbm; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 180 | gh->frame_number = osmo_htonl(fn); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 181 | gh->sub_type = chan_type; |
| 182 | gh->antenna_nr = 0; |
| 183 | |
| 184 | dst = msgb_put(msg, len); |
| 185 | memcpy(dst, data, len); |
| 186 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 187 | return msg; |
| 188 | } |
| 189 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 190 | /*! create L1/L2 data and put it into GSMTAP |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 191 | * \param[in] arfcn GSM ARFCN (Channel Number) |
| 192 | * \param[in] ts GSM time slot |
| 193 | * \param[in] chan_type Channel Type |
| 194 | * \param[in] ss Sub-slot |
| 195 | * \param[in] fn GSM Frame Number |
| 196 | * \param[in] signal_dbm Signal Strength (dBm) |
| 197 | * \param[in] snr Signal/Noise Ratio (SNR) |
| 198 | * \param[in] data Pointer to data buffer |
| 199 | * \param[in] len Length of \ref data |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 200 | * \return message buffer or NULL in case of error |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 201 | * |
| 202 | * This function will allocate a new msgb and fill it with a GSMTAP |
| 203 | * header containing the information |
| 204 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 205 | struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
| 206 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
| 207 | uint8_t snr, const uint8_t *data, unsigned int len) |
| 208 | { |
| 209 | return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type, |
| 210 | ss, fn, signal_dbm, snr, data, len); |
| 211 | } |
| 212 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 213 | #ifdef HAVE_SYS_SOCKET_H |
| 214 | |
| 215 | #include <sys/socket.h> |
| 216 | #include <netinet/in.h> |
| 217 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 218 | /*! Create a new (sending) GSMTAP source socket |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 219 | * \param[in] host host name or IP address in string format |
| 220 | * \param[in] port UDP port number in host byte order |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 221 | * \return file descriptor of the new socket |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 222 | * |
| 223 | * Opens a GSMTAP source (sending) socket, conncet it to host/port and |
| 224 | * return resulting fd. If \a host is NULL, the destination address |
| 225 | * will be localhost. If \a port is 0, the default \ref |
| 226 | * GSMTAP_UDP_PORT will be used. |
| 227 | * */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 228 | int gsmtap_source_init_fd(const char *host, uint16_t port) |
| 229 | { |
| 230 | if (port == 0) |
| 231 | port = GSMTAP_UDP_PORT; |
| 232 | if (host == NULL) |
| 233 | host = "localhost"; |
| 234 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 235 | return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, |
| 236 | OSMO_SOCK_F_CONNECT); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 239 | /*! Add a local sink to an existing GSMTAP source and return fd |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 240 | * \param[in] gsmtap_fd file descriptor of the gsmtap socket |
| 241 | * \returns file descriptor of locally bound receive socket |
| 242 | * |
| 243 | * In case the GSMTAP socket is connected to a local destination |
| 244 | * IP/port, this function creates a corresponding receiving socket |
| 245 | * bound to that destination IP + port. |
| 246 | * |
| 247 | * In case the gsmtap socket is not connected to a local IP/port, or |
| 248 | * creation of the receiving socket fails, a negative error code is |
| 249 | * returned. |
| 250 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 251 | int gsmtap_source_add_sink_fd(int gsmtap_fd) |
| 252 | { |
| 253 | struct sockaddr_storage ss; |
| 254 | socklen_t ss_len = sizeof(ss); |
| 255 | int rc; |
| 256 | |
| 257 | rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len); |
| 258 | if (rc < 0) |
| 259 | return rc; |
| 260 | |
| 261 | if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) { |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 262 | rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, |
Philipp Maier | b8a9162 | 2018-08-23 20:18:55 +0200 | [diff] [blame] | 263 | IPPROTO_UDP, |
| 264 | OSMO_SOCK_F_BIND | |
| 265 | OSMO_SOCK_F_UDP_REUSEADDR); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 266 | if (rc >= 0) |
| 267 | return rc; |
| 268 | } |
| 269 | |
| 270 | return -ENODEV; |
| 271 | } |
| 272 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 273 | /*! Send a \ref msgb through a GSMTAP source |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 274 | * \param[in] gti GSMTAP instance |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 275 | * \param[in] msg message buffer |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 276 | * \return 0 in case of success; negative in case of error |
Neels Hofmeyr | c9a4ce6 | 2018-02-16 01:23:29 +0100 | [diff] [blame] | 277 | * NOTE: in case of nonzero return value, the *caller* must free the msg! |
| 278 | * (This enables the caller to attempt re-sending the message.) |
| 279 | * If 0 is returned, the msgb was freed by this function. |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 280 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 281 | int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg) |
| 282 | { |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 283 | if (!gti) |
| 284 | return -ENODEV; |
| 285 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 286 | if (gti->ofd_wq_mode) |
| 287 | return osmo_wqueue_enqueue(>i->wq, msg); |
| 288 | else { |
| 289 | /* try immediate send and return error if any */ |
| 290 | int rc; |
| 291 | |
| 292 | rc = write(gsmtap_inst_fd(gti), msg->data, msg->len); |
Neels Hofmeyr | 90539ac | 2018-02-16 01:24:03 +0100 | [diff] [blame] | 293 | if (rc < 0) { |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 294 | return rc; |
| 295 | } else if (rc >= msg->len) { |
| 296 | msgb_free(msg); |
| 297 | return 0; |
| 298 | } else { |
| 299 | /* short write */ |
| 300 | return -EIO; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 305 | /*! send an arbitrary type through GSMTAP. |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 306 | * See \ref gsmtap_makemsg_ex for arguments |
| 307 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 308 | int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts, |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 309 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
| 310 | int8_t signal_dbm, uint8_t snr, const uint8_t *data, |
| 311 | unsigned int len) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 312 | { |
| 313 | struct msgb *msg; |
Neels Hofmeyr | a4952aa | 2018-02-16 01:26:00 +0100 | [diff] [blame] | 314 | int rc; |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 315 | |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 316 | if (!gti) |
| 317 | return -ENODEV; |
| 318 | |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 319 | msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm, |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 320 | snr, data, len); |
| 321 | if (!msg) |
| 322 | return -ENOMEM; |
| 323 | |
Neels Hofmeyr | a4952aa | 2018-02-16 01:26:00 +0100 | [diff] [blame] | 324 | rc = gsmtap_sendmsg(gti, msg); |
| 325 | if (rc) |
| 326 | msgb_free(msg); |
| 327 | return rc; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 330 | /*! send a message from L1/L2 through GSMTAP. |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 331 | * See \ref gsmtap_makemsg for arguments |
| 332 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 333 | int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, |
| 334 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
| 335 | int8_t signal_dbm, uint8_t snr, const uint8_t *data, |
| 336 | unsigned int len) |
| 337 | { |
| 338 | return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn, |
| 339 | signal_dbm, snr, data, len); |
| 340 | } |
| 341 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 342 | /* Callback from select layer if we can write to the socket */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 343 | 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] | 344 | { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 345 | int rc; |
| 346 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 347 | rc = write(ofd->fd, msg->data, msg->len); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 348 | if (rc < 0) { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 349 | return rc; |
| 350 | } |
| 351 | if (rc != msg->len) { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 352 | return -EIO; |
| 353 | } |
| 354 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 358 | /* 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] | 359 | 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] | 360 | { |
| 361 | int rc; |
| 362 | uint8_t buf[4096]; |
| 363 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 364 | if (!(flags & OSMO_FD_READ)) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 365 | return 0; |
| 366 | |
| 367 | rc = read(fd->fd, buf, sizeof(buf)); |
| 368 | if (rc < 0) { |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 369 | return rc; |
| 370 | } |
| 371 | /* simply discard any data arriving on the socket */ |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 376 | /*! Add a local sink to an existing GSMTAP source and return fd |
Vadim Yanitskiy | 2f65bb1 | 2019-03-25 15:57:09 +0700 | [diff] [blame] | 377 | * \param[in] gti existing GSMTAP source |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 378 | * \returns file descriptor of locally bound receive socket |
| 379 | * |
| 380 | * In case the GSMTAP socket is connected to a local destination |
| 381 | * IP/port, this function creates a corresponding receiving socket |
| 382 | * bound to that destination IP + port. |
| 383 | * |
| 384 | * In case the gsmtap socket is not connected to a local IP/port, or |
| 385 | * creation of the receiving socket fails, a negative error code is |
| 386 | * returned. |
| 387 | * |
| 388 | * The file descriptor of the receiving socket is automatically added |
| 389 | * to the libosmocore select() handling. |
| 390 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 391 | int gsmtap_source_add_sink(struct gsmtap_inst *gti) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 392 | { |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 393 | int fd, rc; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 394 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 395 | fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti)); |
| 396 | if (fd < 0) |
| 397 | return fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 398 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 399 | if (gti->ofd_wq_mode) { |
| 400 | struct osmo_fd *sink_ofd; |
| 401 | |
| 402 | sink_ofd = >i->sink_ofd; |
| 403 | sink_ofd->fd = fd; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 404 | sink_ofd->when = OSMO_FD_READ; |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 405 | sink_ofd->cb = gsmtap_sink_fd_cb; |
| 406 | |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 407 | rc = osmo_fd_register(sink_ofd); |
| 408 | if (rc < 0) { |
| 409 | close(fd); |
| 410 | return rc; |
| 411 | } |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 414 | return fd; |
| 415 | } |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 416 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 417 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 418 | /*! Open GSMTAP source socket, connect and register osmo_fd |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 419 | * \param[in] host host name or IP address in string format |
| 420 | * \param[in] port UDP port number in host byte order |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 421 | * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 422 | * \return callee-allocated \ref gsmtap_inst |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 423 | * |
| 424 | * Open GSMTAP source (sending) socket, connect it to host/port, |
| 425 | * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue |
Vadim Yanitskiy | 2f65bb1 | 2019-03-25 15:57:09 +0700 | [diff] [blame] | 426 | * registration. |
| 427 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 428 | struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port, |
| 429 | int ofd_wq_mode) |
| 430 | { |
| 431 | struct gsmtap_inst *gti; |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 432 | int fd, rc; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 433 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 434 | fd = gsmtap_source_init_fd(host, port); |
| 435 | if (fd < 0) |
| 436 | return NULL; |
| 437 | |
| 438 | gti = talloc_zero(NULL, struct gsmtap_inst); |
| 439 | gti->ofd_wq_mode = ofd_wq_mode; |
| 440 | gti->wq.bfd.fd = fd; |
| 441 | gti->sink_ofd.fd = -1; |
| 442 | |
| 443 | if (ofd_wq_mode) { |
| 444 | osmo_wqueue_init(>i->wq, 64); |
| 445 | gti->wq.write_cb = &gsmtap_wq_w_cb; |
| 446 | |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 447 | rc = osmo_fd_register(>i->wq.bfd); |
| 448 | if (rc < 0) { |
Vadim Yanitskiy | b9baf02 | 2019-03-24 00:01:27 +0700 | [diff] [blame] | 449 | talloc_free(gti); |
Harald Welte | 9d862c8 | 2016-11-26 00:10:07 +0100 | [diff] [blame] | 450 | close(fd); |
| 451 | return NULL; |
| 452 | } |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | return gti; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 458 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 459 | |
Harald Welte | aa3ba46 | 2017-07-13 00:01:02 +0200 | [diff] [blame] | 460 | const struct value_string gsmtap_gsm_channel_names[] = { |
| 461 | { GSMTAP_CHANNEL_UNKNOWN, "UNKNOWN" }, |
| 462 | { GSMTAP_CHANNEL_BCCH, "BCCH" }, |
| 463 | { GSMTAP_CHANNEL_CCCH, "CCCH" }, |
| 464 | { GSMTAP_CHANNEL_RACH, "RACH" }, |
| 465 | { GSMTAP_CHANNEL_AGCH, "AGCH" }, |
| 466 | { GSMTAP_CHANNEL_PCH, "PCH" }, |
| 467 | { GSMTAP_CHANNEL_SDCCH, "SDCCH" }, |
| 468 | { GSMTAP_CHANNEL_SDCCH4, "SDCCH/4" }, |
| 469 | { GSMTAP_CHANNEL_SDCCH8, "SDCCH/8" }, |
| 470 | { GSMTAP_CHANNEL_TCH_F, "TCH/F/FACCH/F" }, |
| 471 | { GSMTAP_CHANNEL_TCH_H, "TCH/H/FACCH/H" }, |
| 472 | { GSMTAP_CHANNEL_PACCH, "PACCH" }, |
| 473 | { GSMTAP_CHANNEL_CBCH52, "CBCH" }, |
| 474 | { GSMTAP_CHANNEL_PDCH, "PDCH" } , |
| 475 | { GSMTAP_CHANNEL_PTCCH, "PTTCH" }, |
| 476 | { GSMTAP_CHANNEL_CBCH51, "CBCH" }, |
| 477 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH, "LSACCH" }, |
| 478 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH4, "SACCH/4" }, |
| 479 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_SDCCH8, "SACCH/8" }, |
| 480 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_TCH_F, "SACCH/F" }, |
| 481 | { GSMTAP_CHANNEL_ACCH | GSMTAP_CHANNEL_TCH_H, "SACCH/H" }, |
Harald Welte | 20618ca | 2020-03-07 16:40:36 +0100 | [diff] [blame^] | 482 | { GSMTAP_CHANNEL_VOICE, "VOICE" }, |
Harald Welte | aa3ba46 | 2017-07-13 00:01:02 +0200 | [diff] [blame] | 483 | { 0, NULL } |
| 484 | }; |
| 485 | |
| 486 | /* for debugging */ |
| 487 | const struct value_string gsmtap_type_names[] = { |
| 488 | { GSMTAP_TYPE_UM, "GSM Um (MS<->BTS)" }, |
| 489 | { GSMTAP_TYPE_ABIS, "GSM Abis (BTS<->BSC)" }, |
| 490 | { GSMTAP_TYPE_UM_BURST, "GSM Um burst (MS<->BTS)" }, |
| 491 | { GSMTAP_TYPE_SIM, "SIM Card" }, |
| 492 | { GSMTAP_TYPE_TETRA_I1, "TETRA V+D" }, |
| 493 | { GSMTAP_TYPE_TETRA_I1_BURST, "TETRA bursts" }, |
| 494 | { GSMTAP_TYPE_WMX_BURST, "WiMAX burst" }, |
| 495 | { GSMTAP_TYPE_GMR1_UM, "GMR-1 air interfeace (MES-MS<->GTS)"}, |
| 496 | { GSMTAP_TYPE_UMTS_RLC_MAC, "UMTS RLC/MAC" }, |
| 497 | { GSMTAP_TYPE_UMTS_RRC, "UMTS RRC" }, |
| 498 | { GSMTAP_TYPE_LTE_RRC, "LTE RRC" }, |
| 499 | { GSMTAP_TYPE_LTE_MAC, "LTE MAC" }, |
| 500 | { GSMTAP_TYPE_LTE_MAC_FRAMED, "LTE MAC with context hdr" }, |
| 501 | { GSMTAP_TYPE_OSMOCORE_LOG, "libosmocore logging" }, |
| 502 | { GSMTAP_TYPE_QC_DIAG, "Qualcomm DIAG" }, |
| 503 | { 0, NULL } |
| 504 | }; |
| 505 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 506 | /*! @} */ |