Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 1 | /* Routines to talk to the MSC using the IPA Protocol */ |
| 2 | /* |
| 3 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | df6143a | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 4 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 16 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 23 | #include <openbsc/debug.h> |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 24 | #include <osmocom/abis/ipaccess.h> |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 25 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 26 | #include <osmocom/core/write_queue.h> |
| 27 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 28 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 29 | #include <osmocom/gsm/tlv.h> |
| 30 | |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 31 | #include <arpa/inet.h> |
| 32 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
| 38 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 39 | static void connection_loss(struct bsc_msc_connection *con) |
| 40 | { |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 41 | struct osmo_fd *fd; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 42 | |
| 43 | fd = &con->write_queue.bfd; |
| 44 | |
| 45 | close(fd->fd); |
| 46 | fd->fd = -1; |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 47 | fd->cb = osmo_wqueue_bfd_cb; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 48 | fd->when = 0; |
| 49 | |
| 50 | con->is_connected = 0; |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 51 | con->first_contact = 0; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 52 | con->connection_loss(con); |
| 53 | } |
| 54 | |
Holger Hans Peter Freyther | e47a91b | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 55 | static void msc_con_timeout(void *_con) |
| 56 | { |
| 57 | struct bsc_msc_connection *con = _con; |
| 58 | |
| 59 | LOGP(DMSC, LOGL_ERROR, "MSC Connection timeout.\n"); |
| 60 | bsc_msc_lost(con); |
| 61 | } |
| 62 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 63 | /* called in the case of a non blocking connect */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 64 | static int msc_connection_connect(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 65 | { |
| 66 | int rc; |
| 67 | int val; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 68 | struct bsc_msc_connection *con; |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 69 | struct osmo_wqueue *queue; |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 70 | |
| 71 | socklen_t len = sizeof(val); |
| 72 | |
| 73 | if ((what & BSC_FD_WRITE) == 0) { |
Holger Hans Peter Freyther | 62abade | 2010-08-04 02:27:34 +0800 | [diff] [blame] | 74 | LOGP(DMSC, LOGL_ERROR, "Callback but not writable.\n"); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 78 | queue = container_of(fd, struct osmo_wqueue, bfd); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 79 | con = container_of(queue, struct bsc_msc_connection, write_queue); |
| 80 | |
Holger Hans Peter Freyther | d4eed52 | 2010-10-07 04:42:03 +0800 | [diff] [blame] | 81 | /* From here on we will either be connected or reconnect */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 82 | osmo_timer_del(&con->timeout_timer); |
Holger Hans Peter Freyther | d4eed52 | 2010-10-07 04:42:03 +0800 | [diff] [blame] | 83 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 84 | /* check the socket state */ |
| 85 | rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len); |
| 86 | if (rc != 0) { |
| 87 | LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n"); |
| 88 | goto error; |
| 89 | } |
| 90 | if (val != 0) { |
Holger Hans Peter Freyther | 9d90da9 | 2010-04-06 10:25:00 +0200 | [diff] [blame] | 91 | LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC: %d\n", val); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* go to full operation */ |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 97 | fd->cb = osmo_wqueue_bfd_cb; |
Holger Hans Peter Freyther | 0176eb4 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 98 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 99 | |
| 100 | con->is_connected = 1; |
Holger Hans Peter Freyther | b9ac37d | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 101 | LOGP(DMSC, LOGL_NOTICE, "(Re)Connected to the MSC.\n"); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 102 | if (con->connected) |
| 103 | con->connected(con); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 104 | return 0; |
| 105 | |
| 106 | error: |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 107 | osmo_fd_unregister(fd); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 108 | connection_loss(con); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 109 | return -1; |
| 110 | } |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 111 | static void setnonblocking(struct osmo_fd *fd) |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 112 | { |
| 113 | int flags; |
| 114 | |
| 115 | flags = fcntl(fd->fd, F_GETFL); |
| 116 | if (flags < 0) { |
| 117 | perror("fcntl get failed"); |
| 118 | close(fd->fd); |
| 119 | fd->fd = -1; |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | flags |= O_NONBLOCK; |
| 124 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 125 | if (flags < 0) { |
| 126 | perror("fcntl get failed"); |
| 127 | close(fd->fd); |
| 128 | fd->fd = -1; |
| 129 | return; |
| 130 | } |
| 131 | } |
| 132 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 133 | int bsc_msc_connect(struct bsc_msc_connection *con) |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 134 | { |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 135 | struct bsc_msc_dest *dest; |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 136 | struct osmo_fd *fd; |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 137 | struct sockaddr_in sin; |
| 138 | int on = 1, ret; |
| 139 | |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 140 | if (llist_empty(con->dests)) { |
| 141 | LOGP(DMSC, LOGL_ERROR, "No MSC connections configured.\n"); |
| 142 | connection_loss(con); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | /* move to the next connection */ |
| 147 | dest = (struct bsc_msc_dest *) con->dests->next; |
| 148 | llist_del(&dest->list); |
| 149 | llist_add_tail(&dest->list, con->dests); |
| 150 | |
| 151 | LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", |
| 152 | dest->ip, dest->port); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 153 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 154 | con->is_connected = 0; |
| 155 | |
| 156 | fd = &con->write_queue.bfd; |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 157 | fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 158 | fd->priv_nr = 1; |
| 159 | |
| 160 | if (fd->fd < 0) { |
| 161 | perror("Creating TCP socket failed"); |
| 162 | return fd->fd; |
| 163 | } |
| 164 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 165 | /* make it non blocking */ |
| 166 | setnonblocking(fd); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 167 | |
Holger Hans Peter Freyther | ca999a9 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 168 | /* set the socket priority */ |
| 169 | ret = setsockopt(fd->fd, IPPROTO_IP, IP_TOS, |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 170 | &dest->dscp, sizeof(dest->dscp)); |
Holger Hans Peter Freyther | ca999a9 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 171 | if (ret != 0) |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 172 | LOGP(DMSC, LOGL_ERROR, "Failed to set DSCP to %d. %s\n", |
| 173 | dest->dscp, strerror(errno)); |
Holger Hans Peter Freyther | ca999a9 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 174 | |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 175 | memset(&sin, 0, sizeof(sin)); |
| 176 | sin.sin_family = AF_INET; |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 177 | sin.sin_port = htons(dest->port); |
| 178 | inet_aton(dest->ip, &sin.sin_addr); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 179 | |
| 180 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 181 | ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin)); |
| 182 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 183 | if (ret == -1 && errno == EINPROGRESS) { |
| 184 | LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n"); |
| 185 | fd->when = BSC_FD_WRITE; |
| 186 | fd->cb = msc_connection_connect; |
Holger Hans Peter Freyther | e47a91b | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 187 | con->timeout_timer.cb = msc_con_timeout; |
| 188 | con->timeout_timer.data = con; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 189 | osmo_timer_schedule(&con->timeout_timer, 20, 0); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 190 | } else if (ret < 0) { |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 191 | perror("Connection failed"); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 192 | connection_loss(con); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 193 | return ret; |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 194 | } else { |
Holger Hans Peter Freyther | 0176eb4 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 195 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 196 | fd->cb = osmo_wqueue_bfd_cb; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 197 | con->is_connected = 1; |
| 198 | if (con->connected) |
| 199 | con->connected(con); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 200 | } |
| 201 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 202 | ret = osmo_fd_register(fd); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 203 | if (ret < 0) { |
| 204 | perror("Registering the fd failed"); |
| 205 | close(fd->fd); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | return ret; |
| 210 | } |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 211 | |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 212 | struct bsc_msc_connection *bsc_msc_create(void *ctx, struct llist_head *dests) |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 213 | { |
| 214 | struct bsc_msc_connection *con; |
| 215 | |
| 216 | con = talloc_zero(NULL, struct bsc_msc_connection); |
| 217 | if (!con) { |
| 218 | LOGP(DMSC, LOGL_FATAL, "Failed to create the MSC connection.\n"); |
| 219 | return NULL; |
| 220 | } |
| 221 | |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 222 | con->dests = dests; |
| 223 | con->write_queue.bfd.fd = -1; |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 224 | osmo_wqueue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 225 | return con; |
| 226 | } |
| 227 | |
| 228 | void bsc_msc_lost(struct bsc_msc_connection *con) |
| 229 | { |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 230 | osmo_wqueue_clear(&con->write_queue); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 231 | osmo_timer_del(&con->timeout_timer); |
Holger Hans Peter Freyther | fad0753 | 2010-10-07 06:07:57 +0800 | [diff] [blame] | 232 | |
| 233 | if (con->write_queue.bfd.fd >= 0) |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 234 | osmo_fd_unregister(&con->write_queue.bfd); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 235 | connection_loss(con); |
| 236 | } |
| 237 | |
| 238 | static void reconnect_msc(void *_msc) |
| 239 | { |
| 240 | struct bsc_msc_connection *con = _msc; |
| 241 | |
| 242 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 243 | bsc_msc_connect(con); |
| 244 | } |
| 245 | |
| 246 | void bsc_msc_schedule_connect(struct bsc_msc_connection *con) |
| 247 | { |
| 248 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 249 | con->reconnect_timer.cb = reconnect_msc; |
| 250 | con->reconnect_timer.data = con; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 251 | osmo_timer_schedule(&con->reconnect_timer, 5, 0); |
Holger Hans Peter Freyther | f76e7ef | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | struct msgb *bsc_msc_id_get_resp(const char *token) |
| 255 | { |
| 256 | struct msgb *msg; |
| 257 | |
| 258 | if (!token) { |
| 259 | LOGP(DMSC, LOGL_ERROR, "No token specified.\n"); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | msg = msgb_alloc_headroom(4096, 128, "id resp"); |
| 264 | if (!msg) { |
| 265 | LOGP(DMSC, LOGL_ERROR, "Failed to create the message.\n"); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP); |
| 270 | msgb_l16tv_put(msg, strlen(token) + 1, |
Holger Hans Peter Freyther | c42ad8b | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 271 | IPAC_IDTAG_UNITNAME, (uint8_t *) token); |
Holger Hans Peter Freyther | f76e7ef | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 272 | return msg; |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 273 | } |