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, |
Holger Freyther | 52aab54 | 2009-01-03 05:49:22 +0000 | [diff] [blame] | 144 | .control_foo = 0x03 /* UI */, |
Harald Welte | 599639e | 2009-01-03 03:28:10 +0000 | [diff] [blame] | 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 | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 200 | #define TS1_ALLOC_SIZE 300 |
| 201 | |
| 202 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 203 | { |
| 204 | struct mi_e1_handle *e1h = bfd->data; |
| 205 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 206 | struct sockaddr_mISDN l2addr; |
| 207 | struct mISDNhead *hh; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 208 | socklen_t alen; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 209 | int ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 210 | |
| 211 | if (!msg) |
| 212 | return -ENOMEM; |
| 213 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 214 | hh = (struct mISDNhead *) msg->data; |
| 215 | |
| 216 | /* FIXME: Map TEI/SAPI to TRX */ |
| 217 | msg->trx = e1h->bts->c0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 218 | |
| 219 | alen = sizeof(l2addr); |
| 220 | ret = recvfrom(bfd->fd, msg->data, 300, 0, |
| 221 | (struct sockaddr *) &l2addr, &alen); |
| 222 | if (ret < 0) { |
| 223 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | if (alen != sizeof(l2addr)) |
| 228 | return -EINVAL; |
| 229 | |
| 230 | msgb_put(msg, ret); |
| 231 | |
| 232 | DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n", |
| 233 | alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 234 | |
| 235 | DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n", |
| 236 | ret, hh->prim, hh->id); |
| 237 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 238 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 239 | switch (hh->prim) { |
| 240 | case DL_INFORMATION_IND: |
| 241 | DEBUGP(DMI, "got DL_INFORMATION_IND\n"); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 242 | struct sockaddr_mISDN *sa = NULL; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 243 | char *lstr = "UNKN"; |
| 244 | |
| 245 | switch (l2addr.tei) { |
| 246 | case TEI_OML: |
| 247 | sa = &e1h->omladdr; |
| 248 | lstr = "OML"; |
| 249 | break; |
| 250 | case TEI_RSL: |
| 251 | sa = &e1h->l2addr; |
| 252 | lstr = "RSL"; |
| 253 | break; |
| 254 | default: |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 255 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 256 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 257 | if (sa) { |
| 258 | DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n", |
| 259 | lstr, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 260 | memcpy(sa, &l2addr, sizeof(l2addr)); |
| 261 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 262 | break; |
| 263 | case DL_ESTABLISH_IND: |
| 264 | DEBUGP(DMI, "got DL_ESTABLISH_IND\n"); |
| 265 | break; |
| 266 | case DL_ESTABLISH_CNF: |
| 267 | DEBUGP(DMI, "got DL_ESTABLISH_CNF\n"); |
| 268 | break; |
| 269 | case DL_RELEASE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 270 | DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n"); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 271 | break; |
| 272 | case MPH_ACTIVATE_IND: |
| 273 | DEBUGP(DMI, "got MPH_ACTIVATE_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 274 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 275 | e1h->cb(EVT_E1_OML_UP, e1h->bts); |
| 276 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 277 | e1h->cb(EVT_E1_RSL_UP, e1h->bts); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 278 | break; |
| 279 | case MPH_DEACTIVATE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 280 | DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n"); |
| 281 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 282 | e1h->cb(EVT_E1_OML_DN, e1h->bts); |
| 283 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 284 | e1h->cb(EVT_E1_RSL_DN, e1h->bts); |
| 285 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 286 | case DL_DATA_IND: |
| 287 | DEBUGP(DMI, "got DL_DATA_IND\n"); |
Holger Freyther | 52af7d4 | 2009-01-02 01:18:28 +0000 | [diff] [blame] | 288 | write_pcap_packet(PCAP_INPUT, &l2addr, msg); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 289 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 290 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 291 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 292 | if (debug_mask & DMI) { |
| 293 | fprintf(stdout, "RX: "); |
| 294 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 295 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 296 | switch (l2addr.tei) { |
| 297 | case TEI_OML: |
| 298 | ret = abis_nm_rcvmsg(msg); |
| 299 | break; |
| 300 | case TEI_RSL: |
| 301 | ret = abis_rsl_rcvmsg(msg); |
| 302 | break; |
| 303 | default: |
| 304 | fprintf(stderr, "DATA_IND for unknown TEI\n"); |
| 305 | break; |
| 306 | } |
| 307 | break; |
| 308 | default: |
| 309 | DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim); |
| 310 | break; |
| 311 | } |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 316 | { |
| 317 | struct mi_e1_handle *e1h = bfd->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 318 | struct msgb *msg; |
| 319 | struct mISDNhead *hh; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 320 | int ret, no_oml = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 321 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 322 | msg = msgb_dequeue(&e1h->oml_tx_list); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 323 | if (!msg) |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 324 | no_oml = 1; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 325 | else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 326 | u_int8_t *l2_data = msg->data; |
| 327 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 328 | /* prepend the mISDNhead */ |
| 329 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 330 | hh->prim = DL_DATA_REQ; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 331 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 332 | if (debug_mask & DMI) { |
| 333 | fprintf(stdout, "OML TX: "); |
| 334 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 335 | } |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 336 | |
| 337 | write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 338 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 339 | (struct sockaddr *)&e1h->omladdr, |
| 340 | sizeof(e1h->omladdr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 341 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 342 | usleep(100000); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 343 | /* we always dequeue all OML messages */ |
| 344 | return ret; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 345 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 346 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 347 | msg = msgb_dequeue(&e1h->rsl_tx_list); |
| 348 | if (!msg) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 349 | if (no_oml) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 350 | bfd->when &= ~BSC_FD_WRITE; |
| 351 | } else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 352 | u_int8_t *l2_data = msg->data; |
| 353 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 354 | /* prepend the mISDNhead */ |
| 355 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 356 | hh->prim = DL_DATA_REQ; |
| 357 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 358 | if (debug_mask & DMI) { |
| 359 | fprintf(stdout, "RSL TX: "); |
| 360 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 361 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 362 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 363 | write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 364 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 365 | (struct sockaddr *)&e1h->l2addr, |
| 366 | sizeof(e1h->l2addr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 367 | msgb_free(msg); |
Harald Welte | b275042 | 2008-12-27 11:24:23 +0000 | [diff] [blame] | 368 | usleep(10000); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 369 | //sleep(1); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 375 | #define TSX_ALLOC_SIZE 4096 |
| 376 | |
| 377 | /* FIXME: read from a B channel TS */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 378 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 379 | { |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 380 | struct mi_e1_handle *e1h = bfd->data; |
| 381 | struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE); |
| 382 | struct mISDNhead *hh; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 383 | int ret, dummy; |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 384 | |
| 385 | if (!msg) |
| 386 | return -ENOMEM; |
| 387 | |
| 388 | hh = (struct mISDNhead *) msg->data; |
| 389 | |
| 390 | /* FIXME: Map TEI/SAPI to TRX */ |
| 391 | msg->trx = e1h->bts->c0; |
| 392 | |
| 393 | ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0); |
| 394 | if (ret < 0) { |
| 395 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | msgb_put(msg, ret); |
| 400 | |
| 401 | DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id); |
| 402 | |
| 403 | switch (hh->prim) { |
| 404 | case PH_CONTROL_IND: |
| 405 | DEBUGP(DMIB, "got PH_CONTROL_IND\n"); |
| 406 | break; |
| 407 | case PH_DATA_IND: |
| 408 | DEBUGP(DMIB, "got PH_DATA_IND\n"); |
| 409 | |
| 410 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 411 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 412 | if (debug_mask & DMIB) { |
| 413 | fprintf(stdout, "BCHAN RX: "); |
| 414 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 415 | } |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 416 | if (!e1h->ts2_fd) |
| 417 | e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660); |
| 418 | |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 419 | dummy = write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN); |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 420 | |
| 421 | break; |
| 422 | default: |
| 423 | DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim); |
| 424 | break; |
| 425 | } |
Harald Welte | e27bb34 | 2008-12-29 06:06:35 +0000 | [diff] [blame] | 426 | /* FIXME: don't free it if we still hold reference! */ |
| 427 | msgb_free(msg); |
| 428 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 429 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 432 | static int handle_tsX_write(struct bsc_fd *bfd) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 433 | { |
| 434 | /* FIXME: write to a B channel TS */ |
Holger Freyther | 059c254 | 2008-12-27 09:39:48 +0000 | [diff] [blame] | 435 | return -1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* 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] | 439 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 440 | { |
| 441 | unsigned int e1_ts = bfd->priv_nr; |
| 442 | int rc = 0; |
| 443 | |
| 444 | switch (e1_ts) { |
| 445 | case 1: |
| 446 | if (what & BSC_FD_READ) |
| 447 | rc = handle_ts1_read(bfd); |
| 448 | if (what & BSC_FD_WRITE) |
| 449 | rc = handle_ts1_write(bfd); |
| 450 | break; |
| 451 | default: |
| 452 | if (what & BSC_FD_READ) |
| 453 | rc = handle_tsX_read(bfd); |
| 454 | if (what & BSC_FD_WRITE) |
| 455 | rc = handle_tsX_write(bfd); |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | return rc; |
| 460 | } |
| 461 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 462 | int abis_rsl_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 463 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 464 | struct mi_e1_handle *e1h = global_e1h; |
| 465 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 466 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 467 | msgb_enqueue(&e1h->rsl_tx_list, msg); |
| 468 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 473 | int _abis_nm_sendmsg(struct msgb *msg) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 474 | { |
| 475 | struct mi_e1_handle *e1h = global_e1h; |
| 476 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 477 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 478 | msgb_enqueue(&e1h->oml_tx_list, msg); |
| 479 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 484 | static int activate_bchan(struct mi_e1_handle *e1h, int ts) |
| 485 | { |
| 486 | struct mISDNhead hh; |
| 487 | int ret; |
| 488 | struct bsc_fd *bfd = &e1h->fd[ts-1]; |
| 489 | |
| 490 | fprintf(stdout, "activate bchan\n"); |
| 491 | hh.prim = PH_ACTIVATE_REQ; |
| 492 | hh.id = MISDN_ID_ANY; |
| 493 | ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0); |
| 494 | if (ret < 0) { |
| 495 | fprintf(stdout, "could not send ACTIVATE_RQ %s\n", |
| 496 | strerror(errno)); |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 503 | static int mi_e1_setup(struct mi_e1_handle *e1h) |
| 504 | { |
| 505 | int ts, sk, ret, cnt; |
| 506 | struct mISDN_devinfo devinfo; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 507 | |
| 508 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 509 | if (sk < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 510 | fprintf(stderr, "could not open socket %s\n", strerror(errno)); |
| 511 | return sk; |
| 512 | } |
| 513 | |
| 514 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 515 | if (ret) { |
| 516 | fprintf(stderr, "error getting interf count: %s\n", |
| 517 | strerror(errno)); |
| 518 | close(sk); |
| 519 | return -ENODEV; |
| 520 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 521 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 522 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 523 | #if 1 |
| 524 | devinfo.id = e1h->cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 525 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 526 | if (ret < 0) { |
| 527 | fprintf(stdout, "error getting info for device %d: %s\n", |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 528 | e1h->cardnr, strerror(errno)); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 529 | return -ENODEV; |
| 530 | } |
| 531 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 532 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 533 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 534 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 535 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 536 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 537 | #endif |
| 538 | |
| 539 | /* TS0 is CRC4, don't need any fd for it */ |
| 540 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 541 | unsigned int idx = ts-1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 542 | struct bsc_fd *bfd = &e1h->fd[idx]; |
| 543 | struct sockaddr_mISDN addr; |
| 544 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 545 | bfd->data = e1h; |
| 546 | bfd->priv_nr = ts; |
| 547 | bfd->cb = misdn_fd_cb; |
| 548 | |
| 549 | if (ts == 1) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 550 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 551 | bfd->when = BSC_FD_READ; |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 552 | } else |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 553 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 554 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 555 | if (bfd->fd < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 556 | fprintf(stderr, "could not open socket %s\n", |
| 557 | strerror(errno)); |
| 558 | return bfd->fd; |
| 559 | } |
| 560 | |
| 561 | memset(&addr, 0, sizeof(addr)); |
| 562 | addr.family = AF_ISDN; |
| 563 | addr.dev = e1h->cardnr; |
| 564 | if (ts == 1) { |
| 565 | addr.channel = 0; |
| 566 | addr.sapi = 0;/* SAPI not supported yet in kernel */ |
| 567 | addr.tei = TEI_L2ML; |
| 568 | } else |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 569 | addr.channel = ts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 570 | |
| 571 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 572 | if (ret < 0) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 573 | fprintf(stderr, "could not bind l2 socket %s\n", |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 574 | strerror(errno)); |
| 575 | return -EIO; |
| 576 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 577 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 578 | if (ts == 2) { |
| 579 | bfd->when = BSC_FD_READ; |
| 580 | activate_bchan(e1h, ts); |
| 581 | } |
| 582 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 583 | ret = bsc_register_fd(bfd); |
| 584 | if (ret < 0) { |
| 585 | fprintf(stderr, "could not register FD: %s\n", |
| 586 | strerror(ret)); |
| 587 | return ret; |
| 588 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 589 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 590 | |
| 591 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 594 | int mi_setup(struct gsm_bts *bts, int cardnr, |
| 595 | void (cb)(int event, struct gsm_bts *bts)) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 596 | { |
| 597 | struct mi_e1_handle *e1h; |
| 598 | |
| 599 | e1h = malloc(sizeof(*e1h)); |
| 600 | memset(e1h, 0, sizeof(*e1h)); |
| 601 | |
| 602 | e1h->cardnr = cardnr; |
| 603 | e1h->bts = bts; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 604 | e1h->cb = cb; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 605 | INIT_LLIST_HEAD(&e1h->oml_tx_list); |
| 606 | INIT_LLIST_HEAD(&e1h->rsl_tx_list); |
| 607 | |
| 608 | global_e1h = e1h; |
| 609 | |
| 610 | return mi_e1_setup(e1h); |
| 611 | } |