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