blob: 40729fa6ea36a952ede92324e787869498296f1a [file] [log] [blame]
Harald Welte9af6ddf2011-01-01 15:25:50 +01001/* 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 Welte923a3bd2009-02-14 12:51:36 +000020
21#include <unistd.h>
22#include <stdio.h>
23#include <stdlib.h>
Harald Welte923a3bd2009-02-14 12:51:36 +000024#include <sys/socket.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27
28
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010029#include <osmocom/core/select.h>
30#include <osmocom/core/timer.h>
Harald Welte4a88a492014-08-20 23:46:40 +020031#include <osmocom/gsm/protocol/ipaccess.h>
32#include <osmocom/gsm/ipa.h>
Harald Welte37881962009-04-30 15:15:37 +000033#include <openbsc/gsm_data.h>
34
Harald Weltee26d0792009-08-08 11:47:20 +020035static int udp_sock(const char *ifname)
Harald Welte923a3bd2009-02-14 12:51:36 +000036{
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 Weltee26d0792009-08-08 11:47:20 +020044 if (ifname) {
Nikola Kolev10bad102014-05-08 12:45:20 +030045#ifdef __FreeBSD__
46 rc = setsockopt(fd, SOL_SOCKET, IP_RECVIF, ifname,
47 strlen(ifname));
48#else
Harald Weltee26d0792009-08-08 11:47:20 +020049 rc = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
50 strlen(ifname));
Nikola Kolev10bad102014-05-08 12:45:20 +030051#endif
Harald Weltee26d0792009-08-08 11:47:20 +020052 if (rc < 0)
53 goto err;
54 }
55
Holger Hans Peter Freytherd5c270e2013-07-03 10:19:37 +020056 memset(&sa, 0, sizeof(sa));
Harald Welte923a3bd2009-02-14 12:51:36 +000057 sa.sin_family = AF_INET;
58 sa.sin_port = htons(3006);
59 sa.sin_addr.s_addr = INADDR_ANY;
Harald Welte923a3bd2009-02-14 12:51:36 +000060
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 Welte81cff3c2009-08-08 12:14:53 +020070 /* 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 Welte923a3bd2009-02-14 12:51:36 +000076 rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
77 if (rc < 0)
78 goto err;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020079#endif
Harald Welte923a3bd2009-02-14 12:51:36 +000080 return fd;
81
82err:
83 close(fd);
84 return rc;
85}
86
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020087const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00,
Harald Welte4f361fc2009-02-15 15:32:53 +000088 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 };
Harald Welte923a3bd2009-02-14 12:51:36 +000099
100
101static 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
112static int parse_response(unsigned char *buf, int len)
113{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200114 uint8_t t_len;
115 uint8_t t_tag;
116 uint8_t *cur = buf;
Harald Welte923a3bd2009-02-14 12:51:36 +0000117
Harald Welte923a3bd2009-02-14 12:51:36 +0000118 while (cur < buf + len) {
119 t_len = *cur++;
120 t_tag = *cur++;
121
Harald Welte4a88a492014-08-20 23:46:40 +0200122 printf("%s='%s' ", ipa_ccm_idtag_name(t_tag), cur);
Harald Welte923a3bd2009-02-14 12:51:36 +0000123
124 cur += t_len;
125 }
126 printf("\n");
127 return 0;
128}
129
130static 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 Welte81cff3c2009-08-08 12:14:53 +0200141 /* 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 Welte4593ff32009-05-01 14:53:36 +0000148 return parse_response(buf+6, len-6);
Harald Welte923a3bd2009-02-14 12:51:36 +0000149}
150
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200151static int bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welte923a3bd2009-02-14 12:51:36 +0000152{
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 Ayusobf540cb2011-05-06 12:11:06 +0200162static struct osmo_timer_list timer;
Harald Welte923a3bd2009-02-14 12:51:36 +0000163
164static void timer_cb(void *_data)
165{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200166 struct osmo_fd *bfd = _data;
Harald Welte923a3bd2009-02-14 12:51:36 +0000167
168 bfd->when |= BSC_FD_WRITE;
169
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200170 osmo_timer_schedule(&timer, 5, 0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000171}
172
173int main(int argc, char **argv)
174{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200175 struct osmo_fd bfd;
Holger Hans Peter Freytherd3080be2011-05-14 19:46:05 +0200176 char *ifname = NULL;
Harald Welte923a3bd2009-02-14 12:51:36 +0000177 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 Welte042401c2009-06-29 10:43:04 +0200182 if (argc < 2) {
Harald Weltee26d0792009-08-08 11:47:20 +0200183 fprintf(stdout, "you might need to specify the outgoing\n"
184 " network interface, e.g. ``%s eth0''\n", argv[0]);
Holger Hans Peter Freyther1e9ae4b2011-05-17 20:34:53 +0200185 } else {
Holger Hans Peter Freytherd3080be2011-05-14 19:46:05 +0200186 ifname = argv[1];
Harald Welte042401c2009-06-29 10:43:04 +0200187 }
188
Harald Welte923a3bd2009-02-14 12:51:36 +0000189 bfd.cb = bfd_cb;
190 bfd.when = BSC_FD_READ | BSC_FD_WRITE;
Harald Weltee26d0792009-08-08 11:47:20 +0200191 bfd.fd = udp_sock(ifname);
Harald Welte042401c2009-06-29 10:43:04 +0200192 if (bfd.fd < 0) {
193 perror("Cannot create local socket for broadcast udp");
194 exit(1);
195 }
Harald Welte923a3bd2009-02-14 12:51:36 +0000196
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200197 osmo_fd_register(&bfd);
Harald Welte923a3bd2009-02-14 12:51:36 +0000198
199 timer.cb = timer_cb;
200 timer.data = &bfd;
201
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200202 osmo_timer_schedule(&timer, 5, 0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000203
204 printf("Trying to find ip.access BTS by broadcast UDP...\n");
205
206 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200207 rc = osmo_select_main(0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000208 if (rc < 0)
209 exit(3);
210 }
211
212 exit(0);
213}
214