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