Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <errno.h> |
| 26 | #include <string.h> |
| 27 | #include <time.h> |
| 28 | #include <sys/fcntl.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <arpa/inet.h> |
| 33 | #include <mISDNif.h> |
| 34 | |
| 35 | //#define AF_COMPATIBILITY_FUNC |
| 36 | //#include <compat_af_isdn.h> |
Holger Freyther | 66e092b | 2009-03-31 12:13:50 +0000 | [diff] [blame] | 37 | #ifndef AF_ISDN |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 38 | #define AF_ISDN 34 |
| 39 | #define PF_ISDN AF_ISDN |
Holger Freyther | 66e092b | 2009-03-31 12:13:50 +0000 | [diff] [blame] | 40 | #endif |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 41 | |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 42 | #include <osmocore/select.h> |
| 43 | #include <osmocore/msgb.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 44 | #include <openbsc/debug.h> |
| 45 | #include <openbsc/gsm_data.h> |
| 46 | #include <openbsc/e1_input.h> |
| 47 | #include <openbsc/abis_nm.h> |
| 48 | #include <openbsc/abis_rsl.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 49 | #include <osmocore/linuxlist.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 50 | #include <openbsc/subchan_demux.h> |
Harald Welte | 45b407a | 2009-05-23 15:51:12 +0000 | [diff] [blame] | 51 | #include <openbsc/trau_frame.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 52 | #include <openbsc/trau_mux.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 53 | #include <osmocore/talloc.h> |
Harald Welte | 29b9cf8 | 2009-11-30 19:43:54 +0100 | [diff] [blame] | 54 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 34e9749 | 2009-08-10 07:54:02 +0200 | [diff] [blame] | 55 | #include <openbsc/misdn.h> |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 56 | |
Harald Welte | ca17ef8 | 2011-02-05 15:13:27 +0100 | [diff] [blame] | 57 | #include "../bscconfig.h" |
| 58 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 59 | #define NUM_E1_TS 32 |
| 60 | |
| 61 | /* list of all E1 drivers */ |
Harald Welte | 44d542e | 2009-03-10 18:24:35 +0000 | [diff] [blame] | 62 | LLIST_HEAD(e1inp_driver_list); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 63 | |
| 64 | /* list of all E1 lines */ |
Harald Welte | 44d542e | 2009-03-10 18:24:35 +0000 | [diff] [blame] | 65 | LLIST_HEAD(e1inp_line_list); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 66 | |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 67 | static void *tall_sigl_ctx; |
| 68 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 69 | /* |
| 70 | * pcap writing of the misdn load |
| 71 | * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat |
| 72 | */ |
| 73 | #define DLT_LINUX_LAPD 177 |
| 74 | #define PCAP_INPUT 0 |
| 75 | #define PCAP_OUTPUT 1 |
| 76 | |
| 77 | struct pcap_hdr { |
| 78 | u_int32_t magic_number; |
| 79 | u_int16_t version_major; |
| 80 | u_int16_t version_minor; |
| 81 | int32_t thiszone; |
| 82 | u_int32_t sigfigs; |
| 83 | u_int32_t snaplen; |
| 84 | u_int32_t network; |
| 85 | } __attribute__((packed)); |
| 86 | |
| 87 | struct pcaprec_hdr { |
| 88 | u_int32_t ts_sec; |
| 89 | u_int32_t ts_usec; |
| 90 | u_int32_t incl_len; |
| 91 | u_int32_t orig_len; |
| 92 | } __attribute__((packed)); |
| 93 | |
| 94 | struct fake_linux_lapd_header { |
| 95 | u_int16_t pkttype; |
| 96 | u_int16_t hatype; |
| 97 | u_int16_t halen; |
| 98 | u_int64_t addr; |
| 99 | int16_t protocol; |
| 100 | } __attribute__((packed)); |
| 101 | |
| 102 | struct lapd_header { |
| 103 | u_int8_t ea1 : 1; |
| 104 | u_int8_t cr : 1; |
| 105 | u_int8_t sapi : 6; |
| 106 | u_int8_t ea2 : 1; |
| 107 | u_int8_t tei : 7; |
| 108 | u_int8_t control_foo; /* fake UM's ... */ |
| 109 | } __attribute__((packed)); |
| 110 | |
Holger Hans Peter Freyther | 0b369c5 | 2010-11-09 23:10:16 +0100 | [diff] [blame] | 111 | static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset); |
| 112 | static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset); |
| 113 | static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset); |
| 114 | static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset); |
| 115 | static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | static int pcap_fd = -1; |
| 119 | |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 120 | void e1_set_pcap_fd(int fd) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 121 | { |
| 122 | int ret; |
| 123 | struct pcap_hdr header = { |
| 124 | .magic_number = 0xa1b2c3d4, |
| 125 | .version_major = 2, |
| 126 | .version_minor = 4, |
| 127 | .thiszone = 0, |
| 128 | .sigfigs = 0, |
| 129 | .snaplen = 65535, |
| 130 | .network = DLT_LINUX_LAPD, |
| 131 | }; |
| 132 | |
| 133 | pcap_fd = fd; |
| 134 | ret = write(pcap_fd, &header, sizeof(header)); |
| 135 | } |
| 136 | |
| 137 | /* This currently only works for the D-Channel */ |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 138 | static void write_pcap_packet(int direction, int sapi, int tei, |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 139 | struct msgb *msg) { |
| 140 | if (pcap_fd < 0) |
| 141 | return; |
| 142 | |
| 143 | int ret; |
| 144 | time_t cur_time; |
| 145 | struct tm *tm; |
| 146 | |
| 147 | struct fake_linux_lapd_header header = { |
| 148 | .pkttype = 4, |
| 149 | .hatype = 0, |
| 150 | .halen = 0, |
| 151 | .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1, |
| 152 | .protocol = ntohs(48), |
| 153 | }; |
| 154 | |
| 155 | struct lapd_header lapd_header = { |
| 156 | .ea1 = 0, |
| 157 | .cr = direction == PCAP_OUTPUT ? 1 : 0, |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 158 | .sapi = sapi & 0x3F, |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 159 | .ea2 = 1, |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 160 | .tei = tei & 0x7F, |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 161 | .control_foo = 0x03 /* UI */, |
| 162 | }; |
| 163 | |
| 164 | struct pcaprec_hdr payload_header = { |
| 165 | .ts_sec = 0, |
| 166 | .ts_usec = 0, |
Harald Welte | 95aa5c4 | 2011-02-12 14:00:23 +0100 | [diff] [blame] | 167 | .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header) |
| 168 | + sizeof(struct lapd_header), |
| 169 | .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header) |
| 170 | + sizeof(struct lapd_header), |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | |
| 174 | cur_time = time(NULL); |
| 175 | tm = localtime(&cur_time); |
| 176 | payload_header.ts_sec = mktime(tm); |
| 177 | |
| 178 | ret = write(pcap_fd, &payload_header, sizeof(payload_header)); |
| 179 | ret = write(pcap_fd, &header, sizeof(header)); |
| 180 | ret = write(pcap_fd, &lapd_header, sizeof(lapd_header)); |
Harald Welte | 95aa5c4 | 2011-02-12 14:00:23 +0100 | [diff] [blame] | 181 | ret = write(pcap_fd, msg->l2h, msgb_l2len(msg)); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 182 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 183 | |
Harald Welte | d256d4f | 2009-03-10 19:44:48 +0000 | [diff] [blame] | 184 | static const char *sign_types[] = { |
| 185 | [E1INP_SIGN_NONE] = "None", |
| 186 | [E1INP_SIGN_OML] = "OML", |
| 187 | [E1INP_SIGN_RSL] = "RSL", |
| 188 | }; |
| 189 | const char *e1inp_signtype_name(enum e1inp_sign_type tp) |
| 190 | { |
| 191 | if (tp >= ARRAY_SIZE(sign_types)) |
| 192 | return "undefined"; |
| 193 | return sign_types[tp]; |
| 194 | } |
| 195 | |
| 196 | static const char *ts_types[] = { |
| 197 | [E1INP_TS_TYPE_NONE] = "None", |
| 198 | [E1INP_TS_TYPE_SIGN] = "Signalling", |
| 199 | [E1INP_TS_TYPE_TRAU] = "TRAU", |
| 200 | }; |
| 201 | |
| 202 | const char *e1inp_tstype_name(enum e1inp_ts_type tp) |
| 203 | { |
| 204 | if (tp >= ARRAY_SIZE(ts_types)) |
| 205 | return "undefined"; |
| 206 | return ts_types[tp]; |
| 207 | } |
| 208 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 209 | /* callback when a TRAU frame was received */ |
| 210 | static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len, |
| 211 | void *_priv) |
| 212 | { |
| 213 | struct e1inp_ts *e1i_ts = _priv; |
| 214 | struct gsm_e1_subslot src_ss; |
| 215 | |
| 216 | src_ss.e1_nr = e1i_ts->line->num; |
| 217 | src_ss.e1_ts = e1i_ts->num; |
| 218 | src_ss.e1_ts_ss = ch; |
| 219 | |
| 220 | return trau_mux_input(&src_ss, data, len); |
| 221 | } |
| 222 | |
| 223 | int abis_rsl_sendmsg(struct msgb *msg) |
| 224 | { |
| 225 | struct e1inp_sign_link *sign_link; |
| 226 | struct e1inp_driver *e1inp_driver; |
Harald Welte | f55b49f | 2009-05-23 06:20:41 +0000 | [diff] [blame] | 227 | struct e1inp_ts *e1i_ts; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 228 | |
| 229 | msg->l2h = msg->data; |
| 230 | |
Harald Welte (local) | 5fe3318 | 2009-12-28 17:05:43 +0100 | [diff] [blame] | 231 | if (!msg->trx) { |
| 232 | LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n", |
| 233 | hexdump(msg->data, msg->len)); |
Harald Welte | eab3335 | 2009-06-27 03:09:08 +0200 | [diff] [blame] | 234 | talloc_free(msg); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 235 | return -EINVAL; |
Harald Welte (local) | 5fe3318 | 2009-12-28 17:05:43 +0100 | [diff] [blame] | 236 | } else if (!msg->trx->rsl_link) { |
| 237 | LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n", |
| 238 | hexdump(msg->data, msg->len)); |
| 239 | talloc_free(msg); |
| 240 | return -EIO; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | sign_link = msg->trx->rsl_link; |
Harald Welte | f55b49f | 2009-05-23 06:20:41 +0000 | [diff] [blame] | 244 | e1i_ts = sign_link->ts; |
| 245 | if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) { |
| 246 | /* notify the driver we have something to write */ |
| 247 | e1inp_driver = sign_link->ts->line->driver; |
| 248 | e1inp_driver->want_write(e1i_ts); |
| 249 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 250 | msgb_enqueue(&sign_link->tx_list, msg); |
| 251 | |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 252 | /* dump it */ |
| 253 | write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg); |
| 254 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | int _abis_nm_sendmsg(struct msgb *msg) |
| 259 | { |
| 260 | struct e1inp_sign_link *sign_link; |
| 261 | struct e1inp_driver *e1inp_driver; |
Harald Welte | f55b49f | 2009-05-23 06:20:41 +0000 | [diff] [blame] | 262 | struct e1inp_ts *e1i_ts; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 263 | |
| 264 | msg->l2h = msg->data; |
| 265 | |
| 266 | if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) { |
Harald Welte | f933de9 | 2011-02-05 20:18:18 +0100 | [diff] [blame] | 267 | LOGP(DNM, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n"); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | } |
Holger Freyther | fb3f519 | 2009-02-09 23:09:15 +0000 | [diff] [blame] | 270 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 271 | sign_link = msg->trx->bts->oml_link; |
Harald Welte | f55b49f | 2009-05-23 06:20:41 +0000 | [diff] [blame] | 272 | e1i_ts = sign_link->ts; |
| 273 | if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) { |
| 274 | /* notify the driver we have something to write */ |
| 275 | e1inp_driver = sign_link->ts->line->driver; |
| 276 | e1inp_driver->want_write(e1i_ts); |
| 277 | } |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 278 | msgb_enqueue(&sign_link->tx_list, msg); |
| 279 | |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 280 | /* dump it */ |
| 281 | write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg); |
| 282 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* Timeslot */ |
| 287 | |
| 288 | /* configure and initialize one e1inp_ts */ |
| 289 | int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line, |
| 290 | enum e1inp_ts_type type) |
| 291 | { |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 292 | if (ts->type == type && ts->line && line) |
| 293 | return 0; |
| 294 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 295 | ts->type = type; |
| 296 | ts->line = line; |
| 297 | |
| 298 | switch (type) { |
| 299 | case E1INP_TS_TYPE_SIGN: |
Holger Hans Peter Freyther | 91f587e | 2011-01-16 18:12:13 +0100 | [diff] [blame] | 300 | if (line && line->driver) |
Holger Hans Peter Freyther | d85642a | 2010-04-26 16:02:04 +0800 | [diff] [blame] | 301 | ts->sign.delay = line->driver->default_delay; |
| 302 | else |
| 303 | ts->sign.delay = 100000; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 304 | INIT_LLIST_HEAD(&ts->sign.sign_links); |
| 305 | break; |
| 306 | case E1INP_TS_TYPE_TRAU: |
| 307 | subchan_mux_init(&ts->trau.mux); |
| 308 | ts->trau.demux.out_cb = subch_cb; |
| 309 | ts->trau.demux.data = ts; |
| 310 | subch_demux_init(&ts->trau.demux); |
| 311 | break; |
| 312 | default: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 313 | LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n", |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 314 | ts->type); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 320 | struct e1inp_line *e1inp_line_get(u_int8_t e1_nr) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 321 | { |
| 322 | struct e1inp_line *e1i_line; |
| 323 | |
| 324 | /* iterate over global list of e1 lines */ |
Harald Welte | 44d542e | 2009-03-10 18:24:35 +0000 | [diff] [blame] | 325 | llist_for_each_entry(e1i_line, &e1inp_line_list, list) { |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 326 | if (e1i_line->num == e1_nr) |
| 327 | return e1i_line; |
| 328 | } |
| 329 | return NULL; |
| 330 | } |
| 331 | |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 332 | struct e1inp_line *e1inp_line_create(u_int8_t e1_nr, const char *driver_name) |
| 333 | { |
| 334 | struct e1inp_driver *driver; |
| 335 | struct e1inp_line *line; |
| 336 | int i; |
| 337 | |
| 338 | line = e1inp_line_get(e1_nr); |
Harald Welte | 07bb0da | 2011-02-05 15:57:42 +0100 | [diff] [blame] | 339 | if (line) { |
| 340 | LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n", |
| 341 | e1_nr); |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 342 | return NULL; |
Harald Welte | 07bb0da | 2011-02-05 15:57:42 +0100 | [diff] [blame] | 343 | } |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 344 | |
| 345 | driver = e1inp_driver_find(driver_name); |
Harald Welte | 07bb0da | 2011-02-05 15:57:42 +0100 | [diff] [blame] | 346 | if (!driver) { |
| 347 | LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n", |
| 348 | driver_name); |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 349 | return NULL; |
Harald Welte | 07bb0da | 2011-02-05 15:57:42 +0100 | [diff] [blame] | 350 | } |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 351 | |
| 352 | line = talloc_zero(tall_bsc_ctx, struct e1inp_line); |
| 353 | if (!line) |
| 354 | return NULL; |
| 355 | |
| 356 | line->driver = driver; |
| 357 | |
| 358 | line->num = e1_nr; |
| 359 | for (i = 0; i < NUM_E1_TS; i++) { |
| 360 | line->ts[i].num = i+1; |
| 361 | line->ts[i].line = line; |
| 362 | } |
| 363 | llist_add_tail(&line->list, &e1inp_line_list); |
| 364 | |
| 365 | return line; |
| 366 | } |
| 367 | |
| 368 | #if 0 |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 369 | struct e1inp_line *e1inp_line_get_create(u_int8_t e1_nr) |
| 370 | { |
| 371 | struct e1inp_line *line; |
| 372 | int i; |
| 373 | |
| 374 | line = e1inp_line_get(e1_nr); |
| 375 | if (line) |
| 376 | return line; |
| 377 | |
| 378 | line = talloc_zero(tall_bsc_ctx, struct e1inp_line); |
| 379 | if (!line) |
| 380 | return NULL; |
| 381 | |
| 382 | line->num = e1_nr; |
| 383 | for (i = 0; i < NUM_E1_TS; i++) { |
| 384 | line->ts[i].num = i+1; |
| 385 | line->ts[i].line = line; |
| 386 | } |
| 387 | llist_add_tail(&line->list, &e1inp_line_list); |
| 388 | |
| 389 | return line; |
| 390 | } |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 391 | #endif |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 392 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 393 | static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr) |
| 394 | { |
| 395 | struct e1inp_line *e1i_line; |
| 396 | |
| 397 | e1i_line = e1inp_line_get(e1_nr); |
| 398 | if (!e1i_line) |
| 399 | return NULL; |
| 400 | |
| 401 | return &e1i_line->ts[ts_nr-1]; |
| 402 | } |
| 403 | |
| 404 | struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr) |
| 405 | { |
| 406 | struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr); |
| 407 | |
| 408 | if (!e1i_ts) |
| 409 | return NULL; |
| 410 | |
| 411 | return &e1i_ts->trau.mux; |
| 412 | } |
| 413 | |
| 414 | /* Signalling Link */ |
| 415 | |
| 416 | struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i, |
| 417 | u_int8_t tei, u_int8_t sapi) |
| 418 | { |
| 419 | struct e1inp_sign_link *link; |
| 420 | |
| 421 | llist_for_each_entry(link, &e1i->sign.sign_links, list) { |
| 422 | if (link->sapi == sapi && link->tei == tei) |
| 423 | return link; |
| 424 | } |
| 425 | |
| 426 | return NULL; |
| 427 | } |
| 428 | |
| 429 | /* create a new signalling link in a E1 timeslot */ |
| 430 | |
| 431 | struct e1inp_sign_link * |
| 432 | e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type, |
| 433 | struct gsm_bts_trx *trx, u_int8_t tei, |
| 434 | u_int8_t sapi) |
| 435 | { |
| 436 | struct e1inp_sign_link *link; |
| 437 | |
| 438 | if (ts->type != E1INP_TS_TYPE_SIGN) |
| 439 | return NULL; |
| 440 | |
Harald Welte | 470ec29 | 2009-06-26 20:25:23 +0200 | [diff] [blame] | 441 | link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 442 | if (!link) |
| 443 | return NULL; |
| 444 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 445 | link->ts = ts; |
| 446 | link->type = type; |
| 447 | INIT_LLIST_HEAD(&link->tx_list); |
| 448 | link->trx = trx; |
Holger Freyther | fb3f519 | 2009-02-09 23:09:15 +0000 | [diff] [blame] | 449 | link->tei = tei; |
| 450 | link->sapi = sapi; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 451 | |
| 452 | llist_add_tail(&link->list, &ts->sign.sign_links); |
| 453 | |
| 454 | return link; |
| 455 | } |
| 456 | |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 457 | void e1inp_sign_link_destroy(struct e1inp_sign_link *link) |
| 458 | { |
Holger Hans Peter Freyther | 4ac100e | 2010-04-11 14:13:10 +0200 | [diff] [blame] | 459 | struct msgb *msg; |
| 460 | |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 461 | llist_del(&link->list); |
Holger Hans Peter Freyther | 4ac100e | 2010-04-11 14:13:10 +0200 | [diff] [blame] | 462 | while (!llist_empty(&link->tx_list)) { |
| 463 | msg = msgb_dequeue(&link->tx_list); |
| 464 | msgb_free(msg); |
| 465 | } |
| 466 | |
Holger Hans Peter Freyther | f51b900 | 2010-04-11 17:18:02 +0200 | [diff] [blame] | 467 | if (link->ts->type == E1INP_TS_TYPE_SIGN) |
| 468 | bsc_del_timer(&link->ts->sign.tx_timer); |
| 469 | |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 470 | talloc_free(link); |
| 471 | } |
| 472 | |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 473 | /* the E1 driver tells us he has received something on a TS */ |
| 474 | int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg, |
| 475 | u_int8_t tei, u_int8_t sapi) |
| 476 | { |
| 477 | struct e1inp_sign_link *link; |
| 478 | int ret; |
| 479 | |
| 480 | switch (ts->type) { |
| 481 | case E1INP_TS_TYPE_SIGN: |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 482 | /* consult the list of signalling links */ |
Holger Freyther | 0469cf6 | 2009-03-31 12:14:16 +0000 | [diff] [blame] | 483 | write_pcap_packet(PCAP_INPUT, sapi, tei, msg); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 484 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 485 | if (!link) { |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 486 | LOGP(DMI, LOGL_ERROR, "didn't find signalling link for " |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 487 | "tei %d, sapi %d\n", tei, sapi); |
| 488 | return -EINVAL; |
| 489 | } |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 490 | |
Harald Welte | dc5062b | 2010-03-26 21:28:59 +0800 | [diff] [blame] | 491 | log_set_context(BSC_CTX_BTS, link->trx->bts); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 492 | switch (link->type) { |
| 493 | case E1INP_SIGN_OML: |
| 494 | msg->trx = link->trx; |
| 495 | ret = abis_nm_rcvmsg(msg); |
| 496 | break; |
| 497 | case E1INP_SIGN_RSL: |
| 498 | msg->trx = link->trx; |
| 499 | ret = abis_rsl_rcvmsg(msg); |
| 500 | break; |
| 501 | default: |
| 502 | ret = -EINVAL; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 503 | LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
| 506 | break; |
| 507 | case E1INP_TS_TYPE_TRAU: |
Harald Welte | 9e78331 | 2009-02-17 19:02:29 +0000 | [diff] [blame] | 508 | ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg)); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 509 | break; |
| 510 | default: |
| 511 | ret = -EINVAL; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 512 | LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | #define TSX_ALLOC_SIZE 4096 |
| 520 | |
| 521 | /* called by driver if it wants to transmit on a given TS */ |
| 522 | struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts, |
| 523 | struct e1inp_sign_link **sign_link) |
| 524 | { |
| 525 | struct e1inp_sign_link *link; |
| 526 | struct msgb *msg = NULL; |
| 527 | int len; |
| 528 | |
| 529 | switch (e1i_ts->type) { |
| 530 | case E1INP_TS_TYPE_SIGN: |
| 531 | /* FIXME: implement this round robin */ |
| 532 | llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) { |
| 533 | msg = msgb_dequeue(&link->tx_list); |
| 534 | if (msg) { |
| 535 | if (sign_link) |
| 536 | *sign_link = link; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | break; |
| 541 | case E1INP_TS_TYPE_TRAU: |
Harald Welte | 966636f | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 542 | msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX"); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 543 | if (!msg) |
| 544 | return NULL; |
| 545 | len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40); |
| 546 | msgb_put(msg, 40); |
| 547 | break; |
| 548 | default: |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 549 | LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 550 | return NULL; |
| 551 | } |
| 552 | return msg; |
| 553 | } |
| 554 | |
| 555 | /* called by driver in case some kind of link state event */ |
| 556 | int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi) |
| 557 | { |
| 558 | struct e1inp_sign_link *link; |
Harald Welte | f338a03 | 2011-01-14 15:55:42 +0100 | [diff] [blame] | 559 | struct input_signal_data isd; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 560 | |
| 561 | link = e1inp_lookup_sign_link(ts, tei, sapi); |
| 562 | if (!link) |
| 563 | return -EINVAL; |
| 564 | |
Harald Welte | f338a03 | 2011-01-14 15:55:42 +0100 | [diff] [blame] | 565 | isd.link_type = link->type; |
| 566 | isd.trx = link->trx; |
| 567 | |
| 568 | /* report further upwards */ |
| 569 | dispatch_signal(SS_INPUT, evt, &isd); |
Holger Freyther | ff9592f | 2009-03-09 16:17:14 +0000 | [diff] [blame] | 570 | return 0; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* register a driver with the E1 core */ |
| 574 | int e1inp_driver_register(struct e1inp_driver *drv) |
| 575 | { |
Harald Welte | 44d542e | 2009-03-10 18:24:35 +0000 | [diff] [blame] | 576 | llist_add_tail(&drv->list, &e1inp_driver_list); |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 580 | struct e1inp_driver *e1inp_driver_find(const char *name) |
| 581 | { |
| 582 | struct e1inp_driver *drv; |
| 583 | |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 584 | llist_for_each_entry(drv, &e1inp_driver_list, list) { |
| 585 | if (!strcasecmp(name, drv->name)) |
| 586 | return drv; |
| 587 | } |
| 588 | return NULL; |
| 589 | } |
| 590 | |
Harald Welte | 4258182 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 591 | int e1inp_line_update(struct e1inp_line *line) |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 592 | { |
Harald Welte | f27d043 | 2011-02-11 16:49:41 +0100 | [diff] [blame] | 593 | struct input_signal_data isd; |
| 594 | int rc; |
| 595 | |
Harald Welte | 5455243 | 2011-02-05 12:26:55 +0100 | [diff] [blame] | 596 | if (line->driver && line->driver->line_update) |
Harald Welte | f27d043 | 2011-02-11 16:49:41 +0100 | [diff] [blame] | 597 | rc = line->driver->line_update(line); |
Harald Welte | 5455243 | 2011-02-05 12:26:55 +0100 | [diff] [blame] | 598 | else |
Harald Welte | f27d043 | 2011-02-11 16:49:41 +0100 | [diff] [blame] | 599 | rc = 0; |
| 600 | |
| 601 | /* Send a signal to anyone who is interested in new lines being |
| 602 | * configured */ |
| 603 | memset(&isd, 0, sizeof(isd)); |
| 604 | isd.line = line; |
| 605 | dispatch_signal(SS_INPUT, S_INP_LINE_INIT, &isd); |
| 606 | |
| 607 | return rc; |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 608 | } |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 609 | |
Harald Welte | 29b9cf8 | 2009-11-30 19:43:54 +0100 | [diff] [blame] | 610 | static int e1i_sig_cb(unsigned int subsys, unsigned int signal, |
| 611 | void *handler_data, void *signal_data) |
| 612 | { |
| 613 | if (subsys != SS_GLOBAL || |
| 614 | signal != S_GLOBAL_SHUTDOWN) |
| 615 | return 0; |
| 616 | |
| 617 | if (pcap_fd) { |
| 618 | close(pcap_fd); |
| 619 | pcap_fd = -1; |
| 620 | } |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 625 | void e1inp_misdn_init(void); |
Harald Welte | 1dd68c3 | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 626 | void e1inp_dahdi_init(void); |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 627 | |
| 628 | void e1inp_init(void) |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 629 | { |
| 630 | tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1, |
| 631 | "e1inp_sign_link"); |
Harald Welte | 29b9cf8 | 2009-11-30 19:43:54 +0100 | [diff] [blame] | 632 | register_signal_handler(SS_GLOBAL, e1i_sig_cb, NULL); |
Harald Welte | 3016d9f | 2011-02-05 13:54:41 +0100 | [diff] [blame] | 633 | |
| 634 | e1inp_misdn_init(); |
Harald Welte | ca17ef8 | 2011-02-05 15:13:27 +0100 | [diff] [blame] | 635 | #ifdef HAVE_DAHDI_USER_H |
Harald Welte | 1dd68c3 | 2011-02-05 13:58:46 +0100 | [diff] [blame] | 636 | e1inp_dahdi_init(); |
Harald Welte | ca17ef8 | 2011-02-05 15:13:27 +0100 | [diff] [blame] | 637 | #endif |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 638 | } |