Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 1 | /* ip.access nanoBTS configuration tool */ |
| 2 | |
| 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 20 | |
| 21 | #include <unistd.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <arpa/inet.h> |
| 27 | |
| 28 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 29 | #include <osmocom/core/select.h> |
| 30 | #include <osmocom/core/timer.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 31 | #include <openbsc/ipaccess.h> |
| 32 | #include <openbsc/gsm_data.h> |
| 33 | |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 34 | static int udp_sock(const char *ifname) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 35 | { |
| 36 | int fd, rc, bc = 1; |
| 37 | struct sockaddr_in sa; |
| 38 | |
| 39 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 40 | if (fd < 0) |
| 41 | return fd; |
| 42 | |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 43 | if (ifname) { |
Nikola Kolev | e73c62a | 2014-05-08 12:45:20 +0300 | [diff] [blame] | 44 | #ifdef __FreeBSD__ |
| 45 | rc = setsockopt(fd, SOL_SOCKET, IP_RECVIF, ifname, |
| 46 | strlen(ifname)); |
| 47 | #else |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 48 | rc = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, |
| 49 | strlen(ifname)); |
Nikola Kolev | e73c62a | 2014-05-08 12:45:20 +0300 | [diff] [blame] | 50 | #endif |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 51 | if (rc < 0) |
| 52 | goto err; |
| 53 | } |
| 54 | |
Holger Hans Peter Freyther | d66e5c3 | 2013-07-03 10:19:37 +0200 | [diff] [blame] | 55 | memset(&sa, 0, sizeof(sa)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 56 | sa.sin_family = AF_INET; |
| 57 | sa.sin_port = htons(3006); |
| 58 | sa.sin_addr.s_addr = INADDR_ANY; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 59 | |
| 60 | rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 61 | if (rc < 0) |
| 62 | goto err; |
| 63 | |
| 64 | rc = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bc, sizeof(bc)); |
| 65 | if (rc < 0) |
| 66 | goto err; |
| 67 | |
| 68 | #if 0 |
Harald Welte | 71e49a0 | 2009-08-08 12:14:53 +0200 | [diff] [blame] | 69 | /* we cannot bind, since the response packets don't come from |
| 70 | * the broadcast address */ |
| 71 | sa.sin_family = AF_INET; |
| 72 | sa.sin_port = htons(3006); |
| 73 | inet_aton("255.255.255.255", &sa.sin_addr); |
| 74 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 75 | rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 76 | if (rc < 0) |
| 77 | goto err; |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 78 | #endif |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 79 | return fd; |
| 80 | |
| 81 | err: |
| 82 | close(fd); |
| 83 | return rc; |
| 84 | } |
| 85 | |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 86 | const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 87 | IPAC_MSGT_ID_GET, |
| 88 | 0x01, IPAC_IDTAG_MACADDR, |
| 89 | 0x01, IPAC_IDTAG_IPADDR, |
| 90 | 0x01, IPAC_IDTAG_UNIT, |
| 91 | 0x01, IPAC_IDTAG_LOCATION1, |
| 92 | 0x01, IPAC_IDTAG_LOCATION2, |
| 93 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 94 | 0x01, IPAC_IDTAG_SWVERSION, |
| 95 | 0x01, IPAC_IDTAG_UNITNAME, |
| 96 | 0x01, IPAC_IDTAG_SERNR, |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | static int bcast_find(int fd) |
| 101 | { |
| 102 | struct sockaddr_in sa; |
| 103 | |
| 104 | sa.sin_family = AF_INET; |
| 105 | sa.sin_port = htons(3006); |
| 106 | inet_aton("255.255.255.255", &sa.sin_addr); |
| 107 | |
| 108 | return sendto(fd, find_pkt, sizeof(find_pkt), 0, (struct sockaddr *) &sa, sizeof(sa)); |
| 109 | } |
| 110 | |
| 111 | static int parse_response(unsigned char *buf, int len) |
| 112 | { |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 113 | uint8_t t_len; |
| 114 | uint8_t t_tag; |
| 115 | uint8_t *cur = buf; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 116 | |
| 117 | while (cur < buf + len) { |
| 118 | t_len = *cur++; |
| 119 | t_tag = *cur++; |
| 120 | |
Pablo Neira Ayuso | 7b6408c | 2011-04-11 16:32:58 +0200 | [diff] [blame] | 121 | printf("%s='%s' ", ipaccess_idtag_name(t_tag), cur); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 122 | |
| 123 | cur += t_len; |
| 124 | } |
| 125 | printf("\n"); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int read_response(int fd) |
| 130 | { |
| 131 | unsigned char buf[255]; |
| 132 | struct sockaddr_in sa; |
| 133 | int len; |
| 134 | socklen_t sa_len = sizeof(sa); |
| 135 | |
| 136 | len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, &sa_len); |
| 137 | if (len < 0) |
| 138 | return len; |
| 139 | |
Harald Welte | 71e49a0 | 2009-08-08 12:14:53 +0200 | [diff] [blame] | 140 | /* 2 bytes length, 1 byte protocol (0xfe) */ |
| 141 | if (buf[2] != 0xfe) |
| 142 | return 0; |
| 143 | |
| 144 | if (buf[4] != IPAC_MSGT_ID_RESP) |
| 145 | return 0; |
| 146 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 147 | return parse_response(buf+6, len-6); |
| 148 | } |
| 149 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 150 | static int bfd_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 151 | { |
| 152 | if (flags & BSC_FD_READ) |
| 153 | return read_response(bfd->fd); |
| 154 | if (flags & BSC_FD_WRITE) { |
| 155 | bfd->when &= ~BSC_FD_WRITE; |
| 156 | return bcast_find(bfd->fd); |
| 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 161 | static struct osmo_timer_list timer; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 162 | |
| 163 | static void timer_cb(void *_data) |
| 164 | { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 165 | struct osmo_fd *bfd = _data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 166 | |
| 167 | bfd->when |= BSC_FD_WRITE; |
| 168 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 169 | osmo_timer_schedule(&timer, 5, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int main(int argc, char **argv) |
| 173 | { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 174 | struct osmo_fd bfd; |
Holger Hans Peter Freyther | 437879c | 2011-05-14 19:46:05 +0200 | [diff] [blame] | 175 | char *ifname = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 176 | int rc; |
| 177 | |
| 178 | printf("ipaccess-find (C) 2009 by Harald Welte\n"); |
| 179 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 180 | |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 181 | if (argc < 2) { |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 182 | fprintf(stdout, "you might need to specify the outgoing\n" |
| 183 | " network interface, e.g. ``%s eth0''\n", argv[0]); |
Holger Hans Peter Freyther | 5f89492 | 2011-05-17 20:34:53 +0200 | [diff] [blame] | 184 | } else { |
Holger Hans Peter Freyther | 437879c | 2011-05-14 19:46:05 +0200 | [diff] [blame] | 185 | ifname = argv[1]; |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 188 | bfd.cb = bfd_cb; |
| 189 | bfd.when = BSC_FD_READ | BSC_FD_WRITE; |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 190 | bfd.fd = udp_sock(ifname); |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 191 | if (bfd.fd < 0) { |
| 192 | perror("Cannot create local socket for broadcast udp"); |
| 193 | exit(1); |
| 194 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 195 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 196 | osmo_fd_register(&bfd); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 197 | |
| 198 | timer.cb = timer_cb; |
| 199 | timer.data = &bfd; |
| 200 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 201 | osmo_timer_schedule(&timer, 5, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 202 | |
| 203 | printf("Trying to find ip.access BTS by broadcast UDP...\n"); |
| 204 | |
| 205 | while (1) { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 206 | rc = osmo_select_main(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 207 | if (rc < 0) |
| 208 | exit(3); |
| 209 | } |
| 210 | |
| 211 | exit(0); |
| 212 | } |
| 213 | |