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