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, |
| 300 | uint8_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 | |
| 315 | int e1inp_ts_config_sign(struct e1inp_ts *ts, struct e1inp_line *line) |
| 316 | { |
| 317 | if (ts->type == E1INP_TS_TYPE_SIGN && ts->line && line) |
| 318 | return 0; |
| 319 | |
| 320 | ts->type = E1INP_TS_TYPE_SIGN; |
| 321 | ts->line = line; |
| 322 | |
| 323 | if (line && line->driver) |
| 324 | ts->sign.delay = line->driver->default_delay; |
| 325 | else |
| 326 | ts->sign.delay = 100000; |
| 327 | INIT_LLIST_HEAD(&ts->sign.sign_links); |
| 328 | return 0; |
| 329 | } |
| 330 | |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 331 | int e1inp_ts_config_raw(struct e1inp_ts *ts, struct e1inp_line *line, |
| 332 | void (*raw_recv_cb)(struct e1inp_ts *ts, |
| 333 | struct msgb *msg)) |
| 334 | { |
| 335 | if (ts->type == E1INP_TS_TYPE_RAW && ts->line && line) |
| 336 | return 0; |
| 337 | |
| 338 | ts->type = E1INP_TS_TYPE_RAW; |
| 339 | ts->line = line; |
| 340 | ts->raw.recv_cb = raw_recv_cb; |
| 341 | INIT_LLIST_HEAD(&ts->raw.tx_queue); |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 346 | int e1inp_ts_config_hdlc(struct e1inp_ts *ts, struct e1inp_line *line, |
| 347 | void (*hdlc_recv_cb)(struct e1inp_ts *ts, |
| 348 | struct msgb *msg)) |
| 349 | { |
| 350 | if (ts->type == E1INP_TS_TYPE_HDLC && ts->line && line) |
| 351 | return 0; |
| 352 | |
| 353 | ts->type = E1INP_TS_TYPE_HDLC; |
| 354 | ts->line = line; |
| 355 | ts->hdlc.recv_cb = hdlc_recv_cb; |
| 356 | INIT_LLIST_HEAD(&ts->hdlc.tx_queue); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 361 | struct e1inp_line *e1inp_line_find(uint8_t e1_nr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 362 | { |
| 363 | struct e1inp_line *e1i_line; |
| 364 | |
| 365 | /* iterate over global list of e1 lines */ |
| 366 | llist_for_each_entry(e1i_line, &e1inp_line_list, list) { |
| 367 | if (e1i_line->num == e1_nr) |
| 368 | return e1i_line; |
| 369 | } |
| 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | struct e1inp_line * |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 374 | e1inp_line_create(uint8_t e1_nr, const char *driver_name) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 375 | { |
| 376 | struct e1inp_driver *driver; |
| 377 | struct e1inp_line *line; |
| 378 | int i; |
| 379 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 380 | line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 381 | if (line) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 382 | 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] | 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | driver = e1inp_driver_find(driver_name); |
| 387 | if (!driver) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 388 | LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 389 | driver_name); |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | line = talloc_zero(tall_e1inp_ctx, struct e1inp_line); |
| 394 | if (!line) |
| 395 | return NULL; |
| 396 | |
| 397 | line->driver = driver; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 398 | line->num = e1_nr; |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 399 | |
| 400 | 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] | 401 | if (!line->rate_ctr) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 402 | LOGPIL(line, DLINP, LOGL_ERROR, "Cannot allocate counter group\n"); |
Harald Welte | ff8eed2 | 2017-07-12 00:38:25 +0200 | [diff] [blame] | 403 | talloc_free(line); |
| 404 | return NULL; |
| 405 | } |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 406 | |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 407 | line->num_ts = NUM_E1_TS; |
| 408 | for (i = 0; i < line->num_ts; i++) { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 409 | line->ts[i].num = i+1; |
| 410 | line->ts[i].line = line; |
| 411 | } |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 412 | line->refcnt++; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 413 | llist_add_tail(&line->list, &e1inp_line_list); |
| 414 | |
| 415 | return line; |
| 416 | } |
| 417 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 418 | struct e1inp_line * |
| 419 | e1inp_line_clone(void *ctx, struct e1inp_line *line) |
| 420 | { |
| 421 | struct e1inp_line *clone; |
| 422 | |
| 423 | /* clone virtual E1 line for this new OML link. */ |
| 424 | clone = talloc_zero(ctx, struct e1inp_line); |
| 425 | if (clone == NULL) |
| 426 | return NULL; |
| 427 | |
| 428 | memcpy(clone, line, sizeof(struct e1inp_line)); |
Stefan Sperling | b016207 | 2018-05-22 18:19:00 +0200 | [diff] [blame] | 429 | |
| 430 | if (line->name) { |
| 431 | clone->name = talloc_strdup(clone, line->name); |
| 432 | OSMO_ASSERT(clone->name); |
| 433 | } |
| 434 | if (line->sock_path) { |
| 435 | clone->sock_path = talloc_strdup(clone, line->sock_path); |
| 436 | OSMO_ASSERT(clone->sock_path); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Rate counters and driver data are shared between clones. These are pointers |
| 441 | * to dynamic memory so we use reference counting to avoid a double-free (see OS#3137). |
| 442 | */ |
| 443 | OSMO_ASSERT(line->rate_ctr); |
| 444 | clone->rate_ctr = talloc_reference(clone, line->rate_ctr); |
| 445 | if (line->driver_data) |
| 446 | clone->driver_data = talloc_reference(clone, line->driver_data); |
| 447 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 448 | clone->refcnt = 1; |
| 449 | return clone; |
| 450 | } |
| 451 | |
| 452 | void e1inp_line_get(struct e1inp_line *line) |
| 453 | { |
Vadim Yanitskiy | b43ce42 | 2019-12-02 01:49:43 +0700 | [diff] [blame] | 454 | int old_refcnt = line->refcnt++; |
| 455 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 456 | 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] | 457 | line->name, line, old_refcnt, line->refcnt); |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void e1inp_line_put(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 put: %d -> %d\n", |
Vadim Yanitskiy | b43ce42 | 2019-12-02 01:49:43 +0700 | [diff] [blame] | 465 | line->name, line, old_refcnt, line->refcnt); |
| 466 | |
Stefan Sperling | b016207 | 2018-05-22 18:19:00 +0200 | [diff] [blame] | 467 | if (line->refcnt == 0) { |
| 468 | /* Remove our counter group from libosmocore's global counter |
| 469 | * list if we are freeing the last remaining talloc context. |
| 470 | * Otherwise we get a use-after-free when libosmocore's timer |
| 471 | * ticks again and attempts to update these counters (OS#3011). |
| 472 | * |
| 473 | * Note that talloc internally counts "secondary" references |
| 474 | * _in addition to_ the initial allocation context, so yes, |
| 475 | * we must check for *zero* remaining secondary contexts here. */ |
| 476 | if (talloc_reference_count(line->rate_ctr) == 0) { |
| 477 | rate_ctr_group_free(line->rate_ctr); |
| 478 | } else { |
| 479 | /* We are not freeing the last talloc context. |
| 480 | * Instead of calling talloc_free(), unlink this 'line' pointer |
| 481 | * which serves as one of several talloc contexts for the rate |
| 482 | * counters and driver private state. */ |
| 483 | talloc_unlink(line, line->rate_ctr); |
| 484 | if (line->driver_data) |
| 485 | talloc_unlink(line, line->driver_data); |
| 486 | } |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 487 | talloc_free(line); |
Stefan Sperling | b016207 | 2018-05-22 18:19:00 +0200 | [diff] [blame] | 488 | } |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 491 | void |
| 492 | e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops) |
| 493 | { |
| 494 | line->ops = ops; |
| 495 | } |
| 496 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 497 | #if 0 |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 498 | struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 499 | { |
| 500 | struct e1inp_line *line; |
| 501 | int i; |
| 502 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 503 | line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 504 | if (line) |
| 505 | return line; |
| 506 | |
| 507 | line = talloc_zero(tall_e1inp_ctx, struct e1inp_line); |
| 508 | if (!line) |
| 509 | return NULL; |
| 510 | |
| 511 | line->num = e1_nr; |
| 512 | for (i = 0; i < NUM_E1_TS; i++) { |
| 513 | line->ts[i].num = i+1; |
| 514 | line->ts[i].line = line; |
| 515 | } |
| 516 | llist_add_tail(&line->list, &e1inp_line_list); |
| 517 | |
| 518 | return line; |
| 519 | } |
| 520 | #endif |
| 521 | |
| 522 | static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr) |
| 523 | { |
| 524 | struct e1inp_line *e1i_line; |
| 525 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 526 | e1i_line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 527 | if (!e1i_line) |
| 528 | return NULL; |
| 529 | |
| 530 | return &e1i_line->ts[ts_nr-1]; |
| 531 | } |
| 532 | |
| 533 | struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr) |
| 534 | { |
| 535 | struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr); |
| 536 | |
| 537 | if (!e1i_ts) |
| 538 | return NULL; |
| 539 | |
| 540 | return &e1i_ts->trau.mux; |
| 541 | } |
| 542 | |
| 543 | /* Signalling Link */ |
| 544 | |
| 545 | struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i, |
| 546 | uint8_t tei, uint8_t sapi) |
| 547 | { |
| 548 | struct e1inp_sign_link *link; |
| 549 | |
| 550 | llist_for_each_entry(link, &e1i->sign.sign_links, list) { |
| 551 | if (link->sapi == sapi && link->tei == tei) |
| 552 | return link; |
| 553 | } |
| 554 | |
| 555 | return NULL; |
| 556 | } |
| 557 | |
| 558 | /* create a new signalling link in a E1 timeslot */ |
| 559 | |
| 560 | struct e1inp_sign_link * |
| 561 | e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type, |
| 562 | struct gsm_bts_trx *trx, uint8_t tei, |
| 563 | uint8_t sapi) |
| 564 | { |
| 565 | struct e1inp_sign_link *link; |
| 566 | |
| 567 | if (ts->type != E1INP_TS_TYPE_SIGN) |
| 568 | return NULL; |
| 569 | |
| 570 | link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link); |
| 571 | if (!link) |
| 572 | return NULL; |
| 573 | |
| 574 | link->ts = ts; |
| 575 | link->type = type; |
| 576 | INIT_LLIST_HEAD(&link->tx_list); |
| 577 | link->trx = trx; |
| 578 | link->tei = tei; |
| 579 | link->sapi = sapi; |
| 580 | |
| 581 | llist_add_tail(&link->list, &ts->sign.sign_links); |
| 582 | |
| 583 | return link; |
| 584 | } |
| 585 | |
| 586 | void e1inp_sign_link_destroy(struct e1inp_sign_link *link) |
| 587 | { |
| 588 | struct msgb *msg; |
| 589 | |
| 590 | llist_del(&link->list); |
| 591 | while (!llist_empty(&link->tx_list)) { |
| 592 | msg = msgb_dequeue(&link->tx_list); |
| 593 | msgb_free(msg); |
| 594 | } |
| 595 | |
| 596 | if (link->ts->type == E1INP_TS_TYPE_SIGN) |
| 597 | osmo_timer_del(&link->ts->sign.tx_timer); |
| 598 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 599 | if (link->ts->line->driver->close) |
Pablo Neira Ayuso | add3ec8 | 2011-07-05 14:45:46 +0200 | [diff] [blame] | 600 | link->ts->line->driver->close(link); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 601 | |
Pablo Neira Ayuso | 6cc3f92 | 2012-08-22 13:57:58 +0200 | [diff] [blame] | 602 | e1inp_line_put(link->ts->line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 603 | talloc_free(link); |
| 604 | } |
| 605 | |
| 606 | /* XXX */ |
| 607 | /* the E1 driver tells us he has received something on a TS */ |
| 608 | int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg, |
| 609 | uint8_t tei, uint8_t sapi) |
| 610 | { |
| 611 | struct e1inp_sign_link *link; |
| 612 | int ret = 0; |
| 613 | |
| 614 | switch (ts->type) { |
| 615 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 616 | /* we only need to write a 'Fake LAPD' packet here, if |
| 617 | * the underlying driver hides LAPD from us. If we use |
| 618 | * the libosmocore LAPD implementation, it will take |
| 619 | * care of writing the _actual_ LAPD packet */ |
| 620 | if (!ts->lapd) |
| 621 | write_pcap_packet(PCAP_INPUT, sapi, tei, msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 622 | /* consult the list of signalling links */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 623 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 624 | if (!link) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 625 | LOGPITS(ts, DLMI, LOGL_ERROR, "didn't find signalling link for " |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 626 | "tei %d, sapi %d\n", tei, sapi); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 627 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 628 | return -EINVAL; |
| 629 | } |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 630 | if (!ts->line->ops->sign_link) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 631 | LOGPITS(ts, DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 632 | "no action set for signalling messages.\n"); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 633 | msgb_free(msg); |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 634 | return -ENOENT; |
| 635 | } |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 636 | msg->dst = link; |
| 637 | ts->line->ops->sign_link(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 638 | break; |
| 639 | case E1INP_TS_TYPE_TRAU: |
Pablo Neira Ayuso | 211d2ca | 2011-06-07 17:15:10 +0200 | [diff] [blame] | 640 | ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg)); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 641 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 642 | break; |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 643 | case E1INP_TS_TYPE_RAW: |
| 644 | ts->raw.recv_cb(ts, msg); |
| 645 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 646 | case E1INP_TS_TYPE_HDLC: |
| 647 | ts->hdlc.recv_cb(ts, msg); |
| 648 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 649 | default: |
| 650 | ret = -EINVAL; |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 651 | LOGPITS(ts, DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 652 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | |
| 656 | return ret; |
| 657 | } |
| 658 | |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 659 | /*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot |
| 660 | * \param[in] e1i_ts E1 Timeslot data structure |
| 661 | * \param[in] msg Message buffer containing full LAPD message |
| 662 | * |
| 663 | * This is a wrapper around e1inp_rx_ts(), but feeding the incoming |
| 664 | * message first into our LAPD code. This allows a driver to read raw |
| 665 | * (HDLC decoded) data from the timeslot, instead of a LAPD stack |
| 666 | * present in any underlying driver. |
| 667 | */ |
| 668 | int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg) |
| 669 | { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 670 | unsigned int sapi, tei; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 671 | int ret = 0, error = 0; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 672 | |
| 673 | sapi = msg->data[0] >> 2; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 674 | if ((msg->data[0] & 0x1)) |
| 675 | tei = 0; |
| 676 | else |
| 677 | tei = msg->data[1] >> 1; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 678 | |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 679 | 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] | 680 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 681 | ret = lapd_receive(e1i_ts->lapd, msg, &error); |
| 682 | if (ret < 0) { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 683 | switch(error) { |
| 684 | case LAPD_ERR_UNKNOWN_TEI: |
| 685 | /* We don't know about this TEI, probably the BSC |
| 686 | * lost local states (it crashed or it was stopped), |
| 687 | * notify the driver to see if it can do anything to |
| 688 | * recover the existing signalling links with the BTS. |
| 689 | */ |
| 690 | e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi); |
| 691 | return -EIO; |
| 692 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 695 | return 0; |
| 696 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 697 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 698 | void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi, |
| 699 | void *rx_cbdata) |
| 700 | { |
| 701 | struct e1inp_ts *e1i_ts = rx_cbdata; |
| 702 | struct msgb *msg = dp->oph.msg; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 703 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 704 | switch (dp->oph.primitive) { |
| 705 | case PRIM_DL_EST: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 706 | 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] | 707 | e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 708 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 709 | case PRIM_DL_REL: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 710 | 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] | 711 | e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 712 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 713 | case PRIM_DL_DATA: |
| 714 | case PRIM_DL_UNIT_DATA: |
| 715 | if (dp->oph.operation == PRIM_OP_INDICATION) { |
| 716 | msg->l2h = msg->l3h; |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 717 | 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] | 718 | osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)), |
| 719 | sapi, tei); |
| 720 | e1inp_rx_ts(e1i_ts, msg, tei, sapi); |
| 721 | return; |
| 722 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 723 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 724 | case PRIM_MDL_ERROR: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 725 | 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] | 726 | break; |
| 727 | default: |
| 728 | printf("ERROR: unknown prim\n"); |
| 729 | break; |
| 730 | } |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 731 | |
| 732 | msgb_free(msg); |
| 733 | |
| 734 | return; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 737 | #define TSX_ALLOC_SIZE 4096 |
| 738 | |
| 739 | /* called by driver if it wants to transmit on a given TS */ |
| 740 | struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts, |
| 741 | struct e1inp_sign_link **sign_link) |
| 742 | { |
| 743 | struct e1inp_sign_link *link; |
| 744 | struct msgb *msg = NULL; |
| 745 | int len; |
| 746 | |
| 747 | switch (e1i_ts->type) { |
| 748 | case E1INP_TS_TYPE_SIGN: |
| 749 | /* FIXME: implement this round robin */ |
| 750 | llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) { |
| 751 | msg = msgb_dequeue(&link->tx_list); |
| 752 | if (msg) { |
| 753 | if (sign_link) |
| 754 | *sign_link = link; |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | break; |
| 759 | case E1INP_TS_TYPE_TRAU: |
| 760 | msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX"); |
| 761 | if (!msg) |
| 762 | return NULL; |
| 763 | len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40); |
Pablo Neira Ayuso | 33f7175 | 2013-07-05 15:44:12 +0200 | [diff] [blame] | 764 | if (len != 40) { |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 765 | 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] | 766 | msgb_free(msg); |
| 767 | return NULL; |
| 768 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 769 | msgb_put(msg, 40); |
| 770 | break; |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 771 | case E1INP_TS_TYPE_RAW: |
| 772 | /* Get msgb from tx_queue */ |
| 773 | msg = msgb_dequeue(&e1i_ts->raw.tx_queue); |
| 774 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 775 | case E1INP_TS_TYPE_HDLC: |
| 776 | /* Get msgb from tx_queue */ |
| 777 | msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue); |
| 778 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 779 | default: |
Harald Welte | b5af099 | 2020-01-12 12:59:52 +0100 | [diff] [blame] | 780 | 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] | 781 | return NULL; |
| 782 | } |
| 783 | return msg; |
| 784 | } |
| 785 | |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 786 | static int e1inp_int_snd_event(struct e1inp_ts *ts, |
| 787 | struct e1inp_sign_link *link, int evt) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 788 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 789 | struct input_signal_data isd; |
Pablo Neira Ayuso | 31fe5f2 | 2011-08-09 23:15:38 +0200 | [diff] [blame] | 790 | isd.line = ts->line; |
Harald Welte | f35d889 | 2016-10-16 15:33:52 +0200 | [diff] [blame] | 791 | isd.ts_nr = ts->num; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 792 | isd.link_type = link->type; |
| 793 | isd.trx = link->trx; |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 794 | isd.tei = link->tei; |
| 795 | isd.sapi = link->sapi; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 796 | |
| 797 | /* report further upwards */ |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 798 | osmo_signal_dispatch(SS_L_INPUT, evt, &isd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 799 | return 0; |
| 800 | } |
| 801 | |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 802 | |
| 803 | /* called by driver in case some kind of link state event */ |
| 804 | int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi) |
| 805 | { |
| 806 | struct e1inp_sign_link *link; |
| 807 | |
| 808 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 809 | if (!link) |
| 810 | return -EINVAL; |
| 811 | return e1inp_int_snd_event(ts, link, evt); |
| 812 | } |
| 813 | |
| 814 | void e1inp_close_socket(struct e1inp_ts *ts, |
| 815 | struct e1inp_sign_link *sign_link, |
| 816 | struct osmo_fd *bfd) |
| 817 | { |
| 818 | e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN); |
| 819 | /* the first e1inp_sign_link_destroy call closes the socket. */ |
| 820 | if (bfd->fd != -1) { |
| 821 | osmo_fd_unregister(bfd); |
| 822 | close(bfd->fd); |
| 823 | bfd->fd = -1; |
| 824 | } |
| 825 | } |
| 826 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 827 | /* register a driver with the E1 core */ |
| 828 | int e1inp_driver_register(struct e1inp_driver *drv) |
| 829 | { |
| 830 | llist_add_tail(&drv->list, &e1inp_driver_list); |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | struct e1inp_driver *e1inp_driver_find(const char *name) |
| 835 | { |
| 836 | struct e1inp_driver *drv; |
| 837 | |
| 838 | llist_for_each_entry(drv, &e1inp_driver_list, list) { |
| 839 | if (!strcasecmp(name, drv->name)) |
| 840 | return drv; |
| 841 | } |
| 842 | return NULL; |
| 843 | } |
| 844 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 845 | int e1inp_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 846 | { |
| 847 | struct input_signal_data isd; |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 848 | int i, rc; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 849 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 850 | e1inp_line_get(line); |
| 851 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 852 | if (line->driver && line->ops && line->driver->line_update) { |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 853 | rc = line->driver->line_update(line); |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 854 | } else |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 855 | rc = 0; |
| 856 | |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 857 | /* Set the PCAP file descriptor for all timeslots that have |
| 858 | * software LAPD instances, to ensure the osmo_lapd_pcap code is |
| 859 | * used to write PCAP files (if requested) */ |
| 860 | for (i = 0; i < ARRAY_SIZE(line->ts); i++) { |
| 861 | struct e1inp_ts *e1i_ts = &line->ts[i]; |
| 862 | if (e1i_ts->lapd) |
| 863 | e1i_ts->lapd->pcap_fd = pcap_fd; |
| 864 | } |
| 865 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 866 | /* Send a signal to anyone who is interested in new lines being |
| 867 | * configured */ |
| 868 | memset(&isd, 0, sizeof(isd)); |
| 869 | isd.line = line; |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 870 | 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] | 871 | |
| 872 | return rc; |
| 873 | } |
| 874 | |
Pablo Neira Ayuso | a20762a | 2011-07-02 19:01:58 +0200 | [diff] [blame] | 875 | static int e1i_sig_cb(unsigned int subsys, unsigned int signal, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 876 | void *handler_data, void *signal_data) |
| 877 | { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 878 | if (subsys != SS_L_GLOBAL || |
| 879 | signal != S_L_GLOBAL_SHUTDOWN) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 880 | return 0; |
| 881 | |
| 882 | if (pcap_fd) { |
| 883 | close(pcap_fd); |
| 884 | pcap_fd = -1; |
| 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
Harald Welte | cac78fe | 2017-05-25 19:13:13 +0200 | [diff] [blame] | 890 | const struct value_string e1inp_signal_names[] = { |
| 891 | { S_L_INP_NONE, "NONE" }, |
| 892 | { S_L_INP_TEI_UP, "TEI-UP" }, |
| 893 | { S_L_INP_TEI_DN, "TEI-DOWN" }, |
| 894 | { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" }, |
| 895 | { S_L_INP_LINE_INIT, "LINE-INIT" }, |
| 896 | { S_L_INP_LINE_ALARM, "LINE-ALARM" }, |
| 897 | { S_L_INP_LINE_NOALARM, "LINE-NOALARM" }, |
| 898 | { 0, NULL } |
| 899 | }; |
| 900 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 901 | void e1inp_misdn_init(void); |
| 902 | void e1inp_dahdi_init(void); |
Harald Welte | c6c7076 | 2019-05-12 21:54:59 +0200 | [diff] [blame] | 903 | void e1inp_e1d_init(void); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 904 | void e1inp_ipaccess_init(void); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 905 | void e1inp_rs232_init(void); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 906 | void e1inp_unixsocket_init(void); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 907 | |
| 908 | void e1inp_init(void) |
| 909 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 910 | tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 911 | tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1, |
| 912 | "e1inp_sign_link"); |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 913 | osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 914 | |
| 915 | e1inp_misdn_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 916 | #ifdef HAVE_DAHDI_USER_H |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 917 | e1inp_dahdi_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 918 | #endif |
Harald Welte | 52b0d45 | 2019-05-12 21:54:26 +0200 | [diff] [blame] | 919 | #ifdef HAVE_E1D |
Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 920 | e1inp_e1d_init(); |
| 921 | #endif |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 922 | e1inp_ipaccess_init(); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 923 | e1inp_rs232_init(); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 924 | e1inp_unixsocket_init(); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 925 | } |