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