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