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 | |
| 141 | void e1_set_pcap_fd(int fd) |
| 142 | { |
| 143 | int ret; |
| 144 | struct pcap_hdr header = { |
| 145 | .magic_number = 0xa1b2c3d4, |
| 146 | .version_major = 2, |
| 147 | .version_minor = 4, |
| 148 | .thiszone = 0, |
| 149 | .sigfigs = 0, |
| 150 | .snaplen = 65535, |
| 151 | .network = DLT_LINUX_LAPD, |
| 152 | }; |
| 153 | |
| 154 | pcap_fd = fd; |
| 155 | ret = write(pcap_fd, &header, sizeof(header)); |
| 156 | } |
| 157 | |
| 158 | /* This currently only works for the D-Channel */ |
| 159 | static void write_pcap_packet(int direction, int sapi, int tei, |
| 160 | struct msgb *msg) { |
| 161 | if (pcap_fd < 0) |
| 162 | return; |
| 163 | |
| 164 | int ret; |
| 165 | time_t cur_time; |
| 166 | struct tm *tm; |
| 167 | |
| 168 | struct fake_linux_lapd_header header = { |
| 169 | .pkttype = 4, |
| 170 | .hatype = 0, |
| 171 | .halen = 0, |
| 172 | .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1, |
| 173 | .protocol = ntohs(48), |
| 174 | }; |
| 175 | |
| 176 | struct lapd_header lapd_header = { |
| 177 | .ea1 = 0, |
| 178 | .cr = direction == PCAP_OUTPUT ? 1 : 0, |
| 179 | .sapi = sapi & 0x3F, |
| 180 | .ea2 = 1, |
| 181 | .tei = tei & 0x7F, |
| 182 | .control_foo = 0x03 /* UI */, |
| 183 | }; |
| 184 | |
| 185 | struct pcaprec_hdr payload_header = { |
| 186 | .ts_sec = 0, |
| 187 | .ts_usec = 0, |
| 188 | .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header) |
| 189 | + sizeof(struct lapd_header), |
| 190 | .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header) |
| 191 | + sizeof(struct lapd_header), |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | cur_time = time(NULL); |
| 196 | tm = localtime(&cur_time); |
| 197 | payload_header.ts_sec = mktime(tm); |
| 198 | |
| 199 | ret = write(pcap_fd, &payload_header, sizeof(payload_header)); |
| 200 | ret = write(pcap_fd, &header, sizeof(header)); |
| 201 | ret = write(pcap_fd, &lapd_header, sizeof(lapd_header)); |
| 202 | ret = write(pcap_fd, msg->l2h, msgb_l2len(msg)); |
| 203 | } |
| 204 | |
| 205 | static const char *sign_types[] = { |
| 206 | [E1INP_SIGN_NONE] = "None", |
| 207 | [E1INP_SIGN_OML] = "OML", |
| 208 | [E1INP_SIGN_RSL] = "RSL", |
| 209 | }; |
| 210 | const char *e1inp_signtype_name(enum e1inp_sign_type tp) |
| 211 | { |
| 212 | if (tp >= ARRAY_SIZE(sign_types)) |
| 213 | return "undefined"; |
| 214 | return sign_types[tp]; |
| 215 | } |
| 216 | |
| 217 | static const char *ts_types[] = { |
| 218 | [E1INP_TS_TYPE_NONE] = "None", |
| 219 | [E1INP_TS_TYPE_SIGN] = "Signalling", |
| 220 | [E1INP_TS_TYPE_TRAU] = "TRAU", |
| 221 | }; |
| 222 | |
| 223 | const char *e1inp_tstype_name(enum e1inp_ts_type tp) |
| 224 | { |
| 225 | if (tp >= ARRAY_SIZE(ts_types)) |
| 226 | return "undefined"; |
| 227 | return ts_types[tp]; |
| 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; |
| 235 | ; |
| 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 | |
| 253 | /* dump it */ |
| 254 | write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg); |
| 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 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 298 | struct e1inp_line *e1inp_line_find(uint8_t e1_nr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 299 | { |
| 300 | struct e1inp_line *e1i_line; |
| 301 | |
| 302 | /* iterate over global list of e1 lines */ |
| 303 | llist_for_each_entry(e1i_line, &e1inp_line_list, list) { |
| 304 | if (e1i_line->num == e1_nr) |
| 305 | return e1i_line; |
| 306 | } |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | struct e1inp_line * |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 311 | e1inp_line_create(uint8_t e1_nr, const char *driver_name) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 312 | { |
| 313 | struct e1inp_driver *driver; |
| 314 | struct e1inp_line *line; |
| 315 | int i; |
| 316 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 317 | line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 318 | if (line) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 319 | LOGP(DLINP, LOGL_ERROR, "E1 Line %u already exists\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 320 | e1_nr); |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | driver = e1inp_driver_find(driver_name); |
| 325 | if (!driver) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 326 | LOGP(DLINP, LOGL_ERROR, "No such E1 driver '%s'\n", |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 327 | driver_name); |
| 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | line = talloc_zero(tall_e1inp_ctx, struct e1inp_line); |
| 332 | if (!line) |
| 333 | return NULL; |
| 334 | |
| 335 | line->driver = driver; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 336 | line->num = e1_nr; |
Harald Welte | f2737fc | 2011-08-16 14:30:10 +0200 | [diff] [blame] | 337 | |
| 338 | line->rate_ctr = rate_ctr_group_alloc(line, &e1inp_ctr_g_d, line->num); |
| 339 | |
Harald Welte | c288951 | 2011-09-13 23:49:04 +0100 | [diff] [blame] | 340 | line->num_ts = NUM_E1_TS; |
| 341 | for (i = 0; i < line->num_ts; i++) { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 342 | line->ts[i].num = i+1; |
| 343 | line->ts[i].line = line; |
| 344 | } |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 345 | line->refcnt++; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 346 | llist_add_tail(&line->list, &e1inp_line_list); |
| 347 | |
| 348 | return line; |
| 349 | } |
| 350 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 351 | struct e1inp_line * |
| 352 | e1inp_line_clone(void *ctx, struct e1inp_line *line) |
| 353 | { |
| 354 | struct e1inp_line *clone; |
| 355 | |
| 356 | /* clone virtual E1 line for this new OML link. */ |
| 357 | clone = talloc_zero(ctx, struct e1inp_line); |
| 358 | if (clone == NULL) |
| 359 | return NULL; |
| 360 | |
| 361 | memcpy(clone, line, sizeof(struct e1inp_line)); |
| 362 | clone->refcnt = 1; |
| 363 | return clone; |
| 364 | } |
| 365 | |
| 366 | void e1inp_line_get(struct e1inp_line *line) |
| 367 | { |
| 368 | line->refcnt++; |
| 369 | } |
| 370 | |
| 371 | void e1inp_line_put(struct e1inp_line *line) |
| 372 | { |
| 373 | line->refcnt--; |
| 374 | if (line->refcnt == 0) |
| 375 | talloc_free(line); |
| 376 | } |
| 377 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 378 | void |
| 379 | e1inp_line_bind_ops(struct e1inp_line *line, const struct e1inp_line_ops *ops) |
| 380 | { |
| 381 | line->ops = ops; |
| 382 | } |
| 383 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 384 | #if 0 |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 385 | struct e1inp_line *e1inp_line_find_create(uint8_t e1_nr) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 386 | { |
| 387 | struct e1inp_line *line; |
| 388 | int i; |
| 389 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 390 | line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 391 | if (line) |
| 392 | return line; |
| 393 | |
| 394 | line = talloc_zero(tall_e1inp_ctx, struct e1inp_line); |
| 395 | if (!line) |
| 396 | return NULL; |
| 397 | |
| 398 | line->num = e1_nr; |
| 399 | for (i = 0; i < NUM_E1_TS; i++) { |
| 400 | line->ts[i].num = i+1; |
| 401 | line->ts[i].line = line; |
| 402 | } |
| 403 | llist_add_tail(&line->list, &e1inp_line_list); |
| 404 | |
| 405 | return line; |
| 406 | } |
| 407 | #endif |
| 408 | |
| 409 | static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr) |
| 410 | { |
| 411 | struct e1inp_line *e1i_line; |
| 412 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 413 | e1i_line = e1inp_line_find(e1_nr); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 414 | if (!e1i_line) |
| 415 | return NULL; |
| 416 | |
| 417 | return &e1i_line->ts[ts_nr-1]; |
| 418 | } |
| 419 | |
| 420 | struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr) |
| 421 | { |
| 422 | struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr); |
| 423 | |
| 424 | if (!e1i_ts) |
| 425 | return NULL; |
| 426 | |
| 427 | return &e1i_ts->trau.mux; |
| 428 | } |
| 429 | |
| 430 | /* Signalling Link */ |
| 431 | |
| 432 | struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i, |
| 433 | uint8_t tei, uint8_t sapi) |
| 434 | { |
| 435 | struct e1inp_sign_link *link; |
| 436 | |
| 437 | llist_for_each_entry(link, &e1i->sign.sign_links, list) { |
| 438 | if (link->sapi == sapi && link->tei == tei) |
| 439 | return link; |
| 440 | } |
| 441 | |
| 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | /* create a new signalling link in a E1 timeslot */ |
| 446 | |
| 447 | struct e1inp_sign_link * |
| 448 | e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type, |
| 449 | struct gsm_bts_trx *trx, uint8_t tei, |
| 450 | uint8_t sapi) |
| 451 | { |
| 452 | struct e1inp_sign_link *link; |
| 453 | |
| 454 | if (ts->type != E1INP_TS_TYPE_SIGN) |
| 455 | return NULL; |
| 456 | |
| 457 | link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link); |
| 458 | if (!link) |
| 459 | return NULL; |
| 460 | |
| 461 | link->ts = ts; |
| 462 | link->type = type; |
| 463 | INIT_LLIST_HEAD(&link->tx_list); |
| 464 | link->trx = trx; |
| 465 | link->tei = tei; |
| 466 | link->sapi = sapi; |
| 467 | |
| 468 | llist_add_tail(&link->list, &ts->sign.sign_links); |
| 469 | |
| 470 | return link; |
| 471 | } |
| 472 | |
| 473 | void e1inp_sign_link_destroy(struct e1inp_sign_link *link) |
| 474 | { |
| 475 | struct msgb *msg; |
| 476 | |
| 477 | llist_del(&link->list); |
| 478 | while (!llist_empty(&link->tx_list)) { |
| 479 | msg = msgb_dequeue(&link->tx_list); |
| 480 | msgb_free(msg); |
| 481 | } |
| 482 | |
| 483 | if (link->ts->type == E1INP_TS_TYPE_SIGN) |
| 484 | osmo_timer_del(&link->ts->sign.tx_timer); |
| 485 | |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 486 | if (link->ts->line->driver->close) |
Pablo Neira Ayuso | add3ec8 | 2011-07-05 14:45:46 +0200 | [diff] [blame] | 487 | link->ts->line->driver->close(link); |
Pablo Neira Ayuso | c9c4fd3 | 2011-06-30 12:19:42 +0200 | [diff] [blame] | 488 | |
Pablo Neira Ayuso | 6cc3f92 | 2012-08-22 13:57:58 +0200 | [diff] [blame] | 489 | e1inp_line_put(link->ts->line); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 490 | talloc_free(link); |
| 491 | } |
| 492 | |
| 493 | /* XXX */ |
| 494 | /* the E1 driver tells us he has received something on a TS */ |
| 495 | int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg, |
| 496 | uint8_t tei, uint8_t sapi) |
| 497 | { |
| 498 | struct e1inp_sign_link *link; |
| 499 | int ret = 0; |
| 500 | |
| 501 | switch (ts->type) { |
| 502 | case E1INP_TS_TYPE_SIGN: |
| 503 | /* consult the list of signalling links */ |
| 504 | write_pcap_packet(PCAP_INPUT, sapi, tei, msg); |
| 505 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 506 | if (!link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 507 | LOGP(DLMI, LOGL_ERROR, "didn't find signalling link for " |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 508 | "tei %d, sapi %d\n", tei, sapi); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 509 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 510 | return -EINVAL; |
| 511 | } |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 512 | if (!ts->line->ops->sign_link) { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 513 | LOGP(DLINP, LOGL_ERROR, "Fix your application, " |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 514 | "no action set for signalling messages.\n"); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 515 | msgb_free(msg); |
Pablo Neira Ayuso | 5a4b7c5 | 2011-06-07 14:07:48 +0200 | [diff] [blame] | 516 | return -ENOENT; |
| 517 | } |
Pablo Neira Ayuso | dbd82fb | 2011-07-05 15:29:23 +0200 | [diff] [blame] | 518 | msg->dst = link; |
| 519 | ts->line->ops->sign_link(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 520 | break; |
| 521 | case E1INP_TS_TYPE_TRAU: |
Pablo Neira Ayuso | 211d2ca | 2011-06-07 17:15:10 +0200 | [diff] [blame] | 522 | ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg)); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 523 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 524 | break; |
| 525 | default: |
| 526 | ret = -EINVAL; |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 527 | LOGP(DLMI, LOGL_ERROR, "unknown TS type %u\n", ts->type); |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 528 | msgb_free(msg); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 529 | break; |
| 530 | } |
| 531 | |
| 532 | return ret; |
| 533 | } |
| 534 | |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 535 | /*! \brief Receive some data from the L1/HDLC into LAPD of a timeslot |
| 536 | * \param[in] e1i_ts E1 Timeslot data structure |
| 537 | * \param[in] msg Message buffer containing full LAPD message |
| 538 | * |
| 539 | * This is a wrapper around e1inp_rx_ts(), but feeding the incoming |
| 540 | * message first into our LAPD code. This allows a driver to read raw |
| 541 | * (HDLC decoded) data from the timeslot, instead of a LAPD stack |
| 542 | * present in any underlying driver. |
| 543 | */ |
| 544 | int e1inp_rx_ts_lapd(struct e1inp_ts *e1i_ts, struct msgb *msg) |
| 545 | { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 546 | unsigned int sapi, tei; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 547 | int ret = 0, error = 0; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 548 | |
| 549 | sapi = msg->data[0] >> 2; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 550 | if ((msg->data[0] & 0x1)) |
| 551 | tei = 0; |
| 552 | else |
| 553 | tei = msg->data[1] >> 1; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 554 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 555 | 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] | 556 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 557 | ret = lapd_receive(e1i_ts->lapd, msg, &error); |
| 558 | if (ret < 0) { |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 559 | switch(error) { |
| 560 | case LAPD_ERR_UNKNOWN_TEI: |
| 561 | /* We don't know about this TEI, probably the BSC |
| 562 | * lost local states (it crashed or it was stopped), |
| 563 | * notify the driver to see if it can do anything to |
| 564 | * recover the existing signalling links with the BTS. |
| 565 | */ |
| 566 | e1inp_event(e1i_ts, S_L_INP_TEI_UNKNOWN, tei, sapi); |
| 567 | return -EIO; |
| 568 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 571 | return 0; |
| 572 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 573 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 574 | void e1inp_dlsap_up(struct osmo_dlsap_prim *dp, uint8_t tei, uint8_t sapi, |
| 575 | void *rx_cbdata) |
| 576 | { |
| 577 | struct e1inp_ts *e1i_ts = rx_cbdata; |
| 578 | struct msgb *msg = dp->oph.msg; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 579 | |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 580 | switch (dp->oph.primitive) { |
| 581 | case PRIM_DL_EST: |
| 582 | DEBUGP(DLMI, "DL_EST: sapi(%d) tei(%d)\n", sapi, tei); |
| 583 | e1inp_event(e1i_ts, S_L_INP_TEI_UP, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 584 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 585 | case PRIM_DL_REL: |
| 586 | DEBUGP(DLMI, "DL_REL: sapi(%d) tei(%d)\n", sapi, tei); |
| 587 | e1inp_event(e1i_ts, S_L_INP_TEI_DN, tei, sapi); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 588 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 589 | case PRIM_DL_DATA: |
| 590 | case PRIM_DL_UNIT_DATA: |
| 591 | if (dp->oph.operation == PRIM_OP_INDICATION) { |
| 592 | msg->l2h = msg->l3h; |
| 593 | DEBUGP(DLMI, "RX: %s sapi=%d tei=%d\n", |
| 594 | osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)), |
| 595 | sapi, tei); |
| 596 | e1inp_rx_ts(e1i_ts, msg, tei, sapi); |
| 597 | return; |
| 598 | } |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 599 | break; |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 600 | case PRIM_MDL_ERROR: |
| 601 | DEBUGP(DLMI, "MDL_EERROR: cause(%d)\n", dp->u.error_ind.cause); |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 602 | break; |
| 603 | default: |
| 604 | printf("ERROR: unknown prim\n"); |
| 605 | break; |
| 606 | } |
Andreas Eversberg | a7ff001 | 2011-09-26 11:29:30 +0200 | [diff] [blame] | 607 | |
| 608 | msgb_free(msg); |
| 609 | |
| 610 | return; |
Harald Welte | fd44a5f | 2011-08-21 00:48:54 +0200 | [diff] [blame] | 611 | } |
| 612 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 613 | #define TSX_ALLOC_SIZE 4096 |
| 614 | |
| 615 | /* called by driver if it wants to transmit on a given TS */ |
| 616 | struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts, |
| 617 | struct e1inp_sign_link **sign_link) |
| 618 | { |
| 619 | struct e1inp_sign_link *link; |
| 620 | struct msgb *msg = NULL; |
| 621 | int len; |
| 622 | |
| 623 | switch (e1i_ts->type) { |
| 624 | case E1INP_TS_TYPE_SIGN: |
| 625 | /* FIXME: implement this round robin */ |
| 626 | llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) { |
| 627 | msg = msgb_dequeue(&link->tx_list); |
| 628 | if (msg) { |
| 629 | if (sign_link) |
| 630 | *sign_link = link; |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | break; |
| 635 | case E1INP_TS_TYPE_TRAU: |
| 636 | msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX"); |
| 637 | if (!msg) |
| 638 | return NULL; |
| 639 | len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40); |
| 640 | msgb_put(msg, 40); |
| 641 | break; |
| 642 | default: |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 643 | 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] | 644 | return NULL; |
| 645 | } |
| 646 | return msg; |
| 647 | } |
| 648 | |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 649 | static int e1inp_int_snd_event(struct e1inp_ts *ts, |
| 650 | struct e1inp_sign_link *link, int evt) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 651 | { |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 652 | struct input_signal_data isd; |
Pablo Neira Ayuso | 31fe5f2 | 2011-08-09 23:15:38 +0200 | [diff] [blame] | 653 | isd.line = ts->line; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 654 | isd.link_type = link->type; |
| 655 | isd.trx = link->trx; |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 656 | isd.tei = link->tei; |
| 657 | isd.sapi = link->sapi; |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 658 | |
| 659 | /* report further upwards */ |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 660 | osmo_signal_dispatch(SS_L_INPUT, evt, &isd); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 661 | return 0; |
| 662 | } |
| 663 | |
Holger Hans Peter Freyther | 55467f0 | 2011-12-28 19:47:07 +0100 | [diff] [blame] | 664 | |
| 665 | /* called by driver in case some kind of link state event */ |
| 666 | int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi) |
| 667 | { |
| 668 | struct e1inp_sign_link *link; |
| 669 | |
| 670 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 671 | if (!link) |
| 672 | return -EINVAL; |
| 673 | return e1inp_int_snd_event(ts, link, evt); |
| 674 | } |
| 675 | |
| 676 | void e1inp_close_socket(struct e1inp_ts *ts, |
| 677 | struct e1inp_sign_link *sign_link, |
| 678 | struct osmo_fd *bfd) |
| 679 | { |
| 680 | e1inp_int_snd_event(ts, sign_link, S_L_INP_TEI_DN); |
| 681 | /* the first e1inp_sign_link_destroy call closes the socket. */ |
| 682 | if (bfd->fd != -1) { |
| 683 | osmo_fd_unregister(bfd); |
| 684 | close(bfd->fd); |
| 685 | bfd->fd = -1; |
| 686 | } |
| 687 | } |
| 688 | |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 689 | /* register a driver with the E1 core */ |
| 690 | int e1inp_driver_register(struct e1inp_driver *drv) |
| 691 | { |
| 692 | llist_add_tail(&drv->list, &e1inp_driver_list); |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | struct e1inp_driver *e1inp_driver_find(const char *name) |
| 697 | { |
| 698 | struct e1inp_driver *drv; |
| 699 | |
| 700 | llist_for_each_entry(drv, &e1inp_driver_list, list) { |
| 701 | if (!strcasecmp(name, drv->name)) |
| 702 | return drv; |
| 703 | } |
| 704 | return NULL; |
| 705 | } |
| 706 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 707 | int e1inp_line_update(struct e1inp_line *line) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 708 | { |
| 709 | struct input_signal_data isd; |
| 710 | int rc; |
| 711 | |
Pablo Neira Ayuso | 3832c4f | 2011-07-07 17:47:26 +0200 | [diff] [blame] | 712 | e1inp_line_get(line); |
| 713 | |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 714 | if (line->driver && line->ops && line->driver->line_update) { |
Pablo Neira Ayuso | 4e862cb | 2011-08-19 18:43:38 +0200 | [diff] [blame] | 715 | rc = line->driver->line_update(line); |
Pablo Neira Ayuso | f163d23 | 2011-06-25 18:42:55 +0200 | [diff] [blame] | 716 | } else |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 717 | rc = 0; |
| 718 | |
| 719 | /* Send a signal to anyone who is interested in new lines being |
| 720 | * configured */ |
| 721 | memset(&isd, 0, sizeof(isd)); |
| 722 | isd.line = line; |
Pablo Neira Ayuso | de66891 | 2011-08-16 17:26:23 +0200 | [diff] [blame] | 723 | 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] | 724 | |
| 725 | return rc; |
| 726 | } |
| 727 | |
Pablo Neira Ayuso | a20762a | 2011-07-02 19:01:58 +0200 | [diff] [blame] | 728 | static int e1i_sig_cb(unsigned int subsys, unsigned int signal, |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 729 | void *handler_data, void *signal_data) |
| 730 | { |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 731 | if (subsys != SS_L_GLOBAL || |
| 732 | signal != S_L_GLOBAL_SHUTDOWN) |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 733 | return 0; |
| 734 | |
| 735 | if (pcap_fd) { |
| 736 | close(pcap_fd); |
| 737 | pcap_fd = -1; |
| 738 | } |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | void e1inp_misdn_init(void); |
| 744 | void e1inp_dahdi_init(void); |
| 745 | void e1inp_ipaccess_init(void); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 746 | void e1inp_rs232_init(void); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 747 | |
| 748 | void e1inp_init(void) |
| 749 | { |
Pablo Neira Ayuso | 54b4979 | 2011-06-07 12:15:26 +0200 | [diff] [blame] | 750 | tall_e1inp_ctx = talloc_named_const(libosmo_abis_ctx, 1, "e1inp"); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 751 | tall_sigl_ctx = talloc_named_const(tall_e1inp_ctx, 1, |
| 752 | "e1inp_sign_link"); |
Harald Welte | cc2241b | 2011-07-19 16:06:06 +0200 | [diff] [blame] | 753 | osmo_signal_register_handler(SS_L_GLOBAL, e1i_sig_cb, NULL); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 754 | |
| 755 | e1inp_misdn_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 756 | #ifdef HAVE_DAHDI_USER_H |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 757 | e1inp_dahdi_init(); |
Harald Welte | 3bc7885 | 2011-08-24 08:32:38 +0200 | [diff] [blame] | 758 | #endif |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 759 | e1inp_ipaccess_init(); |
Pablo Neira Ayuso | 7e0d006 | 2011-08-19 11:36:15 +0200 | [diff] [blame] | 760 | e1inp_rs232_init(); |
Pablo Neira Ayuso | 0ba77d5 | 2011-06-05 18:32:44 +0200 | [diff] [blame] | 761 | } |