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