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] = { |
Harald Welte | dd0c2ef | 2011-08-11 12:54:07 +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] = { |
Harald Welte | dd0c2ef | 2011-08-11 12:54:07 +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] = { |
| 67 | "hdlc.overrun", "HDLC Overrun" |
| 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)); |
| 397 | clone->refcnt = 1; |
| 398 | return clone; |
| 399 | } |
| 400 | |
| 401 | void e1inp_line_get(struct e1inp_line *line) |
| 402 | { |
| 403 | line->refcnt++; |
| 404 | } |
| 405 | |
| 406 | void e1inp_line_put(struct e1inp_line *line) |
| 407 | { |
| 408 | line->refcnt--; |
| 409 | if (line->refcnt == 0) |
| 410 | talloc_free(line); |
| 411 | } |
| 412 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 413 | void |
| 414 | e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops) |
| 415 | { |
| 416 | line->ops = ops; |
| 417 | } |
| 418 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 419 | #if 0 |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 420 | struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 421 | { |
| 422 | struct e1inp_line *line; |
| 423 | int i; |
| 424 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 425 | line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 426 | if (line) |
| 427 | return line; |
| 428 | |
| 429 | line = talloc_zero(tall_e1inp_ctx, struct e1inp_line); |
| 430 | if (!line) |
| 431 | return NULL; |
| 432 | |
| 433 | line->num = e1_nr; |
| 434 | for (i = 0; i < NUM_E1_TS; i++) { |
| 435 | line->ts[i].num = i+1; |
| 436 | line->ts[i].line = line; |
| 437 | } |
| 438 | llist_add_tail(&line->list, &e1inp_line_list); |
| 439 | |
| 440 | return line; |
| 441 | } |
| 442 | #endif |
| 443 | |
| 444 | static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr) |
| 445 | { |
| 446 | struct e1inp_line *e1i_line; |
| 447 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 448 | e1i_line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 449 | if (!e1i_line) |
| 450 | return NULL; |
| 451 | |
| 452 | return &e1i_line->ts[ts_nr-1]; |
| 453 | } |
| 454 | |
| 455 | struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr) |
| 456 | { |
| 457 | struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr); |
| 458 | |
| 459 | if (!e1i_ts) |
| 460 | return NULL; |
| 461 | |
| 462 | return &e1i_ts->trau.mux; |
| 463 | } |
| 464 | |
| 465 | /* Signalling Link */ |
| 466 | |
| 467 | struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i, |
| 468 | uint8_t tei, uint8_t sapi) |
| 469 | { |
| 470 | struct e1inp_sign_link *link; |
| 471 | |
| 472 | llist_for_each_entry(link, &e1i->sign.sign_links, list) { |
| 473 | if (link->sapi == sapi && link->tei == tei) |
| 474 | return link; |
| 475 | } |
| 476 | |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | /* create a new signalling link in a E1 timeslot */ |
| 481 | |
| 482 | struct e1inp_sign_link * |
| 483 | e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type, |
| 484 | struct gsm_bts_trx *trx, uint8_t tei, |
| 485 | uint8_t sapi) |
| 486 | { |
| 487 | struct e1inp_sign_link *link; |
| 488 | |
| 489 | if (ts->type != E1INP_TS_TYPE_SIGN) |
| 490 | return NULL; |
| 491 | |
| 492 | link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link); |
| 493 | if (!link) |
| 494 | return NULL; |
| 495 | |
| 496 | link->ts = ts; |
| 497 | link->type = type; |
| 498 | INIT_LLIST_HEAD(&link->tx_list); |
| 499 | link->trx = trx; |
| 500 | link->tei = tei; |
| 501 | link->sapi = sapi; |
| 502 | |
| 503 | llist_add_tail(&link->list, &ts->sign.sign_links); |
| 504 | |
| 505 | return link; |
| 506 | } |
| 507 | |
| 508 | void e1inp_sign_link_destroy(struct e1inp_sign_link *link) |
| 509 | { |
| 510 | struct msgb *msg; |
| 511 | |
| 512 | llist_del(&link->list); |
| 513 | while (!llist_empty(&link->tx_list)) { |
| 514 | msg = msgb_dequeue(&link->tx_list); |
| 515 | msgb_free(msg); |
| 516 | } |
| 517 | |
| 518 | if (link->ts->type == E1INP_TS_TYPE_SIGN) |
| 519 | osmo_timer_del(&link->ts->sign.tx_timer); |
| 520 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 521 | if (link->ts->line->driver->close) |
Pablo Neira Ayuso | add3ec8 | 2011-07-05 14:45:46 +0200 | [diff] [blame] | 522 | link->ts->line->driver->close(link); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 523 | |
Pablo Neira Ayuso | 6cc3f92 | 2012-08-22 13:57:58 +0200 | [diff] [blame] | 524 | e1inp_line_put(link->ts->line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 525 | talloc_free(link); |
| 526 | } |
| 527 | |
| 528 | /* XXX */ |
| 529 | /* the E1 driver tells us he has received something on a TS */ |
| 530 | int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg, |
| 531 | uint8_t tei, uint8_t sapi) |
| 532 | { |
| 533 | struct e1inp_sign_link *link; |
| 534 | int ret = 0; |
| 535 | |
| 536 | switch (ts->type) { |
| 537 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 538 | /* we only need to write a 'Fake LAPD' packet here, if |
| 539 | * the underlying driver hides LAPD from us. If we use |
| 540 | * the libosmocore LAPD implementation, it will take |
| 541 | * care of writing the _actual_ LAPD packet */ |
| 542 | if (!ts->lapd) |
| 543 | write_pcap_packet(PCAP_INPUT, sapi, tei, msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 544 | /* consult the list of signalling links */ |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 545 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 546 | if (!link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 547 | LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for " |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 548 | "tei %d, sapi %d\n", tei, sapi); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 549 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 550 | return -EINVAL; |
| 551 | } |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 552 | if (!ts->line->ops->sign_link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 553 | LOGP(DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 554 | "no action set for signalling messages.\n"); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 555 | msgb_free(msg); |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 556 | return -ENOENT; |
| 557 | } |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 558 | msg->dst = link; |
| 559 | ts->line->ops->sign_link(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 560 | break; |
| 561 | case E1INP_TS_TYPE_TRAU: |
Pablo Neira Ayuso | 211d2ca | 2011-06-07 17:15:10 +0200 | [diff] [blame] | 562 | ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg)); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 563 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 564 | break; |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 565 | case E1INP_TS_TYPE_RAW: |
| 566 | ts->raw.recv_cb(ts, msg); |
| 567 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 568 | case E1INP_TS_TYPE_HDLC: |
| 569 | ts->hdlc.recv_cb(ts, msg); |
| 570 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 571 | default: |
| 572 | ret = -EINVAL; |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 573 | LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 574 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | |
| 578 | return ret; |
| 579 | } |
| 580 | |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 581 | /*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot |
| 582 | * \param[in] e1i_ts E1 Timeslot data structure |
| 583 | * \param[in] msg Message buffer containing full LAPD message |
| 584 | * |
| 585 | * This is a wrapper around e1inp_rx_ts(), but feeding the incoming |
| 586 | * message first into our LAPD code. This allows a driver to read raw |
| 587 | * (HDLC decoded) data from the timeslot, instead of a LAPD stack |
| 588 | * present in any underlying driver. |
| 589 | */ |
| 590 | int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg) |
| 591 | { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 592 | unsigned int sapi, tei; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 593 | int ret = 0, error = 0; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 594 | |
| 595 | sapi = msg->data[0] >> 2; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 596 | if ((msg->data[0] & 0x1)) |
| 597 | tei = 0; |
| 598 | else |
| 599 | tei = msg->data[1] >> 1; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 600 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 601 | 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] | 602 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 603 | ret = lapd_receive(e1i_ts->lapd, msg, &error); |
| 604 | if (ret < 0) { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 605 | switch(error) { |
| 606 | case LAPD_ERR_UNKNOWN_TEI: |
| 607 | /* We don't know about this TEI, probably the BSC |
| 608 | * lost local states (it crashed or it was stopped), |
| 609 | * notify the driver to see if it can do anything to |
| 610 | * recover the existing signalling links with the BTS. |
| 611 | */ |
| 612 | e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi); |
| 613 | return -EIO; |
| 614 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 617 | return 0; |
| 618 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 619 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 620 | void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi, |
| 621 | void *rx_cbdata) |
| 622 | { |
| 623 | struct e1inp_ts *e1i_ts = rx_cbdata; |
| 624 | struct msgb *msg = dp->oph.msg; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 625 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 626 | switch (dp->oph.primitive) { |
| 627 | case PRIM_DL_EST: |
| 628 | DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei); |
| 629 | e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 630 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 631 | case PRIM_DL_REL: |
| 632 | DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei); |
| 633 | e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 634 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 635 | case PRIM_DL_DATA: |
| 636 | case PRIM_DL_UNIT_DATA: |
| 637 | if (dp->oph.operation == PRIM_OP_INDICATION) { |
| 638 | msg->l2h = msg->l3h; |
| 639 | DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n", |
| 640 | osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)), |
| 641 | sapi, tei); |
| 642 | e1inp_rx_ts(e1i_ts, msg, tei, sapi); |
| 643 | return; |
| 644 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 645 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 646 | case PRIM_MDL_ERROR: |
| 647 | DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 648 | break; |
| 649 | default: |
| 650 | printf("ERROR: unknown prim\n"); |
| 651 | break; |
| 652 | } |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 653 | |
| 654 | msgb_free(msg); |
| 655 | |
| 656 | return; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 659 | #define TSX_ALLOC_SIZE 4096 |
| 660 | |
| 661 | /* called by driver if it wants to transmit on a given TS */ |
| 662 | struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts, |
| 663 | struct e1inp_sign_link **sign_link) |
| 664 | { |
| 665 | struct e1inp_sign_link *link; |
| 666 | struct msgb *msg = NULL; |
| 667 | int len; |
| 668 | |
| 669 | switch (e1i_ts->type) { |
| 670 | case E1INP_TS_TYPE_SIGN: |
| 671 | /* FIXME: implement this round robin */ |
| 672 | llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) { |
| 673 | msg = msgb_dequeue(&link->tx_list); |
| 674 | if (msg) { |
| 675 | if (sign_link) |
| 676 | *sign_link = link; |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | break; |
| 681 | case E1INP_TS_TYPE_TRAU: |
| 682 | msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX"); |
| 683 | if (!msg) |
| 684 | return NULL; |
| 685 | len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40); |
Pablo Neira Ayuso | 33f7175 | 2013-07-05 15:44:12 +0200 | [diff] [blame] | 686 | if (len != 40) { |
| 687 | LOGP(DLMI, LOGL_ERROR, |
| 688 | "cannot transmit, failed to mux\n"); |
| 689 | msgb_free(msg); |
| 690 | return NULL; |
| 691 | } |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 692 | msgb_put(msg, 40); |
| 693 | break; |
Harald Welte | a0108e7 | 2016-07-27 21:44:50 +0200 | [diff] [blame] | 694 | case E1INP_TS_TYPE_RAW: |
| 695 | /* Get msgb from tx_queue */ |
| 696 | msg = msgb_dequeue(&e1i_ts->raw.tx_queue); |
| 697 | break; |
Harald Welte | 7a228eb | 2016-07-28 11:09:31 +0200 | [diff] [blame] | 698 | case E1INP_TS_TYPE_HDLC: |
| 699 | /* Get msgb from tx_queue */ |
| 700 | msg = msgb_dequeue(&e1i_ts->hdlc.tx_queue); |
| 701 | break; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 702 | default: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 703 | 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] | 704 | return NULL; |
| 705 | } |
| 706 | return msg; |
| 707 | } |
| 708 | |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 709 | static int e1inp_int_snd_event(struct e1inp_ts *ts, |
| 710 | struct e1inp_sign_link *link, int evt) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 711 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 712 | struct input_signal_data isd; |
Pablo Neira Ayuso | 31fe5f2 | 2011-08-09 23:15:38 +0200 | [diff] [blame] | 713 | isd.line = ts->line; |
Harald Welte | f35d889 | 2016-10-16 15:33:52 +0200 | [diff] [blame] | 714 | isd.ts_nr = ts->num; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 715 | isd.link_type = link->type; |
| 716 | isd.trx = link->trx; |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 717 | isd.tei = link->tei; |
| 718 | isd.sapi = link->sapi; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 719 | |
| 720 | /* report further upwards */ |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 721 | osmo_signal_dispatch(SS_L_INPUT, evt, &isd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 722 | return 0; |
| 723 | } |
| 724 | |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 725 | |
| 726 | /* called by driver in case some kind of link state event */ |
| 727 | int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi) |
| 728 | { |
| 729 | struct e1inp_sign_link *link; |
| 730 | |
| 731 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 732 | if (!link) |
| 733 | return -EINVAL; |
| 734 | return e1inp_int_snd_event(ts, link, evt); |
| 735 | } |
| 736 | |
| 737 | void e1inp_close_socket(struct e1inp_ts *ts, |
| 738 | struct e1inp_sign_link *sign_link, |
| 739 | struct osmo_fd *bfd) |
| 740 | { |
| 741 | e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN); |
| 742 | /* the first e1inp_sign_link_destroy call closes the socket. */ |
| 743 | if (bfd->fd != -1) { |
| 744 | osmo_fd_unregister(bfd); |
| 745 | close(bfd->fd); |
| 746 | bfd->fd = -1; |
| 747 | } |
| 748 | } |
| 749 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 750 | /* register a driver with the E1 core */ |
| 751 | int e1inp_driver_register(struct e1inp_driver *drv) |
| 752 | { |
| 753 | llist_add_tail(&drv->list, &e1inp_driver_list); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | struct e1inp_driver *e1inp_driver_find(const char *name) |
| 758 | { |
| 759 | struct e1inp_driver *drv; |
| 760 | |
| 761 | llist_for_each_entry(drv, &e1inp_driver_list, list) { |
| 762 | if (!strcasecmp(name, drv->name)) |
| 763 | return drv; |
| 764 | } |
| 765 | return NULL; |
| 766 | } |
| 767 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 768 | int e1inp_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 769 | { |
| 770 | struct input_signal_data isd; |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 771 | int i, rc; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 772 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 773 | e1inp_line_get(line); |
| 774 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 775 | if (line->driver && line->ops && line->driver->line_update) { |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 776 | rc = line->driver->line_update(line); |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 777 | } else |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 778 | rc = 0; |
| 779 | |
Harald Welte | 7f9d851 | 2016-07-04 09:59:46 +0200 | [diff] [blame] | 780 | /* Set the PCAP file descriptor for all timeslots that have |
| 781 | * software LAPD instances, to ensure the osmo_lapd_pcap code is |
| 782 | * used to write PCAP files (if requested) */ |
| 783 | for (i = 0; i < ARRAY_SIZE(line->ts); i++) { |
| 784 | struct e1inp_ts *e1i_ts = &line->ts[i]; |
| 785 | if (e1i_ts->lapd) |
| 786 | e1i_ts->lapd->pcap_fd = pcap_fd; |
| 787 | } |
| 788 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 789 | /* Send a signal to anyone who is interested in new lines being |
| 790 | * configured */ |
| 791 | memset(&isd, 0, sizeof(isd)); |
| 792 | isd.line = line; |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 793 | 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] | 794 | |
| 795 | return rc; |
| 796 | } |
| 797 | |
Pablo Neira Ayuso | a20762a | 2011-07-02 19:01:58 +0200 | [diff] [blame] | 798 | static int e1i_sig_cb(unsigned int subsys, unsigned int signal, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 799 | void *handler_data, void *signal_data) |
| 800 | { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 801 | if (subsys != SS_L_GLOBAL || |
| 802 | signal != S_L_GLOBAL_SHUTDOWN) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 803 | return 0; |
| 804 | |
| 805 | if (pcap_fd) { |
| 806 | close(pcap_fd); |
| 807 | pcap_fd = -1; |
| 808 | } |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
Harald Welte | cac78fe | 2017-05-25 19:13:13 +0200 | [diff] [blame] | 813 | const struct value_string e1inp_signal_names[] = { |
| 814 | { S_L_INP_NONE, "NONE" }, |
| 815 | { S_L_INP_TEI_UP, "TEI-UP" }, |
| 816 | { S_L_INP_TEI_DN, "TEI-DOWN" }, |
| 817 | { S_L_INP_TEI_UNKNOWN, "TEI-UNKNOWN" }, |
| 818 | { S_L_INP_LINE_INIT, "LINE-INIT" }, |
| 819 | { S_L_INP_LINE_ALARM, "LINE-ALARM" }, |
| 820 | { S_L_INP_LINE_NOALARM, "LINE-NOALARM" }, |
| 821 | { 0, NULL } |
| 822 | }; |
| 823 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 824 | void e1inp_misdn_init(void); |
| 825 | void e1inp_dahdi_init(void); |
| 826 | void e1inp_ipaccess_init(void); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 827 | void e1inp_rs232_init(void); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 828 | void e1inp_unixsocket_init(void); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 829 | |
| 830 | void e1inp_init(void) |
| 831 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 832 | tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 833 | tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1, |
| 834 | "e1inp_sign_link"); |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 835 | osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 836 | |
| 837 | e1inp_misdn_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 838 | #ifdef HAVE_DAHDI_USER_H |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 839 | e1inp_dahdi_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 840 | #endif |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 841 | e1inp_ipaccess_init(); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 842 | e1inp_rs232_init(); |
Alexander Couzens | beb10ef | 2016-11-01 22:05:13 +0100 | [diff] [blame] | 843 | e1inp_unixsocket_init(); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 844 | } |