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