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 | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame^] | 31 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 32 | #include <osmocom/gsm/ipa.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 33 | #include <openbsc/gsm_data.h> |
| 34 | |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 35 | static int udp_sock(const char *ifname) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | { |
| 37 | int fd, rc, bc = 1; |
| 38 | struct sockaddr_in sa; |
| 39 | |
| 40 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 41 | if (fd < 0) |
| 42 | return fd; |
| 43 | |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 44 | if (ifname) { |
Nikola Kolev | e73c62a | 2014-05-08 12:45:20 +0300 | [diff] [blame] | 45 | #ifdef __FreeBSD__ |
| 46 | rc = setsockopt(fd, SOL_SOCKET, IP_RECVIF, ifname, |
| 47 | strlen(ifname)); |
| 48 | #else |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 49 | rc = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, |
| 50 | strlen(ifname)); |
Nikola Kolev | e73c62a | 2014-05-08 12:45:20 +0300 | [diff] [blame] | 51 | #endif |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 52 | if (rc < 0) |
| 53 | goto err; |
| 54 | } |
| 55 | |
Holger Hans Peter Freyther | d66e5c3 | 2013-07-03 10:19:37 +0200 | [diff] [blame] | 56 | memset(&sa, 0, sizeof(sa)); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 57 | sa.sin_family = AF_INET; |
| 58 | sa.sin_port = htons(3006); |
| 59 | sa.sin_addr.s_addr = INADDR_ANY; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 60 | |
| 61 | rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 62 | if (rc < 0) |
| 63 | goto err; |
| 64 | |
| 65 | rc = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bc, sizeof(bc)); |
| 66 | if (rc < 0) |
| 67 | goto err; |
| 68 | |
| 69 | #if 0 |
Harald Welte | 71e49a0 | 2009-08-08 12:14:53 +0200 | [diff] [blame] | 70 | /* we cannot bind, since the response packets don't come from |
| 71 | * the broadcast address */ |
| 72 | sa.sin_family = AF_INET; |
| 73 | sa.sin_port = htons(3006); |
| 74 | inet_aton("255.255.255.255", &sa.sin_addr); |
| 75 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 77 | if (rc < 0) |
| 78 | goto err; |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 79 | #endif |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 80 | return fd; |
| 81 | |
| 82 | err: |
| 83 | close(fd); |
| 84 | return rc; |
| 85 | } |
| 86 | |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 87 | const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 88 | IPAC_MSGT_ID_GET, |
| 89 | 0x01, IPAC_IDTAG_MACADDR, |
| 90 | 0x01, IPAC_IDTAG_IPADDR, |
| 91 | 0x01, IPAC_IDTAG_UNIT, |
| 92 | 0x01, IPAC_IDTAG_LOCATION1, |
| 93 | 0x01, IPAC_IDTAG_LOCATION2, |
| 94 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 95 | 0x01, IPAC_IDTAG_SWVERSION, |
| 96 | 0x01, IPAC_IDTAG_UNITNAME, |
| 97 | 0x01, IPAC_IDTAG_SERNR, |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | static int bcast_find(int fd) |
| 102 | { |
| 103 | struct sockaddr_in sa; |
| 104 | |
| 105 | sa.sin_family = AF_INET; |
| 106 | sa.sin_port = htons(3006); |
| 107 | inet_aton("255.255.255.255", &sa.sin_addr); |
| 108 | |
| 109 | return sendto(fd, find_pkt, sizeof(find_pkt), 0, (struct sockaddr *) &sa, sizeof(sa)); |
| 110 | } |
| 111 | |
| 112 | static int parse_response(unsigned char *buf, int len) |
| 113 | { |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame] | 114 | uint8_t t_len; |
| 115 | uint8_t t_tag; |
| 116 | uint8_t *cur = buf; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 117 | |
| 118 | while (cur < buf + len) { |
| 119 | t_len = *cur++; |
| 120 | t_tag = *cur++; |
| 121 | |
Harald Welte | 161ab99 | 2014-08-20 23:46:40 +0200 | [diff] [blame^] | 122 | printf("%s='%s' ", ipa_ccm_idtag_name(t_tag), cur); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 123 | |
| 124 | cur += t_len; |
| 125 | } |
| 126 | printf("\n"); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int read_response(int fd) |
| 131 | { |
| 132 | unsigned char buf[255]; |
| 133 | struct sockaddr_in sa; |
| 134 | int len; |
| 135 | socklen_t sa_len = sizeof(sa); |
| 136 | |
| 137 | len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, &sa_len); |
| 138 | if (len < 0) |
| 139 | return len; |
| 140 | |
Harald Welte | 71e49a0 | 2009-08-08 12:14:53 +0200 | [diff] [blame] | 141 | /* 2 bytes length, 1 byte protocol (0xfe) */ |
| 142 | if (buf[2] != 0xfe) |
| 143 | return 0; |
| 144 | |
| 145 | if (buf[4] != IPAC_MSGT_ID_RESP) |
| 146 | return 0; |
| 147 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 148 | return parse_response(buf+6, len-6); |
| 149 | } |
| 150 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 151 | static int bfd_cb(struct osmo_fd *bfd, unsigned int flags) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 152 | { |
| 153 | if (flags & BSC_FD_READ) |
| 154 | return read_response(bfd->fd); |
| 155 | if (flags & BSC_FD_WRITE) { |
| 156 | bfd->when &= ~BSC_FD_WRITE; |
| 157 | return bcast_find(bfd->fd); |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 162 | static struct osmo_timer_list timer; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 163 | |
| 164 | static void timer_cb(void *_data) |
| 165 | { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 166 | struct osmo_fd *bfd = _data; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 167 | |
| 168 | bfd->when |= BSC_FD_WRITE; |
| 169 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 170 | osmo_timer_schedule(&timer, 5, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int main(int argc, char **argv) |
| 174 | { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 175 | struct osmo_fd bfd; |
Holger Hans Peter Freyther | 437879c | 2011-05-14 19:46:05 +0200 | [diff] [blame] | 176 | char *ifname = NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 177 | int rc; |
| 178 | |
| 179 | printf("ipaccess-find (C) 2009 by Harald Welte\n"); |
| 180 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 181 | |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 182 | if (argc < 2) { |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 183 | fprintf(stdout, "you might need to specify the outgoing\n" |
| 184 | " network interface, e.g. ``%s eth0''\n", argv[0]); |
Holger Hans Peter Freyther | 5f89492 | 2011-05-17 20:34:53 +0200 | [diff] [blame] | 185 | } else { |
Holger Hans Peter Freyther | 437879c | 2011-05-14 19:46:05 +0200 | [diff] [blame] | 186 | ifname = argv[1]; |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 189 | bfd.cb = bfd_cb; |
| 190 | bfd.when = BSC_FD_READ | BSC_FD_WRITE; |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 191 | bfd.fd = udp_sock(ifname); |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 192 | if (bfd.fd < 0) { |
| 193 | perror("Cannot create local socket for broadcast udp"); |
| 194 | exit(1); |
| 195 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 196 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 197 | osmo_fd_register(&bfd); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 198 | |
| 199 | timer.cb = timer_cb; |
| 200 | timer.data = &bfd; |
| 201 | |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 202 | osmo_timer_schedule(&timer, 5, 0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 203 | |
| 204 | printf("Trying to find ip.access BTS by broadcast UDP...\n"); |
| 205 | |
| 206 | while (1) { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 207 | rc = osmo_select_main(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 208 | if (rc < 0) |
| 209 | exit(3); |
| 210 | } |
| 211 | |
| 212 | exit(0); |
| 213 | } |
| 214 | |