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