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 |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 24 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | f76e7ef | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 25 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 26 | |
| 27 | #include <osmocore/write_queue.h> |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 28 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 29 | |
| 30 | #include <arpa/inet.h> |
| 31 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 38 | static void connection_loss(struct bsc_msc_connection *con) |
| 39 | { |
| 40 | struct bsc_fd *fd; |
| 41 | |
| 42 | fd = &con->write_queue.bfd; |
| 43 | |
| 44 | close(fd->fd); |
| 45 | fd->fd = -1; |
| 46 | fd->cb = write_queue_bfd_cb; |
| 47 | fd->when = 0; |
| 48 | |
| 49 | con->is_connected = 0; |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 50 | con->first_contact = 0; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 51 | con->connection_loss(con); |
| 52 | } |
| 53 | |
Holger Hans Peter Freyther | e47a91b | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 54 | static void msc_con_timeout(void *_con) |
| 55 | { |
| 56 | struct bsc_msc_connection *con = _con; |
| 57 | |
| 58 | LOGP(DMSC, LOGL_ERROR, "MSC Connection timeout.\n"); |
| 59 | bsc_msc_lost(con); |
| 60 | } |
| 61 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 62 | /* called in the case of a non blocking connect */ |
| 63 | static int msc_connection_connect(struct bsc_fd *fd, unsigned int what) |
| 64 | { |
| 65 | int rc; |
| 66 | int val; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 67 | struct bsc_msc_connection *con; |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 68 | struct write_queue *queue; |
| 69 | |
| 70 | socklen_t len = sizeof(val); |
| 71 | |
| 72 | if ((what & BSC_FD_WRITE) == 0) { |
Holger Hans Peter Freyther | 62abade | 2010-08-04 02:27:34 +0800 | [diff] [blame] | 73 | LOGP(DMSC, LOGL_ERROR, "Callback but not writable.\n"); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 77 | queue = container_of(fd, struct write_queue, bfd); |
| 78 | con = container_of(queue, struct bsc_msc_connection, write_queue); |
| 79 | |
Holger Hans Peter Freyther | d4eed52 | 2010-10-07 04:42:03 +0800 | [diff] [blame] | 80 | /* From here on we will either be connected or reconnect */ |
| 81 | bsc_del_timer(&con->timeout_timer); |
| 82 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 83 | /* check the socket state */ |
| 84 | rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len); |
| 85 | if (rc != 0) { |
| 86 | LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n"); |
| 87 | goto error; |
| 88 | } |
| 89 | if (val != 0) { |
Holger Hans Peter Freyther | 9d90da9 | 2010-04-06 10:25:00 +0200 | [diff] [blame] | 90 | 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] | 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* go to full operation */ |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 96 | fd->cb = write_queue_bfd_cb; |
Holger Hans Peter Freyther | 0176eb4 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 97 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 98 | |
| 99 | con->is_connected = 1; |
Holger Hans Peter Freyther | b9ac37d | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 100 | LOGP(DMSC, LOGL_NOTICE, "(Re)Connected to the MSC.\n"); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 101 | if (con->connected) |
| 102 | con->connected(con); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 103 | return 0; |
| 104 | |
| 105 | error: |
| 106 | bsc_unregister_fd(fd); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 107 | connection_loss(con); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 108 | return -1; |
| 109 | } |
| 110 | static void setnonblocking(struct bsc_fd *fd) |
| 111 | { |
| 112 | int flags; |
| 113 | |
| 114 | flags = fcntl(fd->fd, F_GETFL); |
| 115 | if (flags < 0) { |
| 116 | perror("fcntl get failed"); |
| 117 | close(fd->fd); |
| 118 | fd->fd = -1; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | flags |= O_NONBLOCK; |
| 123 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 124 | if (flags < 0) { |
| 125 | perror("fcntl get failed"); |
| 126 | close(fd->fd); |
| 127 | fd->fd = -1; |
| 128 | return; |
| 129 | } |
| 130 | } |
| 131 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 132 | int bsc_msc_connect(struct bsc_msc_connection *con) |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 133 | { |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 134 | struct bsc_fd *fd; |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 135 | struct sockaddr_in sin; |
| 136 | int on = 1, ret; |
| 137 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 138 | LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", con->ip, con->port); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 139 | |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 140 | con->is_connected = 0; |
| 141 | |
| 142 | fd = &con->write_queue.bfd; |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 143 | fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 144 | fd->priv_nr = 1; |
| 145 | |
| 146 | if (fd->fd < 0) { |
| 147 | perror("Creating TCP socket failed"); |
| 148 | return fd->fd; |
| 149 | } |
| 150 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 151 | /* make it non blocking */ |
| 152 | setnonblocking(fd); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 153 | |
Holger Hans Peter Freyther | ca999a9 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 154 | /* set the socket priority */ |
| 155 | ret = setsockopt(fd->fd, IPPROTO_IP, IP_TOS, |
| 156 | &con->prio, sizeof(con->prio)); |
| 157 | if (ret != 0) |
| 158 | LOGP(DMSC, LOGL_ERROR, "Failed to set prio to %d. %s\n", |
| 159 | con->prio, strerror(errno)); |
| 160 | |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 161 | memset(&sin, 0, sizeof(sin)); |
| 162 | sin.sin_family = AF_INET; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 163 | sin.sin_port = htons(con->port); |
| 164 | inet_aton(con->ip, &sin.sin_addr); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 165 | |
| 166 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 167 | ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin)); |
| 168 | |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 169 | if (ret == -1 && errno == EINPROGRESS) { |
| 170 | LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n"); |
| 171 | fd->when = BSC_FD_WRITE; |
| 172 | fd->cb = msc_connection_connect; |
Holger Hans Peter Freyther | e47a91b | 2010-05-05 22:48:56 +0800 | [diff] [blame] | 173 | con->timeout_timer.cb = msc_con_timeout; |
| 174 | con->timeout_timer.data = con; |
| 175 | bsc_schedule_timer(&con->timeout_timer, 20, 0); |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 176 | } else if (ret < 0) { |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 177 | perror("Connection failed"); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 178 | connection_loss(con); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 179 | return ret; |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 180 | } else { |
Holger Hans Peter Freyther | 0176eb4 | 2010-04-08 11:12:32 +0200 | [diff] [blame] | 181 | fd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
Holger Hans Peter Freyther | 6c0a04e | 2010-03-26 10:41:20 +0100 | [diff] [blame] | 182 | fd->cb = write_queue_bfd_cb; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 183 | con->is_connected = 1; |
| 184 | if (con->connected) |
| 185 | con->connected(con); |
Holger Hans Peter Freyther | eef86b5 | 2010-06-15 18:45:06 +0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | ret = bsc_register_fd(fd); |
| 189 | if (ret < 0) { |
| 190 | perror("Registering the fd failed"); |
| 191 | close(fd->fd); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | return ret; |
| 196 | } |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 197 | |
Holger Hans Peter Freyther | ca999a9 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 198 | struct bsc_msc_connection *bsc_msc_create(const char *ip, int port, int prio) |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 199 | { |
| 200 | struct bsc_msc_connection *con; |
| 201 | |
| 202 | con = talloc_zero(NULL, struct bsc_msc_connection); |
| 203 | if (!con) { |
| 204 | LOGP(DMSC, LOGL_FATAL, "Failed to create the MSC connection.\n"); |
| 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | con->ip = ip; |
| 209 | con->port = port; |
Holger Hans Peter Freyther | ca999a9 | 2010-06-15 18:52:38 +0800 | [diff] [blame] | 210 | con->prio = prio; |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 211 | write_queue_init(&con->write_queue, 100); |
| 212 | return con; |
| 213 | } |
| 214 | |
| 215 | void bsc_msc_lost(struct bsc_msc_connection *con) |
| 216 | { |
Holger Hans Peter Freyther | 03ca97e | 2010-03-31 13:15:26 +0200 | [diff] [blame] | 217 | write_queue_clear(&con->write_queue); |
Holger Hans Peter Freyther | d4eed52 | 2010-10-07 04:42:03 +0800 | [diff] [blame] | 218 | bsc_del_timer(&con->timeout_timer); |
Holger Hans Peter Freyther | fad0753 | 2010-10-07 06:07:57 +0800 | [diff] [blame] | 219 | |
| 220 | if (con->write_queue.bfd.fd >= 0) |
| 221 | bsc_unregister_fd(&con->write_queue.bfd); |
Holger Hans Peter Freyther | baf2abe | 2010-06-15 18:47:29 +0800 | [diff] [blame] | 222 | connection_loss(con); |
| 223 | } |
| 224 | |
| 225 | static void reconnect_msc(void *_msc) |
| 226 | { |
| 227 | struct bsc_msc_connection *con = _msc; |
| 228 | |
| 229 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 230 | bsc_msc_connect(con); |
| 231 | } |
| 232 | |
| 233 | void bsc_msc_schedule_connect(struct bsc_msc_connection *con) |
| 234 | { |
| 235 | LOGP(DMSC, LOGL_NOTICE, "Attempting to reconnect to the MSC.\n"); |
| 236 | con->reconnect_timer.cb = reconnect_msc; |
| 237 | con->reconnect_timer.data = con; |
| 238 | bsc_schedule_timer(&con->reconnect_timer, 5, 0); |
Holger Hans Peter Freyther | f76e7ef | 2010-06-15 18:52:05 +0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | struct msgb *bsc_msc_id_get_resp(const char *token) |
| 242 | { |
| 243 | struct msgb *msg; |
| 244 | |
| 245 | if (!token) { |
| 246 | LOGP(DMSC, LOGL_ERROR, "No token specified.\n"); |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | msg = msgb_alloc_headroom(4096, 128, "id resp"); |
| 251 | if (!msg) { |
| 252 | LOGP(DMSC, LOGL_ERROR, "Failed to create the message.\n"); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP); |
| 257 | msgb_l16tv_put(msg, strlen(token) + 1, |
| 258 | IPAC_IDTAG_UNITNAME, (u_int8_t *) token); |
| 259 | return msg; |
Holger Hans Peter Freyther | bec411b | 2010-07-05 14:14:18 +0800 | [diff] [blame] | 260 | } |