Debian Codegraph @

src/osmo-bts-lc15/misc/lc15bts_mgr_nl.c

raw symbols SCIP
1 /* NetworkListen for NuRAN Litecell 1.5 BTS management daemon */
2
3 /* Copyright (C) 2015 by Yves Godin <[email protected]>
4 *
5 * Based on sysmoBTS:
6 * sysmobts_mgr_nl.c
7 * (C) 2014 by Holger Hans Peter Freyther
8 *
9 * All Rights Reserved
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26 #include "misc/lc15bts_mgr.h"
27 #include "misc/lc15bts_misc.h"
28 #include "misc/lc15bts_nl.h"
29 #include "misc/lc15bts_par.h"
30 #include "misc/lc15bts_bid.h"
31
32 #include <osmo-bts/logging.h>
33
34 #include <osmocom/gsm/protocol/ipaccess.h>
35
36 #include <osmocom/core/msgb.h>
37 #include <osmocom/core/socket.h>
38 #include <osmocom/core/select.h>
39
40 #include <arpa/inet.h>
41
42 #include <sys/types.h>
43 #include <sys/socket.h>
44
45 #include <errno.h>
46 #include <string.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49
50 #define ETH0_ADDR_SYSFS "/var/lc15/net/eth0/address"
51
52 static struct osmo_fd nl_fd;
53
54 /*
55 * The TLV structure in IPA messages in UDP packages is a bit
56 * weird. First the header appears to have an extra NULL byte
57 * and second the L16 of the L16TV needs to include +1 for the
58 * tag. The default msgb/tlv and libosmo-abis routines do not
59 * provide this.
60 */
61
62 static void ipaccess_prepend_header_quirk(struct msgb *msg, int proto)
63 {
64 struct ipaccess_head *hh;
65
66 /* prepend the ip.access header */
67 hh = (struct ipaccess_head *) msgb_push(msg, sizeof(*hh) + 1);
68 hh->len = htons(msg->len - sizeof(*hh) - 1);
69 hh->proto = proto;
70 }
71
72 static void quirk_l16tv_put(struct msgb *msg, uint16_t len, uint8_t tag,
73 const uint8_t *val)
74 {
75 uint8_t *buf = msgb_put(msg, len + 2 + 1);
76
77 *buf++ = (len + 1) >> 8;
78 *buf++ = (len + 1) & 0xff;
79 *buf++ = tag;
80 memcpy(buf, val, len);
81 }
82
83 /*
84 * We don't look at the content of the request yet and lie
85 * about most of the responses.
86 */
87 static void respond_to(struct sockaddr_in *src, struct osmo_fd *fd,
88 uint8_t *data, size_t len)
89 {
90 static int fetched_info = 0;
91 static char mac_str[20] = {0, };
92 static char model_name[64] = {0, };
93 static char ser_str[20] = {0, };
94
95 struct sockaddr_in loc_addr;
96 int rc;
97 char loc_ip[INET_ADDRSTRLEN];
98 struct msgb *msg = msgb_alloc_headroom(512, 128, "ipa get response");
99 if (!msg) {
100 LOGP(DFIND, LOGL_ERROR, "Failed to allocate msgb\n");
101 return;
102 }
103
104 if (!fetched_info) {
105 int fd_eth;
106 int serno;
107
108 /* fetch the MAC */
109 fd_eth = open(ETH0_ADDR_SYSFS, O_RDONLY);
110 if (fd_eth >= 0) {
111 read(fd_eth, mac_str, sizeof(mac_str)-1);
112 mac_str[sizeof(mac_str)-1] = '\0';
113 close(fd_eth);
114 }
115
116 /* fetch the serial number */
117 lc15bts_par_get_int(tall_mgr_ctx, LC15BTS_PAR_SERNR, &serno);
118 snprintf(ser_str, sizeof(ser_str), "%d", serno);
119
120 strncpy(model_name, get_hwversion_desc(), sizeof(model_name)-1);
121 fetched_info = 1;
122 }
123
124 if (source_for_dest(&src->sin_addr, &loc_addr.sin_addr) != 0) {
125 LOGP(DFIND, LOGL_ERROR, "Failed to determine local source\n");
126 return;
127 }
128
129 msgb_put_u8(msg, IPAC_MSGT_ID_RESP);
130
131 /* append MAC addr */
132 quirk_l16tv_put(msg, strlen(mac_str) + 1, IPAC_IDTAG_MACADDR, (uint8_t *) mac_str);
133
134 /* append ip address */
135 inet_ntop(AF_INET, &loc_addr.sin_addr, loc_ip, sizeof(loc_ip));
136 quirk_l16tv_put(msg, strlen(loc_ip) + 1, IPAC_IDTAG_IPADDR, (uint8_t *) loc_ip);
137
138 /* append the serial number */
139 quirk_l16tv_put(msg, strlen(ser_str) + 1, IPAC_IDTAG_SERNR, (uint8_t *) ser_str);
140
141 /* abuse some flags */
142 quirk_l16tv_put(msg, strlen(model_name) + 1, IPAC_IDTAG_UNIT, (uint8_t *) model_name);
143
144 /* ip.access nanoBTS would reply to port==3006 */
145 ipaccess_prepend_header_quirk(msg, IPAC_PROTO_IPACCESS);
146 rc = sendto(fd->fd, msg->data, msg->len, 0, (struct sockaddr *)src, sizeof(*src));
147 if (rc != msg->len)
148 LOGP(DFIND, LOGL_ERROR,
149 "Failed to send with rc(%d) errno(%d)\n", rc, errno);
150 }
151
152 static int ipaccess_bcast(struct osmo_fd *fd, unsigned int what)
153 {
154 uint8_t data[2048];
155 char src[INET_ADDRSTRLEN];
156 struct sockaddr_in addr = {};
157 socklen_t len = sizeof(addr);
158 int rc;
159
160 rc = recvfrom(fd->fd, data, sizeof(data), 0,
161 (struct sockaddr *) &addr, &len);
162 if (rc <= 0) {
163 LOGP(DFIND, LOGL_ERROR,
164 "Failed to read from socket errno(%d)\n", errno);
165 return -1;
166 }
167
168 LOGP(DFIND, LOGL_DEBUG,
169 "Received request from: %s size %d\n",
170 inet_ntop(AF_INET, &addr.sin_addr, src, sizeof(src)), rc);
171
172 if (rc < 6)
173 return 0;
174
175 if (data[2] != IPAC_PROTO_IPACCESS || data[4] != IPAC_MSGT_ID_GET)
176 return 0;
177
178 respond_to(&addr, fd, data + 6, rc - 6);
179 return 0;
180 }
181
182 int lc15bts_mgr_nl_init(void)
183 {
184 int rc;
185
186 nl_fd.cb = ipaccess_bcast;
187 rc = osmo_sock_init_ofd(&nl_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
188 "0.0.0.0", 3006, OSMO_SOCK_F_BIND);
189 if (rc < 0) {
190 perror("Socket creation");
191 return -1;
192 }
193
194 return 0;
195 }