Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis interface to E1 */ |
| 2 | |
| 3 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
Harald Welte | 323d39d | 2017-11-13 01:09:21 +0900 | [diff] [blame] | 7 | * SPDX-License-Identifier: AGPL-3.0+ |
| 8 | * |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "internal.h" |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 25 | #include "../config.h" |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <unistd.h> |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 29 | #include <inttypes.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #include <errno.h> |
| 32 | #include <string.h> |
| 33 | #include <time.h> |
Holger Hans Peter Freyther | 2547458 | 2017-01-23 19:49:07 +0100 | [diff] [blame] | 34 | #include <fcntl.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 35 | #include <sys/socket.h> |
| 36 | #include <sys/ioctl.h> |
| 37 | #include <arpa/inet.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 38 | |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 39 | #include <osmocom/abis/lapd.h> |
| 40 | |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 41 | #include <osmocom/core/linuxlist.h> |
| 42 | #include <osmocom/core/talloc.h> |
| 43 | #include <osmocom/core/rate_ctr.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 44 | #include <osmocom/core/logging.h> |
| 45 | #include <osmocom/core/signal.h> |
Neels Hofmeyr | a160e4b | 2018-11-16 00:16:57 +0100 | [diff] [blame] | 46 | #include <osmocom/core/endian.h> |
Pablo Neira Ayuso | 177094b | 2011-06-07 12:21:51 +0200 | [diff] [blame] | 47 | #include <osmocom/abis/e1_input.h> |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 48 | |
| 49 | #define NUM_E1_TS 32 |
| 50 | |
| 51 | static void *tall_e1inp_ctx; |
| 52 | |
| 53 | /* list of all E1 drivers */ |
| 54 | LLIST_HEAD(e1inp_driver_list); |
| 55 | |
| 56 | /* list of all E1 lines */ |
| 57 | LLIST_HEAD(e1inp_line_list); |
| 58 | |
| 59 | static void *tall_sigl_ctx; |
| 60 | |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 61 | static const struct rate_ctr_desc e1inp_ctr_d[] = { |
| 62 | [E1I_CTR_HDLC_ABORT] = { |
Pau Espin Pedrol | a210684 | 2018-07-24 15:00:11 +0200 | [diff] [blame] | 63 | "hdlc:abort", "HDLC abort" |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 64 | }, |
| 65 | [E1I_CTR_HDLC_BADFCS] = { |
Pau Espin Pedrol | a210684 | 2018-07-24 15:00:11 +0200 | [diff] [blame] | 66 | "hdlc:bad_fcs", "HLDC Bad FCS" |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 67 | }, |
| 68 | [E1I_CTR_HDLC_OVERR] = { |
Pau Espin Pedrol | a210684 | 2018-07-24 15:00:11 +0200 | [diff] [blame] | 69 | "hdlc:overrun", "HDLC Overrun" |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 70 | }, |
| 71 | [E1I_CTR_ALARM] = { |
Harald Welte | dd0c2ef | 2011-08-11 12:54:07 +0200 | [diff] [blame] | 72 | "alarm", "Alarm" |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 73 | }, |
| 74 | [E1I_CTR_REMOVED] = { |
Harald Welte | dd0c2ef | 2011-08-11 12:54:07 +0200 | [diff] [blame] | 75 | "removed", "Line removed" |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 76 | }, |
| 77 | }; |
| 78 | |
| 79 | static const struct rate_ctr_group_desc e1inp_ctr_g_d = { |
| 80 | .group_name_prefix = "e1inp", |
| 81 | .group_description = "E1 Input subsystem", |
| 82 | .num_ctr = ARRAY_SIZE(e1inp_ctr_d), |
| 83 | .ctr_desc = e1inp_ctr_d, |
| 84 | }; |
| 85 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 86 | /* |
| 87 | * pcap writing of the misdn load |
| 88 | * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat |
| 89 | */ |
| 90 | #define DLT_LINUX_LAPD 177 |
| 91 | #define PCAP_INPUT 0 |
| 92 | #define PCAP_OUTPUT 1 |
| 93 | |
| 94 | struct pcap_hdr { |
| 95 | uint32_t magic_number; |
| 96 | uint16_t version_major; |
| 97 | uint16_t version_minor; |
| 98 | int32_t thiszone; |
| 99 | uint32_t sigfigs; |
| 100 | uint32_t snaplen; |
| 101 | uint32_t network; |
| 102 | } __attribute__((packed)); |
| 103 | |
| 104 | struct pcaprec_hdr { |
| 105 | uint32_t ts_sec; |
| 106 | uint32_t ts_usec; |
| 107 | uint32_t incl_len; |
| 108 | uint32_t orig_len; |
| 109 | } __attribute__((packed)); |
| 110 | |
| 111 | struct fake_linux_lapd_header { |
| 112 | uint16_t pkttype; |
| 113 | uint16_t hatype; |
| 114 | uint16_t halen; |
| 115 | uint64_t addr; |
| 116 | int16_t protocol; |
| 117 | } __attribute__((packed)); |
| 118 | |
| 119 | struct lapd_header { |
Neels Hofmeyr | a160e4b | 2018-11-16 00:16:57 +0100 | [diff] [blame] | 120 | #if OSMO_IS_LITTLE_ENDIAN |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 121 | uint8_t ea1 : 1; |
| 122 | uint8_t cr : 1; |
| 123 | uint8_t sapi : 6; |
| 124 | uint8_t ea2 : 1; |
| 125 | uint8_t tei : 7; |
| 126 | uint8_t control_foo; /* fake UM's ... */ |
Neels Hofmeyr | a160e4b | 2018-11-16 00:16:57 +0100 | [diff] [blame] | 127 | #elif OSMO_IS_BIG_ENDIAN |
| 128 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 129 | uint8_t sapi:6, cr:1, ea1:1; |
| 130 | uint8_t tei:7, ea2:1; |
| 131 | uint8_t control_foo; |
| 132 | #endif |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 133 | } __attribute__((packed)); |
| 134 | |
| 135 | osmo_static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset); |
| 136 | osmo_static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset); |
| 137 | osmo_static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset); |
| 138 | osmo_static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset); |
| 139 | osmo_static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size); |
| 140 | |
| 141 | |
| 142 | static int pcap_fd = -1; |
| 143 | |
Pablo Neira Ayuso | 2e11f5c | 2013-07-05 15:08:18 +0200 | [diff] [blame] | 144 | int e1_set_pcap_fd(int fd) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 145 | { |
Sylvain Munaut | 4b45e9d | 2020-05-08 09:40:44 +0200 | [diff] [blame] | 146 | const struct pcap_hdr header = { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 147 | .magic_number = 0xa1b2c3d4, |
| 148 | .version_major = 2, |
| 149 | .version_minor = 4, |
| 150 | .thiszone = 0, |
| 151 | .sigfigs = 0, |
| 152 | .snaplen = 65535, |
| 153 | .network = DLT_LINUX_LAPD, |
| 154 | }; |
Sylvain Munaut | 4b45e9d | 2020-05-08 09:40:44 +0200 | [diff] [blame] | 155 | struct e1inp_line *line; |
| 156 | int i; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 157 | |
Sylvain Munaut | 4b45e9d | 2020-05-08 09:40:44 +0200 | [diff] [blame] | 158 | /* write header */ |
| 159 | if (fd >= 0) { |
| 160 | int rc = write(fd, &header, sizeof(header)); |
| 161 | if (rc < 0) |
| 162 | return rc; |
| 163 | } |
| 164 | |
| 165 | /* update fd in all lines in our global list of e1 lines */ |
| 166 | llist_for_each_entry(line, &e1inp_line_list, list) { |
| 167 | /* Set the PCAP file descriptor for all timeslots that have |
| 168 | * software LAPD instances, to ensure the osmo_lapd_pcap code is |
| 169 | * used to write PCAP files (if requested) */ |
| 170 | for (i = 0; i < ARRAY_SIZE(line->ts); i++) { |
| 171 | struct e1inp_ts *e1i_ts = &line->ts[i]; |
| 172 | if (e1i_ts->lapd) |
| 173 | e1i_ts->lapd->pcap_fd = fd; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* close previous and update global */ |
| 178 | if (pcap_fd >= 0) |
| 179 | close(pcap_fd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 180 | pcap_fd = fd; |
Sylvain Munaut | 4b45e9d | 2020-05-08 09:40:44 +0200 | [diff] [blame] | 181 | |
| 182 | return 0; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* This currently only works for the D-Channel */ |
| 186 | static void write_pcap_packet(int direction, int sapi, int tei, |
| 187 | struct msgb *msg) { |
| 188 | if (pcap_fd < 0) |
| 189 | return; |
| 190 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 191 | time_t cur_time; |
| 192 | struct tm *tm; |
| 193 | |
| 194 | struct fake_linux_lapd_header header = { |
| 195 | .pkttype = 4, |
| 196 | .hatype = 0, |
| 197 | .halen = 0, |
| 198 | .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1, |
| 199 | .protocol = ntohs(48), |
| 200 | }; |
| 201 | |
| 202 | struct lapd_header lapd_header = { |
| 203 | .ea1 = 0, |
| 204 | .cr = direction == PCAP_OUTPUT ? 1 : 0, |
| 205 | .sapi = sapi & 0x3F, |
| 206 | .ea2 = 1, |
| 207 | .tei = tei & 0x7F, |
| 208 | .control_foo = 0x03 /* UI */, |
| 209 | }; |
| 210 | |
| 211 | struct pcaprec_hdr payload_header = { |
| 212 | .ts_sec = 0, |
| 213 | .ts_usec = 0, |
| 214 | .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header) |
| 215 | + sizeof(struct lapd_header), |
| 216 | .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header) |
| 217 | + sizeof(struct lapd_header), |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | cur_time = time(NULL); |
| 222 | tm = localtime(&cur_time); |
| 223 | payload_header.ts_sec = mktime(tm); |
| 224 | |
Harald Welte | c9295ea | 2014-08-21 02:41:41 +0200 | [diff] [blame] | 225 | write(pcap_fd, &payload_header, sizeof(payload_header)); |
| 226 | write(pcap_fd, &header, sizeof(header)); |
| 227 | write(pcap_fd, &lapd_header, sizeof(lapd_header)); |
| 228 | write(pcap_fd, msg->l2h, msgb_l2len(msg)); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Harald Welte | 4ca5c53 | 2016-07-27 23:05:03 +0200 | [diff] [blame] | 231 | const struct value_string e1inp_sign_type_names[5] = { |
| 232 | { E1INP_SIGN_NONE, "None" }, |
| 233 | { E1INP_SIGN_OML, "OML" }, |
| 234 | { E1INP_SIGN_RSL, "RSL" }, |
| 235 | { E1INP_SIGN_OSMO, "OSMO" }, |
| 236 | { 0, NULL } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 237 | }; |
Harald Welte | 4ca5c53 | 2016-07-27 23:05:03 +0200 | [diff] [blame] | 238 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 239 | const char *e1inp_signtype_name(enum e1inp_sign_type tp) |
| 240 | { |
Harald Welte | 4ca5c53 | 2016-07-27 23:05:03 +0200 | [diff] [blame] | 241 | return get_value_string(e1inp_sign_type_names, tp); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 244 | const struct value_string e1inp_ts_type_names[6] = { |
Harald Welte | 4ca5c53 | 2016-07-27 23:05:03 +0200 | [diff] [blame] | 245 | { E1INP_TS_TYPE_NONE, "None" }, |
| 246 | { E1INP_TS_TYPE_SIGN, "Signalling" }, |
| 247 | { E1INP_TS_TYPE_TRAU, "TRAU" }, |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 248 | { E1INP_TS_TYPE_RAW, "RAW" }, |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 249 | { E1INP_TS_TYPE_HDLC, "HDLC" }, |
Harald Welte | 4ca5c53 | 2016-07-27 23:05:03 +0200 | [diff] [blame] | 250 | { 0, NULL } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | const char *e1inp_tstype_name(enum e1inp_ts_type tp) |
| 254 | { |
Harald Welte | 4ca5c53 | 2016-07-27 23:05:03 +0200 | [diff] [blame] | 255 | return get_value_string(e1inp_ts_type_names, tp); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Pablo Neira Ayuso | 96e7263 | 2011-06-26 19:08:05 +0200 | [diff] [blame] | 258 | int abis_sendmsg(struct msgb *msg) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 259 | { |
| 260 | struct e1inp_sign_link *sign_link = msg->dst; |
| 261 | struct e1inp_driver *e1inp_driver; |
| 262 | struct e1inp_ts *e1i_ts; |
Alexander Couzens | 35daa67 | 2016-11-08 16:17:20 +0100 | [diff] [blame] | 263 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 264 | msg->l2h = msg->data; |
| 265 | |
| 266 | /* don't know how to route this message. */ |
| 267 | if (sign_link == NULL) { |
Harald Welte | 40b0e8c | 2011-07-21 16:57:34 +0200 | [diff] [blame] | 268 | LOGP(DLINP, LOGL_ERROR, "abis_sendmsg: msg->dst == NULL: %s\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 269 | osmo_hexdump(msg->data, msg->len)); |
| 270 | talloc_free(msg); |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | e1i_ts = sign_link->ts; |
| 274 | if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) { |
| 275 | /* notify the driver we have something to write */ |
| 276 | e1inp_driver = sign_link->ts->line->driver; |
| 277 | e1inp_driver->want_write(e1i_ts); |
| 278 | } |
| 279 | msgb_enqueue(&sign_link->tx_list, msg); |
| 280 | |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 281 | /* we only need to write a 'Fake LAPD' packet here, if the |
| 282 | * underlying driver hides LAPD from us. If we use the |
| 283 | * libosmocore LAPD implementation, it will take care of writing |
| 284 | * the _actual_ LAPD packet */ |
| 285 | if (!e1i_ts->lapd) { |
| 286 | write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, |
| 287 | sign_link->tei, msg); |
| 288 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Pablo Neira Ayuso | 96e7263 | 2011-06-26 19:08:05 +0200 | [diff] [blame] | 293 | int abis_rsl_sendmsg(struct msgb *msg) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 294 | { |
Pablo Neira Ayuso | 96e7263 | 2011-06-26 19:08:05 +0200 | [diff] [blame] | 295 | return abis_sendmsg(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* Timeslot */ |
| 299 | int e1inp_ts_config_trau(struct e1inp_ts *ts, struct e1inp_line *line, |
Pablo Neira Ayuso | 211d2ca | 2011-06-07 17:15:10 +0200 | [diff] [blame] | 300 | int (*trau_rcv_cb)(struct subch_demux *dmx, int ch, |
Harald Welte | 5226e5b | 2020-05-07 23:09:16 +0200 | [diff] [blame] | 301 | const ubit_t *data, int len, void *_priv)) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 302 | { |
| 303 | if (ts->type == E1INP_TS_TYPE_TRAU && ts->line && line) |
| 304 | return 0; |
| 305 | |
| 306 | ts->type = E1INP_TS_TYPE_TRAU; |
| 307 | ts->line = line; |
| 308 | |
| 309 | subchan_mux_init(&ts->trau.mux); |
Pablo Neira Ayuso | 211d2ca | 2011-06-07 17:15:10 +0200 | [diff] [blame] | 310 | ts->trau.demux.out_cb = trau_rcv_cb; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 311 | ts->trau.demux.data = ts; |
| 312 | subch_demux_init(&ts->trau.demux); |
| 313 | return 0; |
| 314 | } |
| 315 | |
Harald Welte | b903188 | 2020-05-02 21:09:15 +0200 | [diff] [blame] | 316 | void e1inp_ts_name(char *out, size_t out_len, const struct e1inp_ts *ts) |
| 317 | { |
| 318 | if (ts->line->name) |
| 319 | snprintf(out, out_len, "%s:%u", ts->line->name, ts->num); |
| 320 | else |
| 321 | snprintf(out, out_len, "%u:%u", ts->line->num, ts->num); |
| 322 | } |
| 323 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 324 | int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line) |
| 325 | { |
| 326 | if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line) |
| 327 | return 0; |
| 328 | |
| 329 | ts->type = E1INP_TS_TYPE_SIGN; |
| 330 | ts->line = line; |
| 331 | |
| 332 | if (line && line->driver) |
| 333 | ts->sign.delay = line->driver->default_delay; |
| 334 | else |
| 335 | ts->sign.delay = 100000; |
| 336 | INIT_LLIST_HEAD(&ts->sign.sign_links); |
| 337 | return 0; |
| 338 | } |
| 339 | |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 340 | int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line, |
| 341 | void (*raw_recv_cb)(struct e1inp_ts *ts, |
| 342 | struct msgb *msg)) |
| 343 | { |
| 344 | if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line) |
| 345 | return 0; |
| 346 | |
| 347 | ts->type = E1INP_TS_TYPE_RAW; |
| 348 | ts->line = line; |
| 349 | ts->raw.recv_cb = raw_recv_cb; |
| 350 | INIT_LLIST_HEAD(&ts->raw.tx_queue); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 355 | int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line, |
| 356 | void (*hdlc_recv_cb)(struct e1inp_ts *ts, |
| 357 | struct msgb *msg)) |
| 358 | { |
| 359 | if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line) |
| 360 | return 0; |
| 361 | |
| 362 | ts->type = E1INP_TS_TYPE_HDLC; |
| 363 | ts->line = line; |
| 364 | ts->hdlc.recv_cb = hdlc_recv_cb; |
| 365 | INIT_LLIST_HEAD(&ts->hdlc.tx_queue); |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 370 | static int e1inp_line_use_cb(struct osmo_use_count_entry *use_count_entry, int32_t old_use_count, |
| 371 | const char *file, int file_line) |
| 372 | { |
| 373 | char buf[512]; |
| 374 | struct osmo_use_count *uc = use_count_entry->use_count; |
| 375 | struct e1inp_line *line = uc->talloc_object; |
| 376 | |
| 377 | LOGPSRC(DLINP, LOGL_INFO, file, file_line, |
| 378 | "E1L(%u) Line (%p) reference count %s changed %" PRId32 " -> %" PRId32 " [%s]\n", |
| 379 | (line)->num, line, use_count_entry->use, |
| 380 | old_use_count, use_count_entry->count, |
| 381 | osmo_use_count_name_buf(buf, sizeof(buf), uc)); |
| 382 | |
| 383 | if (!use_count_entry->count) |
| 384 | osmo_use_count_free(use_count_entry); |
| 385 | |
| 386 | if (osmo_use_count_total(uc) > 0) |
| 387 | return 0; |
| 388 | |
| 389 | /* Remove our counter group from libosmocore's global counter |
| 390 | * list if we are freeing the last remaining talloc context. |
| 391 | * Otherwise we get a use-after-free when libosmocore's timer |
| 392 | * ticks again and attempts to update these counters (OS#3011). |
| 393 | * |
| 394 | * Note that talloc internally counts "secondary" references |
| 395 | * _in addition to_ the initial allocation context, so yes, |
| 396 | * we must check for *zero* remaining secondary contexts here. */ |
| 397 | if (talloc_reference_count(line->rate_ctr) == 0) { |
| 398 | rate_ctr_group_free(line->rate_ctr); |
| 399 | } else { |
| 400 | /* We are not freeing the last talloc context. |
| 401 | * Instead of calling talloc_free(), unlink this 'line' pointer |
| 402 | * which serves as one of several talloc contexts for the rate |
| 403 | * counters and driver private state. */ |
| 404 | talloc_unlink(line, line->rate_ctr); |
| 405 | if (line->driver_data) |
| 406 | talloc_unlink(line, line->driver_data); |
| 407 | } |
| 408 | talloc_free(line); |
| 409 | return 0; |
| 410 | } |
| 411 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 412 | struct e1inp_line *e1inp_line_find(uint8_t e1_nr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 413 | { |
| 414 | struct e1inp_line *e1i_line; |
| 415 | |
| 416 | /* iterate over global list of e1 lines */ |
| 417 | llist_for_each_entry(e1i_line, &e1inp_line_list, list) { |
| 418 | if (e1i_line->num == e1_nr) |
| 419 | return e1i_line; |
| 420 | } |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | struct e1inp_line * |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 425 | e1inp_line_create(uint8_t e1_nr, const char *driver_name) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 426 | { |
| 427 | struct e1inp_driver *driver; |
| 428 | struct e1inp_line *line; |
| 429 | int i; |
| 430 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 431 | line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 432 | if (line) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 433 | LOGPIL(line, DLINP, LOGL_ERROR, "E1 Line %u already exists\n", e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | driver = e1inp_driver_find(driver_name); |
| 438 | if (!driver) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 439 | LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 440 | driver_name); |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | line = talloc_zero(tall_e1inp_ctx, struct e1inp_line); |
| 445 | if (!line) |
| 446 | return NULL; |
| 447 | |
| 448 | line->driver = driver; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 449 | line->num = e1_nr; |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 450 | |
| 451 | line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num); |
Harald Welte | ff8eed2 | 2017-07-12 00:38:25 +0200 | [diff] [blame] | 452 | if (!line->rate_ctr) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 453 | LOGPIL(line, DLINP, LOGL_ERROR, "Cannot allocate counter group\n"); |
Harald Welte | ff8eed2 | 2017-07-12 00:38:25 +0200 | [diff] [blame] | 454 | talloc_free(line); |
| 455 | return NULL; |
| 456 | } |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 457 | |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 458 | line->num_ts = NUM_E1_TS; |
| 459 | for (i = 0; i < line->num_ts; i++) { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 460 | line->ts[i].num = i+1; |
| 461 | line->ts[i].line = line; |
| 462 | } |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 463 | |
| 464 | line->use_count.talloc_object = line; |
| 465 | line->use_count.use_cb = e1inp_line_use_cb; |
| 466 | e1inp_line_get2(line, "ctor"); |
| 467 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 468 | llist_add_tail(&line->list, &e1inp_line_list); |
| 469 | |
| 470 | return line; |
| 471 | } |
| 472 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 473 | struct e1inp_line * |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 474 | e1inp_line_clone(void *ctx, struct e1inp_line *line, const char *use) |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 475 | { |
| 476 | struct e1inp_line *clone; |
| 477 | |
| 478 | /* clone virtual E1 line for this new OML link. */ |
| 479 | clone = talloc_zero(ctx, struct e1inp_line); |
| 480 | if (clone == NULL) |
| 481 | return NULL; |
| 482 | |
| 483 | memcpy(clone, line, sizeof(struct e1inp_line)); |
Stefan Sperling | b016207 | 2018-05-22 18:19:00 +0200 | [diff] [blame] | 484 | |
| 485 | if (line->name) { |
| 486 | clone->name = talloc_strdup(clone, line->name); |
| 487 | OSMO_ASSERT(clone->name); |
| 488 | } |
| 489 | if (line->sock_path) { |
| 490 | clone->sock_path = talloc_strdup(clone, line->sock_path); |
| 491 | OSMO_ASSERT(clone->sock_path); |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Rate counters and driver data are shared between clones. These are pointers |
| 496 | * to dynamic memory so we use reference counting to avoid a double-free (see OS#3137). |
| 497 | */ |
| 498 | OSMO_ASSERT(line->rate_ctr); |
| 499 | clone->rate_ctr = talloc_reference(clone, line->rate_ctr); |
| 500 | if (line->driver_data) |
| 501 | clone->driver_data = talloc_reference(clone, line->driver_data); |
| 502 | |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 503 | clone->use_count = (struct osmo_use_count) { |
| 504 | .talloc_object = clone, |
| 505 | .use_cb = e1inp_line_use_cb, |
| 506 | .use_counts = {0}, |
| 507 | }; |
| 508 | e1inp_line_get2(clone, use); /* Clone is used internally for bfd */ |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 509 | return clone; |
| 510 | } |
| 511 | |
| 512 | void e1inp_line_get(struct e1inp_line *line) |
| 513 | { |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 514 | e1inp_line_get2(line, "unknown"); |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void e1inp_line_put(struct e1inp_line *line) |
| 518 | { |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 519 | e1inp_line_put2(line, "unknown"); |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 522 | void |
| 523 | e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops) |
| 524 | { |
| 525 | line->ops = ops; |
| 526 | } |
| 527 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 528 | #if 0 |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 529 | struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 530 | { |
| 531 | struct e1inp_line *line; |
| 532 | int i; |
| 533 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 534 | line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 535 | if (line) |
| 536 | return line; |
| 537 | |
| 538 | line = talloc_zero(tall_e1inp_ctx, struct e1inp_line); |
| 539 | if (!line) |
| 540 | return NULL; |
| 541 | |
| 542 | line->num = e1_nr; |
| 543 | for (i = 0; i < NUM_E1_TS; i++) { |
| 544 | line->ts[i].num = i+1; |
| 545 | line->ts[i].line = line; |
| 546 | } |
| 547 | llist_add_tail(&line->list, &e1inp_line_list); |
| 548 | |
| 549 | return line; |
| 550 | } |
| 551 | #endif |
| 552 | |
| 553 | static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr) |
| 554 | { |
| 555 | struct e1inp_line *e1i_line; |
| 556 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 557 | e1i_line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 558 | if (!e1i_line) |
| 559 | return NULL; |
| 560 | |
| 561 | return &e1i_line->ts[ts_nr-1]; |
| 562 | } |
| 563 | |
| 564 | struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr) |
| 565 | { |
| 566 | struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr); |
| 567 | |
| 568 | if (!e1i_ts) |
| 569 | return NULL; |
| 570 | |
| 571 | return &e1i_ts->trau.mux; |
| 572 | } |
| 573 | |
| 574 | /* Signalling Link */ |
| 575 | |
| 576 | struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i, |
| 577 | uint8_t tei, uint8_t sapi) |
| 578 | { |
| 579 | struct e1inp_sign_link *link; |
| 580 | |
| 581 | llist_for_each_entry(link, &e1i->sign.sign_links, list) { |
| 582 | if (link->sapi == sapi && link->tei == tei) |
| 583 | return link; |
| 584 | } |
| 585 | |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | /* create a new signalling link in a E1 timeslot */ |
| 590 | |
| 591 | struct e1inp_sign_link * |
| 592 | e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type, |
| 593 | struct gsm_bts_trx *trx, uint8_t tei, |
| 594 | uint8_t sapi) |
| 595 | { |
| 596 | struct e1inp_sign_link *link; |
| 597 | |
| 598 | if (ts->type != E1INP_TS_TYPE_SIGN) |
| 599 | return NULL; |
| 600 | |
| 601 | link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link); |
| 602 | if (!link) |
| 603 | return NULL; |
| 604 | |
| 605 | link->ts = ts; |
| 606 | link->type = type; |
| 607 | INIT_LLIST_HEAD(&link->tx_list); |
| 608 | link->trx = trx; |
| 609 | link->tei = tei; |
| 610 | link->sapi = sapi; |
| 611 | |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 612 | e1inp_line_get2(link->ts->line, "e1inp_sign_link"); |
Pau Espin Pedrol | 89c6b8a | 2020-06-08 20:26:44 +0200 | [diff] [blame] | 613 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 614 | llist_add_tail(&link->list, &ts->sign.sign_links); |
| 615 | |
| 616 | return link; |
| 617 | } |
| 618 | |
| 619 | void e1inp_sign_link_destroy(struct e1inp_sign_link *link) |
| 620 | { |
| 621 | struct msgb *msg; |
| 622 | |
| 623 | llist_del(&link->list); |
| 624 | while (!llist_empty(&link->tx_list)) { |
| 625 | msg = msgb_dequeue(&link->tx_list); |
| 626 | msgb_free(msg); |
| 627 | } |
| 628 | |
| 629 | if (link->ts->type == E1INP_TS_TYPE_SIGN) |
| 630 | osmo_timer_del(&link->ts->sign.tx_timer); |
| 631 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 632 | if (link->ts->line->driver->close) |
Pablo Neira Ayuso | add3ec8 | 2011-07-05 14:45:46 +0200 | [diff] [blame] | 633 | link->ts->line->driver->close(link); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 634 | |
Pau Espin Pedrol | 5196cd5 | 2020-07-14 17:52:09 +0200 | [diff] [blame] | 635 | e1inp_line_put2(link->ts->line, "e1inp_sign_link"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 636 | talloc_free(link); |
| 637 | } |
| 638 | |
| 639 | /* XXX */ |
| 640 | /* the E1 driver tells us he has received something on a TS */ |
| 641 | int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg, |
| 642 | uint8_t tei, uint8_t sapi) |
| 643 | { |
| 644 | struct e1inp_sign_link *link; |
| 645 | int ret = 0; |
| 646 | |
| 647 | switch (ts->type) { |
| 648 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 649 | /* we only need to write a 'Fake LAPD' packet here, if |
| 650 | * the underlying driver hides LAPD from us. If we use |
| 651 | * the libosmocore LAPD implementation, it will take |
| 652 | * care of writing the _actual_ LAPD packet */ |
| 653 | if (!ts->lapd) |
| 654 | write_pcap_packet(PCAP_INPUT, sapi, tei, msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 655 | /* consult the list of signalling links */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 656 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 657 | if (!link) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 658 | LOGPITS(ts, DLMI, LOGL_ERROR, "didn't find signalling link for " |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 659 | "tei %d, sapi %d\n", tei, sapi); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 660 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 661 | return -EINVAL; |
| 662 | } |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 663 | if (!ts->line->ops->sign_link) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 664 | LOGPITS(ts, DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 665 | "no action set for signalling messages.\n"); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 666 | msgb_free(msg); |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 667 | return -ENOENT; |
| 668 | } |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 669 | msg->dst = link; |
| 670 | ts->line->ops->sign_link(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 671 | break; |
| 672 | case E1INP_TS_TYPE_TRAU: |
Pablo Neira Ayuso | 211d2ca | 2011-06-07 17:15:10 +0200 | [diff] [blame] | 673 | ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg)); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 674 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 675 | break; |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 676 | case E1INP_TS_TYPE_RAW: |
| 677 | ts->raw.recv_cb(ts, msg); |
| 678 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 679 | case E1INP_TS_TYPE_HDLC: |
| 680 | ts->hdlc.recv_cb(ts, msg); |
| 681 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 682 | default: |
| 683 | ret = -EINVAL; |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 684 | LOGPITS(ts, DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 685 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 686 | break; |
| 687 | } |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 692 | /*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot |
| 693 | * \param[in] e1i_ts E1 Timeslot data structure |
| 694 | * \param[in] msg Message buffer containing full LAPD message |
| 695 | * |
| 696 | * This is a wrapper around e1inp_rx_ts(), but feeding the incoming |
| 697 | * message first into our LAPD code. This allows a driver to read raw |
| 698 | * (HDLC decoded) data from the timeslot, instead of a LAPD stack |
| 699 | * present in any underlying driver. |
| 700 | */ |
| 701 | int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg) |
| 702 | { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 703 | unsigned int sapi, tei; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 704 | int ret = 0, error = 0; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 705 | |
| 706 | sapi = msg->data[0] >> 2; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 707 | if ((msg->data[0] & 0x1)) |
| 708 | tei = 0; |
| 709 | else |
| 710 | tei = msg->data[1] >> 1; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 711 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 712 | LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "<= len = %d, sapi(%d) tei(%d)\n", msg->len, sapi, tei); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 713 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 714 | ret = lapd_receive(e1i_ts->lapd, msg, &error); |
| 715 | if (ret < 0) { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 716 | switch(error) { |
| 717 | case LAPD_ERR_UNKNOWN_TEI: |
| 718 | /* We don't know about this TEI, probably the BSC |
| 719 | * lost local states (it crashed or it was stopped), |
| 720 | * notify the driver to see if it can do anything to |
| 721 | * recover the existing signalling links with the BTS. |
| 722 | */ |
| 723 | e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi); |
| 724 | return -EIO; |
| 725 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 728 | return 0; |
| 729 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 730 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 731 | void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi, |
| 732 | void *rx_cbdata) |
| 733 | { |
| 734 | struct e1inp_ts *e1i_ts = rx_cbdata; |
| 735 | struct msgb *msg = dp->oph.msg; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 736 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 737 | switch (dp->oph.primitive) { |
| 738 | case PRIM_DL_EST: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 739 | LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 740 | e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 741 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 742 | case PRIM_DL_REL: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 743 | LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 744 | e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 745 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 746 | case PRIM_DL_DATA: |
| 747 | case PRIM_DL_UNIT_DATA: |
| 748 | if (dp->oph.operation == PRIM_OP_INDICATION) { |
| 749 | msg->l2h = msg->l3h; |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 750 | LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "RX: %s sapi=%d tei=%d\n", |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 751 | osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)), |
| 752 | sapi, tei); |
| 753 | e1inp_rx_ts(e1i_ts, msg, tei, sapi); |
| 754 | return; |
| 755 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 756 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 757 | case PRIM_MDL_ERROR: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 758 | LOGPITS(e1i_ts, DLMI, LOGL_DEBUG, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 759 | break; |
| 760 | default: |
| 761 | printf("ERROR: unknown prim\n"); |
| 762 | break; |
| 763 | } |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 764 | |
| 765 | msgb_free(msg); |
| 766 | |
| 767 | return; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 770 | #define TSX_ALLOC_SIZE 4096 |
| 771 | |
| 772 | /* called by driver if it wants to transmit on a given TS */ |
| 773 | struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts, |
| 774 | struct e1inp_sign_link **sign_link) |
| 775 | { |
| 776 | struct e1inp_sign_link *link; |
| 777 | struct msgb *msg = NULL; |
| 778 | int len; |
| 779 | |
| 780 | switch (e1i_ts->type) { |
| 781 | case E1INP_TS_TYPE_SIGN: |
| 782 | /* FIXME: implement this round robin */ |
| 783 | llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) { |
| 784 | msg = msgb_dequeue(&link->tx_list); |
| 785 | if (msg) { |
| 786 | if (sign_link) |
| 787 | *sign_link = link; |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | break; |
| 792 | case E1INP_TS_TYPE_TRAU: |
| 793 | msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX"); |
| 794 | if (!msg) |
| 795 | return NULL; |
| 796 | len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40); |
Pablo Neira Ayuso | 33f7175 | 2013-07-05 15:44:12 +0200 | [diff] [blame] | 797 | if (len != 40) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 798 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "cannot transmit, failed to mux\n"); |
Pablo Neira Ayuso | 33f7175 | 2013-07-05 15:44:12 +0200 | [diff] [blame] | 799 | msgb_free(msg); |
| 800 | return NULL; |
| 801 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 802 | msgb_put(msg, 40); |
| 803 | break; |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 804 | case E1INP_TS_TYPE_RAW: |
| 805 | /* Get msgb from tx_queue */ |
| 806 | msg = msgb_dequeue(&e1i_ts->raw.tx_queue); |
| 807 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 808 | case E1INP_TS_TYPE_HDLC: |
| 809 | /* Get msgb from tx_queue */ |
| 810 | msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue); |
| 811 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 812 | default: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 813 | LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 814 | return NULL; |
| 815 | } |
| 816 | return msg; |
| 817 | } |
| 818 | |
Pau Espin Pedrol | 8737ad4 | 2020-07-14 21:11:56 +0200 | [diff] [blame] | 819 | int e1inp_int_snd_event(struct e1inp_ts *ts, struct e1inp_sign_link *link, int evt) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 820 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 821 | struct input_signal_data isd; |
Pablo Neira Ayuso | 31fe5f2 | 2011-08-09 23:15:38 +0200 | [diff] [blame] | 822 | isd.line = ts->line; |
Harald Welte | f35d889 | 2016-10-16 15:33:52 +0200 | [diff] [blame] | 823 | isd.ts_nr = ts->num; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 824 | isd.link_type = link->type; |
| 825 | isd.trx = link->trx; |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 826 | isd.tei = link->tei; |
| 827 | isd.sapi = link->sapi; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 828 | |
| 829 | /* report further upwards */ |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 830 | osmo_signal_dispatch(SS_L_INPUT, evt, &isd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 834 | |
| 835 | /* called by driver in case some kind of link state event */ |
| 836 | int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi) |
| 837 | { |
| 838 | struct e1inp_sign_link *link; |
| 839 | |
| 840 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 841 | if (!link) |
| 842 | return -EINVAL; |
| 843 | return e1inp_int_snd_event(ts, link, evt); |
| 844 | } |
| 845 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 846 | /* register a driver with the E1 core */ |
| 847 | int e1inp_driver_register(struct e1inp_driver *drv) |
| 848 | { |
| 849 | llist_add_tail(&drv->list, &e1inp_driver_list); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | struct e1inp_driver *e1inp_driver_find(const char *name) |
| 854 | { |
| 855 | struct e1inp_driver *drv; |
| 856 | |
| 857 | llist_for_each_entry(drv, &e1inp_driver_list, list) { |
| 858 | if (!strcasecmp(name, drv->name)) |
| 859 | return drv; |
| 860 | } |
| 861 | return NULL; |
| 862 | } |
| 863 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 864 | int e1inp_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 865 | { |
| 866 | struct input_signal_data isd; |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 867 | int i, rc; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 868 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 869 | if (line->driver && line->ops && line->driver->line_update) { |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 870 | rc = line->driver->line_update(line); |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 871 | } else |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 872 | rc = 0; |
| 873 | |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 874 | /* Set the PCAP file descriptor for all timeslots that have |
| 875 | * software LAPD instances, to ensure the osmo_lapd_pcap code is |
| 876 | * used to write PCAP files (if requested) */ |
| 877 | for (i = 0; i < ARRAY_SIZE(line->ts); i++) { |
| 878 | struct e1inp_ts *e1i_ts = &line->ts[i]; |
| 879 | if (e1i_ts->lapd) |
| 880 | e1i_ts->lapd->pcap_fd = pcap_fd; |
| 881 | } |
| 882 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 883 | /* Send a signal to anyone who is interested in new lines being |
| 884 | * configured */ |
| 885 | memset(&isd, 0, sizeof(isd)); |
| 886 | isd.line = line; |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 887 | osmo_signal_dispatch(SS_L_INPUT, S_L_INP_LINE_INIT, &isd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 888 | |
| 889 | return rc; |
| 890 | } |
| 891 | |
Pablo Neira Ayuso | a20762a | 2011-07-02 19:01:58 +0200 | [diff] [blame] | 892 | static int e1i_sig_cb(unsigned int subsys, unsigned int signal, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 893 | void *handler_data, void *signal_data) |
| 894 | { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 895 | if (subsys != SS_L_GLOBAL || |
| 896 | signal != S_L_GLOBAL_SHUTDOWN) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 897 | return 0; |
| 898 | |
| 899 | if (pcap_fd) { |
| 900 | close(pcap_fd); |
| 901 | pcap_fd = -1; |
| 902 | } |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
Harald Welte | cac78fe | 2017-05-25 19:13:13 +0200 | [diff] [blame] | 907 | const struct value_string e1inp_signal_names[] = { |
| 908 | { S_L_INP_NONE, "NONE" }, |
| 909 | { S_L_INP_TEI_UP, "TEI-UP" }, |
| 910 | { S_L_INP_TEI_DN, "TEI-DOWN" }, |
| 911 | { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" }, |
| 912 | { S_L_INP_LINE_INIT, "LINE-INIT" }, |
| 913 | { S_L_INP_LINE_ALARM, "LINE-ALARM" }, |
| 914 | { S_L_INP_LINE_NOALARM, "LINE-NOALARM" }, |
| 915 | { 0, NULL } |
| 916 | }; |
| 917 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 918 | void e1inp_misdn_init(void); |
| 919 | void e1inp_dahdi_init(void); |
Harald Welte | c6c7076 | 2019-05-12 21:54:59 +0200 | [diff] [blame] | 920 | void e1inp_e1d_init(void); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 921 | void e1inp_ipaccess_init(void); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 922 | void e1inp_rs232_init(void); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 923 | void e1inp_unixsocket_init(void); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 924 | |
| 925 | void e1inp_init(void) |
| 926 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 927 | tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 928 | tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1, |
| 929 | "e1inp_sign_link"); |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 930 | osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 931 | |
| 932 | e1inp_misdn_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 933 | #ifdef HAVE_DAHDI_USER_H |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 934 | e1inp_dahdi_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 935 | #endif |
Harald Welte | 52b0d45 | 2019-05-12 21:54:26 +0200 | [diff] [blame] | 936 | #ifdef HAVE_E1D |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 937 | e1inp_e1d_init(); |
| 938 | #endif |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 939 | e1inp_ipaccess_init(); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 940 | e1inp_rs232_init(); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 941 | e1inp_unixsocket_init(); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 942 | } |