Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1 | /* OpenBSC Abis interface to mISDNuser */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
Holger Freyther | 2139b24 | 2009-01-02 01:17:48 +0000 | [diff] [blame] | 29 | #include <time.h> |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 30 | #include <sys/fcntl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <mISDNif.h> |
| 35 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 36 | //#define AF_COMPATIBILITY_FUNC |
| 37 | //#include <compat_af_isdn.h> |
| 38 | #define AF_ISDN 34 |
| 39 | #define PF_ISDN AF_ISDN |
| 40 | |
| 41 | #include <openbsc/select.h> |
| 42 | #include <openbsc/msgb.h> |
| 43 | #include <openbsc/debug.h> |
| 44 | #include <openbsc/gsm_data.h> |
| 45 | #include <openbsc/abis_nm.h> |
| 46 | #include <openbsc/abis_rsl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 47 | |
| 48 | #define NUM_E1_TS 32 |
| 49 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * pcap writing of the misdn load |
| 53 | * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat |
| 54 | */ |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 55 | #define DLT_LINUX_LAPD 177 |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 56 | #define PCAP_INPUT 0 |
| 57 | #define PCAP_OUTPUT 1 |
| 58 | |
| 59 | struct pcap_hdr { |
| 60 | u_int32_t magic_number; |
| 61 | u_int16_t version_major; |
| 62 | u_int16_t version_minor; |
| 63 | int32_t thiszone; |
| 64 | u_int32_t sigfigs; |
| 65 | u_int32_t snaplen; |
| 66 | u_int32_t network; |
| 67 | } __attribute__((packed)); |
| 68 | |
| 69 | struct pcaprec_hdr { |
| 70 | u_int32_t ts_sec; |
| 71 | u_int32_t ts_usec; |
| 72 | u_int32_t incl_len; |
| 73 | u_int32_t orig_len; |
| 74 | } __attribute__((packed)); |
| 75 | |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 76 | struct pseudo_isdn_header { |
| 77 | int32_t uton; |
| 78 | u_int8_t channel; |
| 79 | } __attribute__((packed)); |
| 80 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 81 | struct fake_lapd_frame { |
| 82 | u_int8_t ea1 : 1; |
| 83 | u_int8_t cr : 1; |
| 84 | u_int8_t sapi : 6; |
| 85 | u_int8_t ea2 : 1; |
| 86 | u_int8_t tei : 7; |
| 87 | u_int8_t control_foo; /* fake UM's ... */ |
| 88 | } __attribute__((packed)); |
| 89 | |
| 90 | static int pcap_fd = -1; |
| 91 | |
| 92 | void mi_set_pcap_fd(int fd) |
| 93 | { |
| 94 | int ret; |
| 95 | struct pcap_hdr header = { |
| 96 | .magic_number = 0xa1b2c3d4, |
| 97 | .version_major = 2, |
| 98 | .version_minor = 4, |
| 99 | .thiszone = 0, |
| 100 | .sigfigs = 0, |
| 101 | .snaplen = 65535, |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 102 | .network = DLT_LINUX_LAPD, |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | pcap_fd = fd; |
| 106 | ret = write(pcap_fd, &header, sizeof(header)); |
| 107 | } |
| 108 | |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 109 | /* This currently only works for the D-Channel */ |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 110 | static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr, |
| 111 | struct msgb *msg) { |
| 112 | if (pcap_fd < 0) |
| 113 | return; |
| 114 | |
| 115 | int ret; |
Holger Freyther | 2139b24 | 2009-01-02 01:17:48 +0000 | [diff] [blame] | 116 | time_t cur_time; |
| 117 | struct tm *tm; |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 118 | |
| 119 | struct pseudo_isdn_header isdn = { |
| 120 | /* user to network.. we are the network.. gboolean is a init */ |
| 121 | .uton = 0, |
| 122 | .channel = addr->channel, |
| 123 | }; |
| 124 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 125 | struct fake_lapd_frame header = { |
| 126 | .ea1 = 0, |
| 127 | .cr = PCAP_OUTPUT ? 1 : 0, |
| 128 | .sapi = addr->sapi & 0x3F, |
| 129 | .ea2 = 1, |
| 130 | .tei = addr->tei & 0x7F, |
| 131 | .control_foo = 0x13 /* UI with P set */, |
| 132 | }; |
| 133 | |
| 134 | struct pcaprec_hdr payload_header = { |
| 135 | .ts_sec = 0, |
| 136 | .ts_usec = 0, |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 137 | .incl_len = msg->len + sizeof(struct fake_lapd_frame) |
| 138 | + sizeof(struct pseudo_isdn_header) |
| 139 | - MISDN_HEADER_LEN, |
| 140 | .orig_len = msg->len + sizeof(struct fake_lapd_frame) |
| 141 | + sizeof(struct pseudo_isdn_header) |
| 142 | - MISDN_HEADER_LEN, |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 145 | |
| 146 | |
Holger Freyther | 2139b24 | 2009-01-02 01:17:48 +0000 | [diff] [blame] | 147 | cur_time = time(NULL); |
| 148 | tm = localtime(&cur_time); |
| 149 | payload_header.ts_sec = mktime(tm); |
| 150 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 151 | ret = write(pcap_fd, &payload_header, sizeof(payload_header)); |
Harald Welte | 62a923d | 2009-01-03 01:32:00 +0000 | [diff] [blame^] | 152 | ret = write(pcap_fd, &isdn, sizeof(isdn)); |
Harald Welte | ca6bf78 | 2009-01-03 00:41:04 +0000 | [diff] [blame] | 153 | ret = write(pcap_fd, &header, sizeof(header)); |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 154 | ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN, |
| 155 | msg->len - MISDN_HEADER_LEN); |
| 156 | } |
| 157 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 158 | /* data structure for one E1 interface with A-bis */ |
| 159 | struct mi_e1_handle { |
| 160 | struct gsm_bts *bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 161 | /* The mISDN card number of the card we use */ |
| 162 | int cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 163 | /* The RSL adress */ |
| 164 | struct sockaddr_mISDN l2addr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 165 | /* The OML adress */ |
| 166 | struct sockaddr_mISDN omladdr; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 167 | /* list (queue) of to-be-sent msgb's */ |
| 168 | struct llist_head rsl_tx_list; |
| 169 | struct llist_head oml_tx_list; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 170 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 171 | void (*cb)(int event, struct gsm_bts *bts); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 172 | struct bsc_fd fd[NUM_E1_TS]; |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 173 | |
| 174 | int ts2_fd; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 177 | /* FIXME: this needs to go */ |
| 178 | static struct mi_e1_handle *global_e1h; |
| 179 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 180 | #define SAPI_L2ML 0 |
| 181 | #define SAPI_OML 62 |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 182 | #define SAPI_RSL 0 /* 63 ? */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 183 | |
| 184 | #define TEI_L2ML 127 |
| 185 | #define TEI_OML 25 |
| 186 | #define TEI_RSL 1 |
| 187 | |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 188 | void hexdump(unsigned char *buf, int len) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 189 | { |
| 190 | int i; |
| 191 | for (i = 0; i < len; i++) { |
| 192 | fprintf(stdout, "%02x ", buf[i]); |
| 193 | } |
| 194 | fprintf(stdout, "\n"); |
| 195 | } |
| 196 | |
| 197 | #define TS1_ALLOC_SIZE 300 |
| 198 | |
| 199 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 200 | { |
| 201 | struct mi_e1_handle *e1h = bfd->data; |
| 202 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 203 | struct sockaddr_mISDN l2addr; |
| 204 | struct mISDNhead *hh; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 205 | socklen_t alen; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 206 | int ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 207 | |
| 208 | if (!msg) |
| 209 | return -ENOMEM; |
| 210 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 211 | hh = (struct mISDNhead *) msg->data; |
| 212 | |
| 213 | /* FIXME: Map TEI/SAPI to TRX */ |
| 214 | msg->trx = e1h->bts->c0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 215 | |
| 216 | alen = sizeof(l2addr); |
| 217 | ret = recvfrom(bfd->fd, msg->data, 300, 0, |
| 218 | (struct sockaddr *) &l2addr, &alen); |
| 219 | if (ret < 0) { |
| 220 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | if (alen != sizeof(l2addr)) |
| 225 | return -EINVAL; |
| 226 | |
| 227 | msgb_put(msg, ret); |
| 228 | |
| 229 | DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n", |
| 230 | alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 231 | |
| 232 | DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n", |
| 233 | ret, hh->prim, hh->id); |
| 234 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 235 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 236 | switch (hh->prim) { |
| 237 | case DL_INFORMATION_IND: |
| 238 | DEBUGP(DMI, "got DL_INFORMATION_IND\n"); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 239 | struct sockaddr_mISDN *sa = NULL; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 240 | char *lstr = "UNKN"; |
| 241 | |
| 242 | switch (l2addr.tei) { |
| 243 | case TEI_OML: |
| 244 | sa = &e1h->omladdr; |
| 245 | lstr = "OML"; |
| 246 | break; |
| 247 | case TEI_RSL: |
| 248 | sa = &e1h->l2addr; |
| 249 | lstr = "RSL"; |
| 250 | break; |
| 251 | default: |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 252 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 253 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 254 | if (sa) { |
| 255 | DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n", |
| 256 | lstr, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 257 | memcpy(sa, &l2addr, sizeof(l2addr)); |
| 258 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 259 | break; |
| 260 | case DL_ESTABLISH_IND: |
| 261 | DEBUGP(DMI, "got DL_ESTABLISH_IND\n"); |
| 262 | break; |
| 263 | case DL_ESTABLISH_CNF: |
| 264 | DEBUGP(DMI, "got DL_ESTABLISH_CNF\n"); |
| 265 | break; |
| 266 | case DL_RELEASE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 267 | DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n"); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 268 | break; |
| 269 | case MPH_ACTIVATE_IND: |
| 270 | DEBUGP(DMI, "got MPH_ACTIVATE_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 271 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 272 | e1h->cb(EVT_E1_OML_UP, e1h->bts); |
| 273 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 274 | e1h->cb(EVT_E1_RSL_UP, e1h->bts); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 275 | break; |
| 276 | case MPH_DEACTIVATE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 277 | DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n"); |
| 278 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 279 | e1h->cb(EVT_E1_OML_DN, e1h->bts); |
| 280 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 281 | e1h->cb(EVT_E1_RSL_DN, e1h->bts); |
| 282 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 283 | case DL_DATA_IND: |
| 284 | DEBUGP(DMI, "got DL_DATA_IND\n"); |
Holger Freyther | 52af7d4 | 2009-01-02 01:18:28 +0000 | [diff] [blame] | 285 | write_pcap_packet(PCAP_INPUT, &l2addr, msg); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 286 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 287 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 288 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 289 | if (debug_mask & DMI) { |
| 290 | fprintf(stdout, "RX: "); |
| 291 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 292 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 293 | switch (l2addr.tei) { |
| 294 | case TEI_OML: |
| 295 | ret = abis_nm_rcvmsg(msg); |
| 296 | break; |
| 297 | case TEI_RSL: |
| 298 | ret = abis_rsl_rcvmsg(msg); |
| 299 | break; |
| 300 | default: |
| 301 | fprintf(stderr, "DATA_IND for unknown TEI\n"); |
| 302 | break; |
| 303 | } |
| 304 | break; |
| 305 | default: |
| 306 | DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim); |
| 307 | break; |
| 308 | } |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 313 | { |
| 314 | struct mi_e1_handle *e1h = bfd->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 315 | struct msgb *msg; |
| 316 | struct mISDNhead *hh; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 317 | int ret, no_oml = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 318 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 319 | msg = msgb_dequeue(&e1h->oml_tx_list); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 320 | if (!msg) |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 321 | no_oml = 1; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 322 | else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 323 | u_int8_t *l2_data = msg->data; |
| 324 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 325 | /* prepend the mISDNhead */ |
| 326 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 327 | hh->prim = DL_DATA_REQ; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 328 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 329 | if (debug_mask & DMI) { |
| 330 | fprintf(stdout, "OML TX: "); |
| 331 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 332 | } |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 333 | |
| 334 | write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 335 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 336 | (struct sockaddr *)&e1h->omladdr, |
| 337 | sizeof(e1h->omladdr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 338 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 339 | usleep(100000); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 340 | /* we always dequeue all OML messages */ |
| 341 | return ret; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 342 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 343 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 344 | msg = msgb_dequeue(&e1h->rsl_tx_list); |
| 345 | if (!msg) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 346 | if (no_oml) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 347 | bfd->when &= ~BSC_FD_WRITE; |
| 348 | } else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 349 | u_int8_t *l2_data = msg->data; |
| 350 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 351 | /* prepend the mISDNhead */ |
| 352 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 353 | hh->prim = DL_DATA_REQ; |
| 354 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 355 | if (debug_mask & DMI) { |
| 356 | fprintf(stdout, "RSL TX: "); |
| 357 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 358 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 359 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 360 | write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 361 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 362 | (struct sockaddr *)&e1h->l2addr, |
| 363 | sizeof(e1h->l2addr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 364 | msgb_free(msg); |
Harald Welte | b275042 | 2008-12-27 11:24:23 +0000 | [diff] [blame] | 365 | usleep(10000); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 366 | //sleep(1); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 372 | #define TSX_ALLOC_SIZE 4096 |
| 373 | |
| 374 | /* FIXME: read from a B channel TS */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 375 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 376 | { |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 377 | struct mi_e1_handle *e1h = bfd->data; |
| 378 | struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE); |
| 379 | struct mISDNhead *hh; |
| 380 | int ret; |
| 381 | |
| 382 | if (!msg) |
| 383 | return -ENOMEM; |
| 384 | |
| 385 | hh = (struct mISDNhead *) msg->data; |
| 386 | |
| 387 | /* FIXME: Map TEI/SAPI to TRX */ |
| 388 | msg->trx = e1h->bts->c0; |
| 389 | |
| 390 | ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0); |
| 391 | if (ret < 0) { |
| 392 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | msgb_put(msg, ret); |
| 397 | |
| 398 | DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id); |
| 399 | |
| 400 | switch (hh->prim) { |
| 401 | case PH_CONTROL_IND: |
| 402 | DEBUGP(DMIB, "got PH_CONTROL_IND\n"); |
| 403 | break; |
| 404 | case PH_DATA_IND: |
| 405 | DEBUGP(DMIB, "got PH_DATA_IND\n"); |
| 406 | |
| 407 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 408 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 409 | if (debug_mask & DMIB) { |
| 410 | fprintf(stdout, "BCHAN RX: "); |
| 411 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 412 | } |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 413 | if (!e1h->ts2_fd) |
| 414 | e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660); |
| 415 | |
| 416 | write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 417 | |
| 418 | break; |
| 419 | default: |
| 420 | DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim); |
| 421 | break; |
| 422 | } |
Harald Welte | e27bb34 | 2008-12-29 06:06:35 +0000 | [diff] [blame] | 423 | /* FIXME: don't free it if we still hold reference! */ |
| 424 | msgb_free(msg); |
| 425 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 426 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 429 | static int handle_tsX_write(struct bsc_fd *bfd) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 430 | { |
| 431 | /* FIXME: write to a B channel TS */ |
Holger Freyther | 059c254 | 2008-12-27 09:39:48 +0000 | [diff] [blame] | 432 | return -1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* callback from select.c in case one of the fd's can be read/written */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 436 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 437 | { |
| 438 | unsigned int e1_ts = bfd->priv_nr; |
| 439 | int rc = 0; |
| 440 | |
| 441 | switch (e1_ts) { |
| 442 | case 1: |
| 443 | if (what & BSC_FD_READ) |
| 444 | rc = handle_ts1_read(bfd); |
| 445 | if (what & BSC_FD_WRITE) |
| 446 | rc = handle_ts1_write(bfd); |
| 447 | break; |
| 448 | default: |
| 449 | if (what & BSC_FD_READ) |
| 450 | rc = handle_tsX_read(bfd); |
| 451 | if (what & BSC_FD_WRITE) |
| 452 | rc = handle_tsX_write(bfd); |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | return rc; |
| 457 | } |
| 458 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 459 | int abis_rsl_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 460 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 461 | struct mi_e1_handle *e1h = global_e1h; |
| 462 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 463 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 464 | msgb_enqueue(&e1h->rsl_tx_list, msg); |
| 465 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 470 | int _abis_nm_sendmsg(struct msgb *msg) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 471 | { |
| 472 | struct mi_e1_handle *e1h = global_e1h; |
| 473 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 474 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 475 | msgb_enqueue(&e1h->oml_tx_list, msg); |
| 476 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 481 | static int activate_bchan(struct mi_e1_handle *e1h, int ts) |
| 482 | { |
| 483 | struct mISDNhead hh; |
| 484 | int ret; |
| 485 | struct bsc_fd *bfd = &e1h->fd[ts-1]; |
| 486 | |
| 487 | fprintf(stdout, "activate bchan\n"); |
| 488 | hh.prim = PH_ACTIVATE_REQ; |
| 489 | hh.id = MISDN_ID_ANY; |
| 490 | ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0); |
| 491 | if (ret < 0) { |
| 492 | fprintf(stdout, "could not send ACTIVATE_RQ %s\n", |
| 493 | strerror(errno)); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 500 | static int mi_e1_setup(struct mi_e1_handle *e1h) |
| 501 | { |
| 502 | int ts, sk, ret, cnt; |
| 503 | struct mISDN_devinfo devinfo; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 504 | |
| 505 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 506 | if (sk < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 507 | fprintf(stderr, "could not open socket %s\n", strerror(errno)); |
| 508 | return sk; |
| 509 | } |
| 510 | |
| 511 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 512 | if (ret) { |
| 513 | fprintf(stderr, "error getting interf count: %s\n", |
| 514 | strerror(errno)); |
| 515 | close(sk); |
| 516 | return -ENODEV; |
| 517 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 518 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 519 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 520 | #if 1 |
| 521 | devinfo.id = e1h->cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 522 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 523 | if (ret < 0) { |
| 524 | fprintf(stdout, "error getting info for device %d: %s\n", |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 525 | e1h->cardnr, strerror(errno)); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 526 | return -ENODEV; |
| 527 | } |
| 528 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 529 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 530 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 531 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 532 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 533 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 534 | #endif |
| 535 | |
| 536 | /* TS0 is CRC4, don't need any fd for it */ |
| 537 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 538 | unsigned int idx = ts-1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 539 | struct bsc_fd *bfd = &e1h->fd[idx]; |
| 540 | struct sockaddr_mISDN addr; |
| 541 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 542 | bfd->data = e1h; |
| 543 | bfd->priv_nr = ts; |
| 544 | bfd->cb = misdn_fd_cb; |
| 545 | |
| 546 | if (ts == 1) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 547 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 548 | bfd->when = BSC_FD_READ; |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 549 | } else |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 550 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 551 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 552 | if (bfd->fd < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 553 | fprintf(stderr, "could not open socket %s\n", |
| 554 | strerror(errno)); |
| 555 | return bfd->fd; |
| 556 | } |
| 557 | |
| 558 | memset(&addr, 0, sizeof(addr)); |
| 559 | addr.family = AF_ISDN; |
| 560 | addr.dev = e1h->cardnr; |
| 561 | if (ts == 1) { |
| 562 | addr.channel = 0; |
| 563 | addr.sapi = 0;/* SAPI not supported yet in kernel */ |
| 564 | addr.tei = TEI_L2ML; |
| 565 | } else |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 566 | addr.channel = ts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 567 | |
| 568 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 569 | if (ret < 0) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 570 | fprintf(stderr, "could not bind l2 socket %s\n", |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 571 | strerror(errno)); |
| 572 | return -EIO; |
| 573 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 574 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 575 | if (ts == 2) { |
| 576 | bfd->when = BSC_FD_READ; |
| 577 | activate_bchan(e1h, ts); |
| 578 | } |
| 579 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 580 | ret = bsc_register_fd(bfd); |
| 581 | if (ret < 0) { |
| 582 | fprintf(stderr, "could not register FD: %s\n", |
| 583 | strerror(ret)); |
| 584 | return ret; |
| 585 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 586 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 587 | |
| 588 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 591 | int mi_setup(struct gsm_bts *bts, int cardnr, |
| 592 | void (cb)(int event, struct gsm_bts *bts)) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 593 | { |
| 594 | struct mi_e1_handle *e1h; |
| 595 | |
| 596 | e1h = malloc(sizeof(*e1h)); |
| 597 | memset(e1h, 0, sizeof(*e1h)); |
| 598 | |
| 599 | e1h->cardnr = cardnr; |
| 600 | e1h->bts = bts; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 601 | e1h->cb = cb; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 602 | INIT_LLIST_HEAD(&e1h->oml_tx_list); |
| 603 | INIT_LLIST_HEAD(&e1h->rsl_tx_list); |
| 604 | |
| 605 | global_e1h = e1h; |
| 606 | |
| 607 | return mi_e1_setup(e1h); |
| 608 | } |