| 1 |
/* NetworkListen for NuRAN OC-2G 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/oc2gbts_mgr.h" |
| 27 |
#include "misc/oc2gbts_misc.h" |
| 28 |
#include "misc/oc2gbts_nl.h" |
| 29 |
#include "misc/oc2gbts_par.h" |
| 30 |
#include "misc/oc2gbts_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/oc2g/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 |
int model; |
| 108 |
char rev_maj, rev_min; |
| 109 |
|
| 110 |
/* fetch the MAC */ |
| 111 |
fd_eth = open(ETH0_ADDR_SYSFS, O_RDONLY); |
| 112 |
if (fd_eth >= 0) { |
| 113 |
read(fd_eth, mac_str, sizeof(mac_str)-1); |
| 114 |
mac_str[sizeof(mac_str)-1] = '\0'; |
| 115 |
close(fd_eth); |
| 116 |
} |
| 117 |
|
| 118 |
/* fetch the serial number */ |
| 119 |
oc2gbts_par_get_int(OC2GBTS_PAR_SERNR, &serno); |
| 120 |
snprintf(ser_str, sizeof(ser_str), "%d", serno); |
| 121 |
|
| 122 |
/* fetch the model and trx number */ |
| 123 |
snprintf(model_name, sizeof(model_name), "OC-2G BTS"); |
| 124 |
|
| 125 |
oc2gbts_rev_get(&rev_maj, &rev_min); |
| 126 |
snprintf(model_name, sizeof(model_name), "%s Rev %c.%c", |
| 127 |
model_name, rev_maj, rev_min); |
| 128 |
|
| 129 |
model = oc2gbts_model_get(); |
| 130 |
if (model >= 0) { |
| 131 |
snprintf(model_name, sizeof(model_name), "%s (%05X)", |
| 132 |
model_name, model); |
| 133 |
} |
| 134 |
fetched_info = 1; |
| 135 |
} |
| 136 |
|
| 137 |
if (source_for_dest(&src->sin_addr, &loc_addr.sin_addr) != 0) { |
| 138 |
LOGP(DFIND, LOGL_ERROR, "Failed to determine local source\n"); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
msgb_put_u8(msg, IPAC_MSGT_ID_RESP); |
| 143 |
|
| 144 |
/* append MAC addr */ |
| 145 |
quirk_l16tv_put(msg, strlen(mac_str) + 1, IPAC_IDTAG_MACADDR, (uint8_t *) mac_str); |
| 146 |
|
| 147 |
/* append ip address */ |
| 148 |
inet_ntop(AF_INET, &loc_addr.sin_addr, loc_ip, sizeof(loc_ip)); |
| 149 |
quirk_l16tv_put(msg, strlen(loc_ip) + 1, IPAC_IDTAG_IPADDR, (uint8_t *) loc_ip); |
| 150 |
|
| 151 |
/* append the serial number */ |
| 152 |
quirk_l16tv_put(msg, strlen(ser_str) + 1, IPAC_IDTAG_SERNR, (uint8_t *) ser_str); |
| 153 |
|
| 154 |
/* abuse some flags */ |
| 155 |
quirk_l16tv_put(msg, strlen(model_name) + 1, IPAC_IDTAG_UNIT, (uint8_t *) model_name); |
| 156 |
|
| 157 |
/* ip.access nanoBTS would reply to port==3006 */ |
| 158 |
ipaccess_prepend_header_quirk(msg, IPAC_PROTO_IPACCESS); |
| 159 |
rc = sendto(fd->fd, msg->data, msg->len, 0, (struct sockaddr *)src, sizeof(*src)); |
| 160 |
if (rc != msg->len) |
| 161 |
LOGP(DFIND, LOGL_ERROR, |
| 162 |
"Failed to send with rc(%d) errno(%d)\n", rc, errno); |
| 163 |
} |
| 164 |
|
| 165 |
static int ipaccess_bcast(struct osmo_fd *fd, unsigned int what) |
| 166 |
{ |
| 167 |
uint8_t data[2048]; |
| 168 |
char src[INET_ADDRSTRLEN]; |
| 169 |
struct sockaddr_in addr = {}; |
| 170 |
socklen_t len = sizeof(addr); |
| 171 |
int rc; |
| 172 |
|
| 173 |
rc = recvfrom(fd->fd, data, sizeof(data), 0, |
| 174 |
(struct sockaddr *) &addr, &len); |
| 175 |
if (rc <= 0) { |
| 176 |
LOGP(DFIND, LOGL_ERROR, |
| 177 |
"Failed to read from socket errno(%d)\n", errno); |
| 178 |
return -1; |
| 179 |
} |
| 180 |
|
| 181 |
LOGP(DFIND, LOGL_DEBUG, |
| 182 |
"Received request from: %s size %d\n", |
| 183 |
inet_ntop(AF_INET, &addr.sin_addr, src, sizeof(src)), rc); |
| 184 |
|
| 185 |
if (rc < 6) |
| 186 |
return 0; |
| 187 |
|
| 188 |
if (data[2] != IPAC_PROTO_IPACCESS || data[4] != IPAC_MSGT_ID_GET) |
| 189 |
return 0; |
| 190 |
|
| 191 |
respond_to(&addr, fd, data + 6, rc - 6); |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int oc2gbts_mgr_nl_init(void) |
| 196 |
{ |
| 197 |
int rc; |
| 198 |
|
| 199 |
nl_fd.cb = ipaccess_bcast; |
| 200 |
rc = osmo_sock_init_ofd(&nl_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 201 |
"0.0.0.0", 3006, OSMO_SOCK_F_BIND); |
| 202 |
if (rc < 0) { |
| 203 |
perror("Socket creation"); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |