| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static void ipaccess_prepend_header_quirk(struct msgb *msg, int proto) |
| 63 |
{ |
| 64 |
struct ipaccess_head *hh; |
| 65 |
|
| 66 |
|
| 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 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 132 |
quirk_l16tv_put(msg, strlen(mac_str) + 1, IPAC_IDTAG_MACADDR, (uint8_t *) mac_str); |
| 133 |
|
| 134 |
|
| 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 |
|
| 139 |
quirk_l16tv_put(msg, strlen(ser_str) + 1, IPAC_IDTAG_SERNR, (uint8_t *) ser_str); |
| 140 |
|
| 141 |
|
| 142 |
quirk_l16tv_put(msg, strlen(model_name) + 1, IPAC_IDTAG_UNIT, (uint8_t *) model_name); |
| 143 |
|
| 144 |
|
| 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 |
} |