1103 lines
33 KiB
C
1103 lines
33 KiB
C
#ifndef _GNU_SOURCE
|
|||
|
|
#define _GNU_SOURCE
|
||
|
|
#endif
|
||
|
|
|
||
|
|
/* Bananarchy event-driven IMS / VoLTE / VoWiFi / MBN orchestration daemon. */
|
||
|
|
|
||
|
|
#include <ctype.h>
|
||
|
|
#include <errno.h>
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <poll.h>
|
||
|
|
#include <signal.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <sys/socket.h>
|
||
|
|
#include <sys/stat.h>
|
||
|
|
#include <sys/types.h>
|
||
|
|
#include <sys/un.h>
|
||
|
|
#include <sys/wait.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#include "common.h"
|
||
|
|
#include "ims_mbn.h"
|
||
|
|
#include "ims_qrtr.h"
|
||
|
|
|
||
|
|
static volatile sig_atomic_t g_stop = 0;
|
||
|
|
static volatile sig_atomic_t g_reload = 0;
|
||
|
|
static volatile sig_atomic_t g_chld_pending = 0;
|
||
|
|
static int g_sig_pipe[2] = {-1, -1};
|
||
|
|
|
||
|
|
static struct ims_config g_cfg;
|
||
|
|
static struct ims_runtime_state g_state;
|
||
|
|
static struct ims_qrtr_ep g_qrtr;
|
||
|
|
static int g_qrtr_ready = 0;
|
||
|
|
static struct ims_qrtr_ep g_imsdcm;
|
||
|
|
static int g_imsdcm_ready = 0;
|
||
|
|
static uint16_t g_txn_id = 1;
|
||
|
|
|
||
|
|
static pid_t g_vowifi_pid = -1;
|
||
|
|
static char g_vowifi_running_mode[16] = "";
|
||
|
|
static int g_bearer_up = 0;
|
||
|
|
static int g_baseband_ims_set = 0;
|
||
|
|
static int64_t g_baseband_try_since_ms = 0;
|
||
|
|
static char g_pdc_loaded_id[64] = "";
|
||
|
|
|
||
|
|
static void wake_sig_pipe(void) {
|
||
|
|
if (g_sig_pipe[1] >= 0) {
|
||
|
|
char b = 1;
|
||
|
|
ssize_t w = write(g_sig_pipe[1], &b, 1);
|
||
|
|
(void)w;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void on_term_sig(int sig) {
|
||
|
|
(void)sig;
|
||
|
|
g_stop = 1;
|
||
|
|
wake_sig_pipe();
|
||
|
|
}
|
||
|
|
|
||
|
|
static void on_hup_sig(int sig) {
|
||
|
|
(void)sig;
|
||
|
|
g_reload = 1;
|
||
|
|
wake_sig_pipe();
|
||
|
|
}
|
||
|
|
|
||
|
|
static void on_chld_sig(int sig) {
|
||
|
|
(void)sig;
|
||
|
|
g_chld_pending = 1;
|
||
|
|
wake_sig_pipe();
|
||
|
|
}
|
||
|
|
|
||
|
|
static uint16_t next_txn(void) {
|
||
|
|
uint16_t t = g_txn_id++;
|
||
|
|
if (g_txn_id == 0) {
|
||
|
|
g_txn_id = 1;
|
||
|
|
}
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
static const char *ims_bearer_script(void) {
|
||
|
|
const char *p = getenv("BANANARCHY_IMS_BEARER_SH");
|
||
|
|
return (p && *p) ? p : "/usr/bin/bananarchy-ims-bearer.sh";
|
||
|
|
}
|
||
|
|
|
||
|
|
static const char *ims_vowifi_bin(void) {
|
||
|
|
const char *p = getenv("BANANARCHY_VOWIFI_BIN");
|
||
|
|
return (p && *p) ? p : "/usr/bin/bananarchy-vowifi";
|
||
|
|
}
|
||
|
|
|
||
|
|
static int run_bearer_cmd(const char *action, const char *apn,
|
||
|
|
char *out_iface, size_t iface_cap,
|
||
|
|
char *out_ip, size_t ip_cap) {
|
||
|
|
if (out_iface && iface_cap) out_iface[0] = '\0';
|
||
|
|
if (out_ip && ip_cap) out_ip[0] = '\0';
|
||
|
|
const char *script = ims_bearer_script();
|
||
|
|
if (access(script, X_OK) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
int pfd[2];
|
||
|
|
if (pipe2(pfd, O_CLOEXEC) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
pid_t pid = fork();
|
||
|
|
if (pid < 0) {
|
||
|
|
close(pfd[0]);
|
||
|
|
close(pfd[1]);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
if (pid == 0) {
|
||
|
|
close(pfd[0]);
|
||
|
|
dup2(pfd[1], STDOUT_FILENO);
|
||
|
|
close(pfd[1]);
|
||
|
|
int devnull = open("/dev/null", O_RDONLY | O_CLOEXEC);
|
||
|
|
if (devnull >= 0) {
|
||
|
|
dup2(devnull, STDIN_FILENO);
|
||
|
|
if (devnull > 2) close(devnull);
|
||
|
|
}
|
||
|
|
execl(script, script, action, apn && *apn ? apn : "ims", (char *)NULL);
|
||
|
|
_exit(127);
|
||
|
|
}
|
||
|
|
close(pfd[1]);
|
||
|
|
char buf[512];
|
||
|
|
size_t off = 0;
|
||
|
|
while (off + 1 < sizeof(buf)) {
|
||
|
|
ssize_t r = read(pfd[0], buf + off, sizeof(buf) - 1 - off);
|
||
|
|
if (r <= 0) {
|
||
|
|
if (r < 0 && errno == EINTR) continue;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
off += (size_t)r;
|
||
|
|
}
|
||
|
|
buf[off] = '\0';
|
||
|
|
close(pfd[0]);
|
||
|
|
|
||
|
|
int status = 0;
|
||
|
|
if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
char *line = buf;
|
||
|
|
while (line && *line) {
|
||
|
|
char *nl = strchr(line, '\n');
|
||
|
|
if (nl) *nl = '\0';
|
||
|
|
if (strncmp(line, "IFACE=", 6) == 0 && out_iface) {
|
||
|
|
ims_str_copy(out_iface, iface_cap, line + 6);
|
||
|
|
} else if (strncmp(line, "IP=", 3) == 0 && out_ip) {
|
||
|
|
ims_str_copy(out_ip, ip_cap, line + 3);
|
||
|
|
}
|
||
|
|
line = nl ? nl + 1 : NULL;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void notify_ui_hooks(void) {
|
||
|
|
if (getenv("BANANARCHY_IMS_NO_UI_NOTIFY")) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (access("/usr/share/sxmo/default_hooks/sxmo_hook_statusbar.sh", X_OK) == 0) {
|
||
|
|
pid_t pid = fork();
|
||
|
|
if (pid == 0) {
|
||
|
|
int devnull = open("/dev/null", O_RDWR | O_CLOEXEC);
|
||
|
|
if (devnull >= 0) {
|
||
|
|
dup2(devnull, STDIN_FILENO);
|
||
|
|
dup2(devnull, STDOUT_FILENO);
|
||
|
|
dup2(devnull, STDERR_FILENO);
|
||
|
|
if (devnull > 2) close(devnull);
|
||
|
|
}
|
||
|
|
execl("/bin/sh", "sh", "-c",
|
||
|
|
"PATH=\"$HOME/.config/sxmo/hooks:/usr/share/sxmo/default_hooks:$PATH\" "
|
||
|
|
"sxmo_hook_statusbar.sh modem >/dev/null 2>&1 || true",
|
||
|
|
(char *)NULL);
|
||
|
|
_exit(0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void stop_vowifi_child(void) {
|
||
|
|
if (g_vowifi_pid <= 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
kill(g_vowifi_pid, SIGTERM);
|
||
|
|
for (int i = 0; i < 20; i++) {
|
||
|
|
int st = 0;
|
||
|
|
pid_t w = waitpid(g_vowifi_pid, &st, WNOHANG);
|
||
|
|
if (w == g_vowifi_pid || (w < 0 && errno == ECHILD)) {
|
||
|
|
g_vowifi_pid = -1;
|
||
|
|
g_vowifi_running_mode[0] = '\0';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
usleep(50000);
|
||
|
|
}
|
||
|
|
kill(g_vowifi_pid, SIGKILL);
|
||
|
|
(void)waitpid(g_vowifi_pid, NULL, 0);
|
||
|
|
g_vowifi_pid = -1;
|
||
|
|
g_vowifi_running_mode[0] = '\0';
|
||
|
|
}
|
||
|
|
|
||
|
|
static int start_vowifi_child(const char *mode, const char *iface, const char *local_ip) {
|
||
|
|
const char *bin = ims_vowifi_bin();
|
||
|
|
if (access(bin, X_OK) != 0) {
|
||
|
|
ims_str_copy(g_state.last_error, sizeof(g_state.last_error),
|
||
|
|
"bananarchy-vowifi binary missing");
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
if (g_vowifi_pid > 0) {
|
||
|
|
if (strcmp(g_vowifi_running_mode, mode) == 0 && kill(g_vowifi_pid, 0) == 0) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
stop_vowifi_child();
|
||
|
|
}
|
||
|
|
|
||
|
|
pid_t pid = fork();
|
||
|
|
if (pid < 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
if (pid == 0) {
|
||
|
|
char arg_mode[32];
|
||
|
|
snprintf(arg_mode, sizeof(arg_mode), "--mode=%s", mode);
|
||
|
|
if (iface && *iface) {
|
||
|
|
setenv("BANANARCHY_IMS_NET_IFACE", iface, 1);
|
||
|
|
}
|
||
|
|
if (local_ip && *local_ip) {
|
||
|
|
setenv("BANANARCHY_IMS_NET_IP", local_ip, 1);
|
||
|
|
}
|
||
|
|
execl(bin, bin, arg_mode, (char *)NULL);
|
||
|
|
_exit(127);
|
||
|
|
}
|
||
|
|
g_vowifi_pid = pid;
|
||
|
|
ims_str_copy(g_vowifi_running_mode, sizeof(g_vowifi_running_mode), mode);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void sync_vowifi_child_status(void) {
|
||
|
|
if (g_vowifi_pid <= 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
char sock_path[PATH_MAX];
|
||
|
|
ims_state_file_path(sock_path, sizeof(sock_path), "vowifi.sock");
|
||
|
|
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||
|
|
if (fd < 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
struct sockaddr_un addr;
|
||
|
|
memset(&addr, 0, sizeof(addr));
|
||
|
|
addr.sun_family = AF_UNIX;
|
||
|
|
if (strlen(sock_path) >= sizeof(addr.sun_path)) {
|
||
|
|
close(fd);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
memcpy(addr.sun_path, sock_path, strlen(sock_path) + 1);
|
||
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
||
|
|
close(fd);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const char *q = "STATUS\n";
|
||
|
|
if (write(fd, q, strlen(q)) <= 0) {
|
||
|
|
close(fd);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
struct pollfd pfd;
|
||
|
|
pfd.fd = fd;
|
||
|
|
pfd.events = POLLIN;
|
||
|
|
pfd.revents = 0;
|
||
|
|
char rsp[512];
|
||
|
|
rsp[0] = '\0';
|
||
|
|
if (poll(&pfd, 1, 250) > 0) {
|
||
|
|
ssize_t n = read(fd, rsp, sizeof(rsp) - 1);
|
||
|
|
if (n > 0) {
|
||
|
|
rsp[n] = '\0';
|
||
|
|
const char *reg = strstr(rsp, "registered=");
|
||
|
|
const char *ipv6 = strstr(rsp, "ipv6=");
|
||
|
|
const char *pcscf = strstr(rsp, "pcscf=");
|
||
|
|
const char *tun = strstr(rsp, "tun_ip=");
|
||
|
|
const char *epdg = strstr(rsp, "epdg=");
|
||
|
|
int is_reg = reg ? atoi(reg + 11) : 0;
|
||
|
|
if (strcmp(g_vowifi_running_mode, "vowifi") == 0) {
|
||
|
|
g_state.vowifi_registered = is_reg;
|
||
|
|
if (ipv6) g_state.vowifi_ipv6_epdg = atoi(ipv6 + 5);
|
||
|
|
} else if (strcmp(g_vowifi_running_mode, "volte") == 0) {
|
||
|
|
g_state.volte_registered = is_reg;
|
||
|
|
}
|
||
|
|
if (pcscf) {
|
||
|
|
char tmp[64];
|
||
|
|
size_t i = 0;
|
||
|
|
while (pcscf[6 + i] && !isspace((unsigned char)pcscf[6 + i]) && i + 1 < sizeof(tmp)) {
|
||
|
|
tmp[i] = pcscf[6 + i];
|
||
|
|
i++;
|
||
|
|
}
|
||
|
|
tmp[i] = '\0';
|
||
|
|
ims_str_copy(g_state.pcscf_addr, sizeof(g_state.pcscf_addr), tmp);
|
||
|
|
}
|
||
|
|
if (tun) {
|
||
|
|
char tmp[64];
|
||
|
|
size_t i = 0;
|
||
|
|
while (tun[7 + i] && !isspace((unsigned char)tun[7 + i]) && i + 1 < sizeof(tmp)) {
|
||
|
|
tmp[i] = tun[7 + i];
|
||
|
|
i++;
|
||
|
|
}
|
||
|
|
tmp[i] = '\0';
|
||
|
|
ims_str_copy(g_state.tun_ip, sizeof(g_state.tun_ip), tmp);
|
||
|
|
}
|
||
|
|
if (epdg) {
|
||
|
|
char tmp[128];
|
||
|
|
size_t i = 0;
|
||
|
|
while (epdg[5 + i] && !isspace((unsigned char)epdg[5 + i]) && i + 1 < sizeof(tmp)) {
|
||
|
|
tmp[i] = epdg[5 + i];
|
||
|
|
i++;
|
||
|
|
}
|
||
|
|
tmp[i] = '\0';
|
||
|
|
ims_str_copy(g_state.epdg_host, sizeof(g_state.epdg_host), tmp);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
close(fd);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int ensure_qrtr(void) {
|
||
|
|
if (!g_qrtr_ready || g_qrtr.fd < 0) {
|
||
|
|
if (ims_qrtr_open(&g_qrtr) != 0) {
|
||
|
|
g_qrtr_ready = 0;
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
g_qrtr_ready = 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void ensure_imsdcm(void) {
|
||
|
|
if (!g_imsdcm_ready || g_imsdcm.fd < 0) {
|
||
|
|
if (ims_qrtr_open(&g_imsdcm) == 0) {
|
||
|
|
g_imsdcm_ready = 1;
|
||
|
|
(void)ims_qrtr_register_server(&g_imsdcm, IMS_QMI_SVC_IMSDCM, 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void close_imsdcm(void) {
|
||
|
|
if (g_imsdcm_ready || g_imsdcm.fd >= 0) {
|
||
|
|
ims_qrtr_close(&g_imsdcm);
|
||
|
|
g_imsdcm_ready = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static int is_wifi_connected(void) {
|
||
|
|
const char *mock_wifi = getenv("BANANARCHY_VOWIFI_WIFI_UP");
|
||
|
|
if (mock_wifi && *mock_wifi) {
|
||
|
|
return (strcmp(mock_wifi, "1") == 0) ? 1 : 0;
|
||
|
|
}
|
||
|
|
char opstate[32] = "";
|
||
|
|
if (read_first_line("/sys/class/net/wlan0/operstate", opstate, sizeof(opstate)) == 0 &&
|
||
|
|
strncmp(opstate, "up", 2) == 0) {
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void detect_network_state(int *out_4g_only, char *out_tech, size_t tech_cap,
|
||
|
|
char *out_mcc, char *out_mnc) {
|
||
|
|
*out_4g_only = 0;
|
||
|
|
ims_str_copy(out_tech, tech_cap, "unknown");
|
||
|
|
out_mcc[0] = '\0';
|
||
|
|
out_mnc[0] = '\0';
|
||
|
|
|
||
|
|
const char *mock_tech = getenv("BANANARCHY_MOCK_ACCESS_TECH");
|
||
|
|
if (mock_tech && *mock_tech) {
|
||
|
|
ims_str_copy(out_tech, tech_cap, mock_tech);
|
||
|
|
if (strcmp(mock_tech, "lte-only") == 0 || strcmp(mock_tech, "lte") == 0) {
|
||
|
|
*out_4g_only = 1;
|
||
|
|
}
|
||
|
|
const char *mmcc = getenv("BANANARCHY_MOCK_MCC");
|
||
|
|
const char *mmnc = getenv("BANANARCHY_MOCK_MNC");
|
||
|
|
if (mmcc) ims_str_copy(out_mcc, 8, mmcc);
|
||
|
|
if (mmnc) ims_str_copy(out_mnc, 8, mmnc);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (ensure_qrtr() == 0) {
|
||
|
|
int reg = 0, lte_only = 0, has_cs = 0;
|
||
|
|
if (ims_qmi_nas_get_serving(&g_qrtr, next_txn(), ®, <e_only, &has_cs,
|
||
|
|
out_tech, tech_cap, out_mcc, out_mnc) == 0) {
|
||
|
|
*out_4g_only = lte_only;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static int pdc_send_load_and_select(const struct ims_mbn_info *info, int activate_now) {
|
||
|
|
if (!info || !info->valid || !info->is_sw_config || ensure_qrtr() != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
struct ims_qrtr_addr pdc_addr;
|
||
|
|
if (ims_qrtr_lookup(&g_qrtr, IMS_QMI_SVC_PDC, 0, 400, &pdc_addr) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
int fd = open(info->path, O_RDONLY | O_CLOEXEC);
|
||
|
|
if (fd < 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
uint8_t *file_buf = (uint8_t *)malloc(info->file_size);
|
||
|
|
if (!file_buf) {
|
||
|
|
close(fd);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
size_t off = 0;
|
||
|
|
while (off < info->file_size) {
|
||
|
|
ssize_t r = read(fd, file_buf + off, info->file_size - off);
|
||
|
|
if (r <= 0) {
|
||
|
|
if (r < 0 && errno == EINTR) continue;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
off += (size_t)r;
|
||
|
|
}
|
||
|
|
close(fd);
|
||
|
|
if (off != info->file_size) {
|
||
|
|
free(file_buf);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* Stream in 1024-byte QMI_PDC_LOAD_CONFIG (0x0026) chunks over QRTR. */
|
||
|
|
const size_t chunk_sz = 1024;
|
||
|
|
size_t sent = 0;
|
||
|
|
int rc = 0;
|
||
|
|
while (sent < off) {
|
||
|
|
size_t n = off - sent;
|
||
|
|
if (n > chunk_sz) n = chunk_sz;
|
||
|
|
|
||
|
|
uint8_t tlv[1280];
|
||
|
|
size_t toff = 0;
|
||
|
|
/* TLV 0x01: config_type (1 = SW) + id_len + id + total_size + frame */
|
||
|
|
uint8_t load_hdr[128];
|
||
|
|
size_t idlen = strlen(info->id);
|
||
|
|
if (idlen > 64) idlen = 64;
|
||
|
|
load_hdr[0] = 0x01U; /* QMI_PDC_CONFIG_TYPE_SOFTWARE */
|
||
|
|
load_hdr[1] = (uint8_t)idlen;
|
||
|
|
memcpy(load_hdr + 2, info->id, idlen);
|
||
|
|
(void)ims_qmi_tlv_add(tlv, sizeof(tlv), &toff, 0x01U, load_hdr, (uint16_t)(2 + idlen));
|
||
|
|
uint32_t total32 = (uint32_t)off;
|
||
|
|
(void)ims_qmi_tlv_add(tlv, sizeof(tlv), &toff, 0x02U, &total32, 4);
|
||
|
|
uint8_t frame[1100];
|
||
|
|
ims_qmi_put_le16(frame, (uint16_t)n);
|
||
|
|
memcpy(frame + 2, file_buf + sent, n);
|
||
|
|
(void)ims_qmi_tlv_add(tlv, sizeof(tlv), &toff, 0x03U, frame, (uint16_t)(2 + n));
|
||
|
|
|
||
|
|
uint8_t rsp[512];
|
||
|
|
size_t rlen = 0;
|
||
|
|
if (ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_LOAD_CONFIG,
|
||
|
|
tlv, toff, 1500, rsp, sizeof(rsp), &rlen) != 0 ||
|
||
|
|
ims_qmi_check_result(rsp, rlen, NULL) != 0) {
|
||
|
|
rc = -1;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
sent += n;
|
||
|
|
}
|
||
|
|
free(file_buf);
|
||
|
|
if (rc != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
ims_str_copy(g_pdc_loaded_id, sizeof(g_pdc_loaded_id), info->id);
|
||
|
|
|
||
|
|
if (!activate_now) {
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
uint8_t tlv[128];
|
||
|
|
size_t toff = 0;
|
||
|
|
uint8_t sel[96];
|
||
|
|
size_t idlen = strlen(info->id);
|
||
|
|
if (idlen > 64) idlen = 64;
|
||
|
|
sel[0] = 0x01U; /* SOFTWARE */
|
||
|
|
sel[1] = (uint8_t)idlen;
|
||
|
|
memcpy(sel + 2, info->id, idlen);
|
||
|
|
(void)ims_qmi_tlv_add(tlv, sizeof(tlv), &toff, 0x01U, sel, (uint16_t)(2 + idlen));
|
||
|
|
uint32_t sub_id = (uint32_t)(g_cfg.ims_sim_slot > 0 ? g_cfg.ims_sim_slot - 1 : 0);
|
||
|
|
(void)ims_qmi_tlv_add(tlv, sizeof(tlv), &toff, 0x10U, &sub_id, 4);
|
||
|
|
|
||
|
|
uint8_t rsp[512];
|
||
|
|
size_t rlen = 0;
|
||
|
|
if (ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_SET_SELECTED_CONFIG,
|
||
|
|
tlv, toff, 1200, rsp, sizeof(rsp), &rlen) != 0 ||
|
||
|
|
ims_qmi_check_result(rsp, rlen, NULL) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
|
||
|
|
uint8_t act_tlv[32];
|
||
|
|
size_t aoff = 0;
|
||
|
|
uint32_t ctype = 1; /* SW */
|
||
|
|
(void)ims_qmi_tlv_add(act_tlv, sizeof(act_tlv), &aoff, 0x01U, &ctype, 4);
|
||
|
|
(void)ims_qmi_tlv_add(act_tlv, sizeof(act_tlv), &aoff, 0x10U, &sub_id, 4);
|
||
|
|
if (ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_ACTIVATE_CONFIG,
|
||
|
|
act_tlv, aoff, 1500, rsp, sizeof(rsp), &rlen) != 0 ||
|
||
|
|
ims_qmi_check_result(rsp, rlen, NULL) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static int pdc_select_only(const char *id) {
|
||
|
|
if (!id || !*id || ensure_qrtr() != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
struct ims_qrtr_addr pdc_addr;
|
||
|
|
if (ims_qrtr_lookup(&g_qrtr, IMS_QMI_SVC_PDC, 0, 400, &pdc_addr) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
uint8_t tlv[128];
|
||
|
|
size_t toff = 0;
|
||
|
|
uint8_t sel[96];
|
||
|
|
size_t idlen = strlen(id);
|
||
|
|
if (idlen > 64) idlen = 64;
|
||
|
|
sel[0] = 0x01U; /* SOFTWARE */
|
||
|
|
sel[1] = (uint8_t)idlen;
|
||
|
|
memcpy(sel + 2, id, idlen);
|
||
|
|
(void)ims_qmi_tlv_add(tlv, sizeof(tlv), &toff, 0x01U, sel, (uint16_t)(2 + idlen));
|
||
|
|
uint32_t sub_id = (uint32_t)(g_cfg.ims_sim_slot > 0 ? g_cfg.ims_sim_slot - 1 : 0);
|
||
|
|
(void)ims_qmi_tlv_add(tlv, sizeof(tlv), &toff, 0x10U, &sub_id, 4);
|
||
|
|
|
||
|
|
uint8_t rsp[512];
|
||
|
|
size_t rlen = 0;
|
||
|
|
if (ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_SET_SELECTED_CONFIG,
|
||
|
|
tlv, toff, 1200, rsp, sizeof(rsp), &rlen) != 0 ||
|
||
|
|
ims_qmi_check_result(rsp, rlen, NULL) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
uint8_t act_tlv[32];
|
||
|
|
size_t aoff = 0;
|
||
|
|
uint32_t ctype = 1; /* SW */
|
||
|
|
(void)ims_qmi_tlv_add(act_tlv, sizeof(act_tlv), &aoff, 0x01U, &ctype, 4);
|
||
|
|
(void)ims_qmi_tlv_add(act_tlv, sizeof(act_tlv), &aoff, 0x10U, &sub_id, 4);
|
||
|
|
if (ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_ACTIVATE_CONFIG,
|
||
|
|
act_tlv, aoff, 1500, rsp, sizeof(rsp), &rlen) != 0 ||
|
||
|
|
ims_qmi_check_result(rsp, rlen, NULL) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void pdc_delete_or_reset(const char *id, int is_reset) {
|
||
|
|
if (ensure_qrtr() != 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
struct ims_qrtr_addr pdc_addr;
|
||
|
|
if (ims_qrtr_lookup(&g_qrtr, IMS_QMI_SVC_PDC, 0, 300, &pdc_addr) != 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
uint8_t rsp[512];
|
||
|
|
size_t rlen = 0;
|
||
|
|
if (is_reset) {
|
||
|
|
uint8_t tlvs[32];
|
||
|
|
size_t toff = 0;
|
||
|
|
uint32_t ctype = 1;
|
||
|
|
(void)ims_qmi_tlv_add(tlvs, sizeof(tlvs), &toff, 0x01U, &ctype, 4);
|
||
|
|
(void)ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_DEACTIVATE_CONFIG,
|
||
|
|
tlvs, toff, 800, rsp, sizeof(rsp), &rlen);
|
||
|
|
(void)ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_RESET,
|
||
|
|
NULL, 0, 800, rsp, sizeof(rsp), &rlen);
|
||
|
|
} else if (id && *id) {
|
||
|
|
uint8_t tlvs[128];
|
||
|
|
size_t toff = 0;
|
||
|
|
uint32_t ctype = 1;
|
||
|
|
(void)ims_qmi_tlv_add(tlvs, sizeof(tlvs), &toff, 0x01U, &ctype, 4);
|
||
|
|
(void)ims_qmi_tlv_add(tlvs, sizeof(tlvs), &toff, 0x10U, id, (uint16_t)strlen(id));
|
||
|
|
(void)ims_qmi_transact(&g_qrtr, &pdc_addr, next_txn(), IMS_QMI_PDC_DELETE_CONFIG,
|
||
|
|
tlvs, toff, 800, rsp, sizeof(rsp), &rlen);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void sync_mbn_metadata(void) {
|
||
|
|
if (!g_cfg.active_mbn[0]) {
|
||
|
|
g_state.active_mbn[0] = '\0';
|
||
|
|
g_state.mbn_description[0] = '\0';
|
||
|
|
g_state.mbn_version[0] = '\0';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
ims_str_copy(g_state.active_mbn, sizeof(g_state.active_mbn), g_cfg.active_mbn);
|
||
|
|
char p[PATH_MAX];
|
||
|
|
snprintf(p, sizeof(p), "%s/%s.mbn", ims_profiles_dir(), g_cfg.active_mbn);
|
||
|
|
struct ims_mbn_info info;
|
||
|
|
if (ims_mbn_inspect_file(p, &info) == 0 && info.valid) {
|
||
|
|
ims_str_copy(g_state.mbn_description, sizeof(g_state.mbn_description), info.description);
|
||
|
|
ims_str_copy(g_state.mbn_version, sizeof(g_state.mbn_version), info.version_str);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void reconcile_all(void) {
|
||
|
|
struct ims_runtime_state prev = g_state;
|
||
|
|
ims_config_load(&g_cfg, NULL);
|
||
|
|
ims_str_copy(g_state.volte_mode, sizeof(g_state.volte_mode), g_cfg.volte_mode);
|
||
|
|
ims_str_copy(g_state.vowifi_mode, sizeof(g_state.vowifi_mode), g_cfg.vowifi_mode);
|
||
|
|
g_state.vowifi_sim = g_cfg.ims_sim_slot;
|
||
|
|
|
||
|
|
int network_4g_only = 0;
|
||
|
|
char tech[32], mcc[8], mnc[8];
|
||
|
|
detect_network_state(&network_4g_only, tech, sizeof(tech), mcc, mnc);
|
||
|
|
g_state.network_4g_only = network_4g_only;
|
||
|
|
ims_str_copy(g_state.access_tech, sizeof(g_state.access_tech), tech);
|
||
|
|
if (mcc[0]) ims_str_copy(g_state.mcc, sizeof(g_state.mcc), mcc);
|
||
|
|
if (mnc[0]) ims_str_copy(g_state.mnc, sizeof(g_state.mnc), mnc);
|
||
|
|
|
||
|
|
/* Auto-match installed SW MBN if enabled and none selected yet. */
|
||
|
|
if (g_cfg.auto_install_mbn && !g_cfg.active_mbn[0] && mcc[0] && mnc[0]) {
|
||
|
|
struct ims_mbn_info matched;
|
||
|
|
if (ims_profile_match_plmn(mcc, mnc, &matched)) {
|
||
|
|
ims_str_copy(g_cfg.active_mbn, sizeof(g_cfg.active_mbn), matched.id);
|
||
|
|
(void)ims_config_save(&g_cfg, NULL);
|
||
|
|
if (strcmp(g_pdc_loaded_id, matched.id) == 0) {
|
||
|
|
(void)pdc_select_only(matched.id);
|
||
|
|
} else {
|
||
|
|
(void)pdc_send_load_and_select(&matched, 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
sync_mbn_metadata();
|
||
|
|
|
||
|
|
/* Effective activation policy: disabled by default UNLESS 4G-only. */
|
||
|
|
int effective_volte = (strcmp(g_cfg.volte_mode, "on") == 0) ||
|
||
|
|
(strcmp(g_cfg.volte_mode, "auto") == 0 && network_4g_only == 1);
|
||
|
|
int effective_vowifi = (strcmp(g_cfg.vowifi_mode, "on") == 0) ||
|
||
|
|
(strcmp(g_cfg.vowifi_mode, "auto") == 0 && network_4g_only == 1);
|
||
|
|
|
||
|
|
int qrtr_ok = (ensure_qrtr() == 0);
|
||
|
|
struct ims_qrtr_addr tmp_addr;
|
||
|
|
int has_ims = qrtr_ok && (ims_qrtr_lookup(&g_qrtr, IMS_QMI_SVC_IMS, 0, 200, &tmp_addr) == 0);
|
||
|
|
int has_imsa = qrtr_ok && (ims_qrtr_lookup(&g_qrtr, IMS_QMI_SVC_IMSA, 0, 200, &tmp_addr) == 0);
|
||
|
|
int has_dsd = qrtr_ok && (ims_qrtr_lookup(&g_qrtr, IMS_QMI_SVC_DSD, 0, 200, &tmp_addr) == 0);
|
||
|
|
g_state.volte_capable = (has_ims || has_imsa || access(ims_vowifi_bin(), X_OK) == 0) ? 1 : 0;
|
||
|
|
|
||
|
|
int wifi_up = is_wifi_connected();
|
||
|
|
int want_vowifi = (effective_vowifi && wifi_up) ? 1 : 0;
|
||
|
|
int want_userspace_volte = 0;
|
||
|
|
|
||
|
|
/* 1. Reconcile VoLTE */
|
||
|
|
if (!effective_volte) {
|
||
|
|
g_state.volte_active = 0;
|
||
|
|
if (strcmp(g_vowifi_running_mode, "volte") == 0) {
|
||
|
|
stop_vowifi_child();
|
||
|
|
}
|
||
|
|
if (g_baseband_ims_set && has_ims) {
|
||
|
|
(void)ims_qmi_ims_set_services(&g_qrtr, next_txn(), 0, 0, 0);
|
||
|
|
g_baseband_ims_set = 0;
|
||
|
|
}
|
||
|
|
if (g_bearer_up) {
|
||
|
|
char dummy_if[32], dummy_ip[64];
|
||
|
|
(void)run_bearer_cmd("down", g_cfg.ims_apn, dummy_if, sizeof(dummy_if),
|
||
|
|
dummy_ip, sizeof(dummy_ip));
|
||
|
|
g_bearer_up = 0;
|
||
|
|
g_state.ims_apn_iface[0] = '\0';
|
||
|
|
g_state.ims_apn_ip[0] = '\0';
|
||
|
|
}
|
||
|
|
close_imsdcm();
|
||
|
|
g_state.volte_registered = 0;
|
||
|
|
g_baseband_try_since_ms = 0;
|
||
|
|
ims_str_copy(g_state.volte_backend, sizeof(g_state.volte_backend), "none");
|
||
|
|
} else {
|
||
|
|
g_state.volte_active = 1;
|
||
|
|
if (qrtr_ok) {
|
||
|
|
ensure_imsdcm();
|
||
|
|
}
|
||
|
|
if (has_dsd) {
|
||
|
|
(void)ims_qmi_dsd_set_ims_apn(&g_qrtr, next_txn(), g_cfg.ims_apn);
|
||
|
|
}
|
||
|
|
|
||
|
|
int force_userspace = (strcmp(g_cfg.volte_backend, "userspace") == 0);
|
||
|
|
int force_baseband = (strcmp(g_cfg.volte_backend, "baseband") == 0);
|
||
|
|
|
||
|
|
if (!force_userspace && has_ims && has_imsa) {
|
||
|
|
ims_str_copy(g_state.volte_backend, sizeof(g_state.volte_backend), "baseband");
|
||
|
|
if (!g_baseband_ims_set) {
|
||
|
|
(void)ims_qmi_ims_set_services(&g_qrtr, next_txn(), 1, 0, 1);
|
||
|
|
g_baseband_ims_set = 1;
|
||
|
|
g_baseband_try_since_ms = now_ms();
|
||
|
|
}
|
||
|
|
int imsa_reg = 0, imsa_volte = 0, imsa_vowifi = 0;
|
||
|
|
if (ims_qmi_imsa_get_status(&g_qrtr, next_txn(), &imsa_reg, &imsa_volte,
|
||
|
|
&imsa_vowifi) == 0 && imsa_reg && imsa_volte) {
|
||
|
|
g_state.volte_registered = 1;
|
||
|
|
} else {
|
||
|
|
g_state.volte_registered = 0;
|
||
|
|
/* In auto mode, fall back to userspace SIP/RTP on ims APN if baseband doesn't register. */
|
||
|
|
if (!force_baseband && g_baseband_try_since_ms > 0 &&
|
||
|
|
(now_ms() - g_baseband_try_since_ms) > 8000 && !want_vowifi &&
|
||
|
|
strstr(tech, "lte") != NULL) {
|
||
|
|
want_userspace_volte = 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (!force_baseband) {
|
||
|
|
want_userspace_volte = (!want_vowifi && strstr(tech, "lte") != NULL) ? 1 : 0;
|
||
|
|
ims_str_copy(g_state.volte_backend, sizeof(g_state.volte_backend), "userspace");
|
||
|
|
} else {
|
||
|
|
ims_str_copy(g_state.volte_backend, sizeof(g_state.volte_backend), "baseband");
|
||
|
|
g_state.volte_registered = 0;
|
||
|
|
ims_str_copy(g_state.last_error, sizeof(g_state.last_error),
|
||
|
|
"Baseband QMI_IMS/IMSA unavailable");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/* 2. Reconcile VoWiFi vs Userspace VoLTE worker process */
|
||
|
|
if (want_vowifi) {
|
||
|
|
g_state.vowifi_active = 1;
|
||
|
|
ims_str_copy(g_state.tun_iface, sizeof(g_state.tun_iface), "tun_ims");
|
||
|
|
if (start_vowifi_child("vowifi", NULL, NULL) == 0) {
|
||
|
|
sync_vowifi_child_status();
|
||
|
|
}
|
||
|
|
} else if (want_userspace_volte) {
|
||
|
|
g_state.vowifi_active = 0;
|
||
|
|
g_state.vowifi_registered = 0;
|
||
|
|
ims_str_copy(g_state.volte_backend, sizeof(g_state.volte_backend), "userspace");
|
||
|
|
if (!g_bearer_up) {
|
||
|
|
if (run_bearer_cmd("up", g_cfg.ims_apn, g_state.ims_apn_iface,
|
||
|
|
sizeof(g_state.ims_apn_iface), g_state.ims_apn_ip,
|
||
|
|
sizeof(g_state.ims_apn_ip)) == 0 &&
|
||
|
|
g_state.ims_apn_ip[0] != '\0') {
|
||
|
|
g_bearer_up = 1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (g_bearer_up && g_state.ims_apn_ip[0] != '\0') {
|
||
|
|
if (start_vowifi_child("volte", g_state.ims_apn_iface, g_state.ims_apn_ip) == 0) {
|
||
|
|
sync_vowifi_child_status();
|
||
|
|
}
|
||
|
|
} else if (strcmp(g_vowifi_running_mode, "volte") == 0) {
|
||
|
|
stop_vowifi_child();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
g_state.vowifi_active = 0;
|
||
|
|
g_state.vowifi_registered = 0;
|
||
|
|
g_state.vowifi_ipv6_epdg = 0;
|
||
|
|
g_state.tun_ip[0] = '\0';
|
||
|
|
g_state.tun_iface[0] = '\0';
|
||
|
|
if (!want_userspace_volte) {
|
||
|
|
stop_vowifi_child();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
(void)ims_state_write(&g_state);
|
||
|
|
|
||
|
|
if (prev.volte_active != g_state.volte_active ||
|
||
|
|
prev.volte_registered != g_state.volte_registered ||
|
||
|
|
prev.vowifi_active != g_state.vowifi_active ||
|
||
|
|
prev.vowifi_registered != g_state.vowifi_registered ||
|
||
|
|
prev.network_4g_only != g_state.network_4g_only) {
|
||
|
|
notify_ui_hooks();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void handle_imsdcm_qrtr_packet(void) {
|
||
|
|
struct ims_qrtr_addr src;
|
||
|
|
uint8_t buf[2048];
|
||
|
|
ssize_t n = ims_qrtr_recv(&g_imsdcm, &src, buf, sizeof(buf), 0);
|
||
|
|
if (n < 7) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
uint8_t flags = buf[0];
|
||
|
|
uint16_t txn = ims_qmi_get_le16(buf + 1);
|
||
|
|
uint16_t msg_id = ims_qmi_get_le16(buf + 3);
|
||
|
|
uint16_t tlen = ims_qmi_get_le16(buf + 5);
|
||
|
|
if (7U + (size_t)tlen > (size_t)n) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (flags == IMS_QMI_FLAG_IND) {
|
||
|
|
/* IMSA / NAS indication: reconcile state immediately. */
|
||
|
|
reconcile_all();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (flags != IMS_QMI_FLAG_REQ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const uint8_t *tlvs = buf + 7;
|
||
|
|
uint8_t r_tlvs[256];
|
||
|
|
size_t roff = 0;
|
||
|
|
uint32_t res = 0; /* QMI_RESULT_SUCCESS */
|
||
|
|
|
||
|
|
if (msg_id == IMS_QMI_IMSDCM_PDP_ACTIVATE) {
|
||
|
|
char apn[64];
|
||
|
|
ims_str_copy(apn, sizeof(apn), g_cfg.ims_apn);
|
||
|
|
uint16_t al = 0;
|
||
|
|
const uint8_t *av = ims_qmi_tlv_find(tlvs, tlen, 0x03U, &al);
|
||
|
|
if (av && al >= 2) {
|
||
|
|
size_t slen = av[0];
|
||
|
|
if (slen > 0 && slen < al && slen < sizeof(apn)) {
|
||
|
|
memcpy(apn, av + 1, slen);
|
||
|
|
apn[slen] = '\0';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (run_bearer_cmd("up", apn, g_state.ims_apn_iface, sizeof(g_state.ims_apn_iface),
|
||
|
|
g_state.ims_apn_ip, sizeof(g_state.ims_apn_ip)) == 0) {
|
||
|
|
g_bearer_up = 1;
|
||
|
|
(void)ims_state_write(&g_state);
|
||
|
|
} else {
|
||
|
|
res = 0x00010001U; /* FAILURE */
|
||
|
|
}
|
||
|
|
(void)ims_qmi_tlv_add(r_tlvs, sizeof(r_tlvs), &roff, 0x02U, &res, 4);
|
||
|
|
if (g_state.ims_apn_ip[0]) {
|
||
|
|
(void)ims_qmi_tlv_add(r_tlvs, sizeof(r_tlvs), &roff, 0x10U,
|
||
|
|
g_state.ims_apn_ip, (uint16_t)strlen(g_state.ims_apn_ip));
|
||
|
|
}
|
||
|
|
} else if (msg_id == IMS_QMI_IMSDCM_PDP_DEACTIVATE) {
|
||
|
|
char dummy_if[32], dummy_ip[64];
|
||
|
|
(void)run_bearer_cmd("down", g_cfg.ims_apn, dummy_if, sizeof(dummy_if),
|
||
|
|
dummy_ip, sizeof(dummy_ip));
|
||
|
|
g_bearer_up = 0;
|
||
|
|
g_state.ims_apn_iface[0] = '\0';
|
||
|
|
g_state.ims_apn_ip[0] = '\0';
|
||
|
|
(void)ims_state_write(&g_state);
|
||
|
|
(void)ims_qmi_tlv_add(r_tlvs, sizeof(r_tlvs), &roff, 0x02U, &res, 4);
|
||
|
|
} else {
|
||
|
|
(void)ims_qmi_tlv_add(r_tlvs, sizeof(r_tlvs), &roff, 0x02U, &res, 4);
|
||
|
|
}
|
||
|
|
|
||
|
|
uint8_t rsp[512];
|
||
|
|
size_t rsp_len = ims_qmi_pack_msg(rsp, sizeof(rsp), IMS_QMI_FLAG_RSP, txn, msg_id,
|
||
|
|
r_tlvs, roff);
|
||
|
|
if (rsp_len > 0) {
|
||
|
|
(void)ims_qrtr_send(&g_imsdcm, &src, rsp, rsp_len);
|
||
|
|
}
|
||
|
|
if (msg_id == IMS_QMI_IMSDCM_PDP_ACTIVATE || msg_id == IMS_QMI_IMSDCM_PDP_DEACTIVATE) {
|
||
|
|
uint8_t ind[512];
|
||
|
|
size_t ind_len = ims_qmi_pack_msg(ind, sizeof(ind), IMS_QMI_FLAG_IND, 1, msg_id,
|
||
|
|
r_tlvs, roff);
|
||
|
|
if (ind_len > 0) {
|
||
|
|
(void)ims_qrtr_send(&g_imsdcm, &src, ind, ind_len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static int client_is_authorized_mutation(int cfd) {
|
||
|
|
const char *allow = getenv("BANANARCHY_IMS_ALLOW_NONROOT");
|
||
|
|
if (allow && strcmp(allow, "1") == 0) {
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
struct ucred uc;
|
||
|
|
socklen_t ulen = sizeof(uc);
|
||
|
|
if (getsockopt(cfd, SOL_SOCKET, SO_PEERCRED, &uc, &ulen) == 0) {
|
||
|
|
return (uc.uid == 0 || uc.uid == geteuid());
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static void reply_client(int cfd, const char *text) {
|
||
|
|
size_t len = strlen(text);
|
||
|
|
size_t off = 0;
|
||
|
|
while (off < len) {
|
||
|
|
ssize_t w = write(cfd, text + off, len - off);
|
||
|
|
if (w < 0) {
|
||
|
|
if (errno == EINTR) continue;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
off += (size_t)w;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static void handle_ctl_client(int srv_fd) {
|
||
|
|
int cfd = accept4(srv_fd, NULL, NULL, SOCK_CLOEXEC);
|
||
|
|
if (cfd < 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
char line[512];
|
||
|
|
ssize_t n = read(cfd, line, sizeof(line) - 1);
|
||
|
|
if (n <= 0) {
|
||
|
|
close(cfd);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
line[n] = '\0';
|
||
|
|
ims_trim_ascii(line);
|
||
|
|
|
||
|
|
if (strcmp(line, "STATUS") == 0) {
|
||
|
|
char rsp[1024];
|
||
|
|
snprintf(rsp, sizeof(rsp),
|
||
|
|
"OK volte_active=%d volte_reg=%d vowifi_active=%d vowifi_reg=%d "
|
||
|
|
"4g_only=%d tech=%s backend=%s\n",
|
||
|
|
g_state.volte_active, g_state.volte_registered,
|
||
|
|
g_state.vowifi_active, g_state.vowifi_registered,
|
||
|
|
g_state.network_4g_only, g_state.access_tech, g_state.volte_backend);
|
||
|
|
reply_client(cfd, rsp);
|
||
|
|
} else if (strcmp(line, "REFRESH") == 0 || strncmp(line, "NET_EVENT ", 10) == 0) {
|
||
|
|
reconcile_all();
|
||
|
|
reply_client(cfd, "OK\n");
|
||
|
|
} else if (strcmp(line, "AUTO_MATCH") == 0) {
|
||
|
|
if (!client_is_authorized_mutation(cfd)) {
|
||
|
|
reply_client(cfd, "ERR unauthorized\n");
|
||
|
|
} else {
|
||
|
|
int net4g = 0;
|
||
|
|
char tech[32], mcc[8], mnc[8];
|
||
|
|
detect_network_state(&net4g, tech, sizeof(tech), mcc, mnc);
|
||
|
|
struct ims_mbn_info m;
|
||
|
|
if (ims_profile_match_plmn(mcc, mnc, &m)) {
|
||
|
|
ims_str_copy(g_cfg.active_mbn, sizeof(g_cfg.active_mbn), m.id);
|
||
|
|
(void)ims_config_save(&g_cfg, NULL);
|
||
|
|
(void)pdc_send_load_and_select(&m, 1);
|
||
|
|
reconcile_all();
|
||
|
|
char rsp[256];
|
||
|
|
snprintf(rsp, sizeof(rsp), "OK MATCHED=%s PLMN=%s-%s\n", m.id, mcc, mnc);
|
||
|
|
reply_client(cfd, rsp);
|
||
|
|
} else {
|
||
|
|
reply_client(cfd, "ERR no matching profile\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (strncmp(line, "PDC_LOAD ", 9) == 0 || strncmp(line, "PDC_SELECT ", 11) == 0) {
|
||
|
|
if (!client_is_authorized_mutation(cfd)) {
|
||
|
|
reply_client(cfd, "ERR unauthorized\n");
|
||
|
|
} else {
|
||
|
|
int activate = (strncmp(line, "PDC_SELECT ", 11) == 0);
|
||
|
|
const char *id = activate ? line + 11 : line + 9;
|
||
|
|
char p[PATH_MAX];
|
||
|
|
snprintf(p, sizeof(p), "%s/%s.mbn", ims_profiles_dir(), id);
|
||
|
|
struct ims_mbn_info info;
|
||
|
|
if (ims_mbn_inspect_file(p, &info) == 0 && info.is_sw_config) {
|
||
|
|
int rc = activate ? pdc_select_only(info.id) : pdc_send_load_and_select(&info, 0);
|
||
|
|
if (activate && rc != 0) {
|
||
|
|
rc = pdc_send_load_and_select(&info, 1);
|
||
|
|
}
|
||
|
|
reconcile_all();
|
||
|
|
reply_client(cfd, rc == 0 ? "OK\n" : "OK stored_offline\n");
|
||
|
|
} else {
|
||
|
|
reply_client(cfd, "ERR invalid profile\n");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (strncmp(line, "PDC_DELETE ", 11) == 0) {
|
||
|
|
if (!client_is_authorized_mutation(cfd)) {
|
||
|
|
reply_client(cfd, "ERR unauthorized\n");
|
||
|
|
} else {
|
||
|
|
pdc_delete_or_reset(line + 11, 0);
|
||
|
|
reconcile_all();
|
||
|
|
reply_client(cfd, "OK\n");
|
||
|
|
}
|
||
|
|
} else if (strcmp(line, "PDC_RESET") == 0) {
|
||
|
|
if (!client_is_authorized_mutation(cfd)) {
|
||
|
|
reply_client(cfd, "ERR unauthorized\n");
|
||
|
|
} else {
|
||
|
|
pdc_delete_or_reset(NULL, 1);
|
||
|
|
reconcile_all();
|
||
|
|
reply_client(cfd, "OK\n");
|
||
|
|
}
|
||
|
|
} else if (strcmp(line, "STOP") == 0) {
|
||
|
|
if (!client_is_authorized_mutation(cfd)) {
|
||
|
|
reply_client(cfd, "ERR unauthorized\n");
|
||
|
|
} else {
|
||
|
|
reply_client(cfd, "OK\n");
|
||
|
|
g_stop = 1;
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
reply_client(cfd, "ERR unknown command\n");
|
||
|
|
}
|
||
|
|
close(cfd);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int open_ctl_socket(void) {
|
||
|
|
if (ims_mkdir_p(ims_state_dir(), 0755) != 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
char sock_path[PATH_MAX];
|
||
|
|
ims_state_file_path(sock_path, sizeof(sock_path), "imsd.sock");
|
||
|
|
unlink(sock_path);
|
||
|
|
|
||
|
|
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
||
|
|
if (fd < 0) {
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
struct sockaddr_un addr;
|
||
|
|
memset(&addr, 0, sizeof(addr));
|
||
|
|
addr.sun_family = AF_UNIX;
|
||
|
|
if (strlen(sock_path) >= sizeof(addr.sun_path)) {
|
||
|
|
close(fd);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
memcpy(addr.sun_path, sock_path, strlen(sock_path) + 1);
|
||
|
|
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0 ||
|
||
|
|
listen(fd, 8) != 0) {
|
||
|
|
close(fd);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
chmod(sock_path, 0666);
|
||
|
|
return fd;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main(void) {
|
||
|
|
if (ims_mkdir_p(ims_state_dir(), 0755) != 0) {
|
||
|
|
fprintf(stderr, "bananarchy-imsd: failed creating state dir\n");
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
char lock_path[PATH_MAX];
|
||
|
|
ims_state_file_path(lock_path, sizeof(lock_path), "imsd.lock");
|
||
|
|
if (lock_singleton(lock_path) < 0) {
|
||
|
|
fprintf(stderr, "bananarchy-imsd: already running\n");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pipe2(g_sig_pipe, O_NONBLOCK | O_CLOEXEC) != 0) {
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
struct sigaction sa;
|
||
|
|
memset(&sa, 0, sizeof(sa));
|
||
|
|
sa.sa_handler = on_term_sig;
|
||
|
|
sigaction(SIGTERM, &sa, NULL);
|
||
|
|
sigaction(SIGINT, &sa, NULL);
|
||
|
|
sa.sa_handler = on_hup_sig;
|
||
|
|
sigaction(SIGHUP, &sa, NULL);
|
||
|
|
sa.sa_handler = on_chld_sig;
|
||
|
|
sa.sa_flags = SA_RESTART;
|
||
|
|
sigaction(SIGCHLD, &sa, NULL);
|
||
|
|
signal(SIGPIPE, SIG_IGN);
|
||
|
|
|
||
|
|
int srv_fd = open_ctl_socket();
|
||
|
|
if (srv_fd < 0) {
|
||
|
|
fprintf(stderr, "bananarchy-imsd: cannot bind imsd.sock: %s\n", strerror(errno));
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
ims_state_defaults(&g_state);
|
||
|
|
reconcile_all();
|
||
|
|
|
||
|
|
while (!g_stop) {
|
||
|
|
struct pollfd pfds[4];
|
||
|
|
nfds_t nfds = 0;
|
||
|
|
|
||
|
|
int sig_idx = (int)nfds;
|
||
|
|
pfds[nfds].fd = g_sig_pipe[0];
|
||
|
|
pfds[nfds].events = POLLIN;
|
||
|
|
pfds[nfds].revents = 0;
|
||
|
|
nfds++;
|
||
|
|
|
||
|
|
int srv_idx = (int)nfds;
|
||
|
|
pfds[nfds].fd = srv_fd;
|
||
|
|
pfds[nfds].events = POLLIN;
|
||
|
|
pfds[nfds].revents = 0;
|
||
|
|
nfds++;
|
||
|
|
|
||
|
|
int qrtr_idx = -1;
|
||
|
|
if (g_imsdcm_ready && g_imsdcm.fd >= 0) {
|
||
|
|
qrtr_idx = (int)nfds;
|
||
|
|
pfds[nfds].fd = g_imsdcm.fd;
|
||
|
|
pfds[nfds].events = POLLIN;
|
||
|
|
pfds[nfds].revents = 0;
|
||
|
|
nfds++;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* Idle 0% CPU whenever no child registration sync is pending. */
|
||
|
|
int timeout_ms = (g_vowifi_pid > 0 || (g_state.volte_active && !g_state.volte_registered))
|
||
|
|
? 2000
|
||
|
|
: -1;
|
||
|
|
int prc = poll(pfds, nfds, timeout_ms);
|
||
|
|
if (prc < 0) {
|
||
|
|
if (errno == EINTR) continue;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pfds[sig_idx].revents & POLLIN) {
|
||
|
|
char drain[64];
|
||
|
|
while (read(g_sig_pipe[0], drain, sizeof(drain)) > 0) {}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (qrtr_idx >= 0 && (pfds[qrtr_idx].revents & POLLIN)) {
|
||
|
|
handle_imsdcm_qrtr_packet();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (g_chld_pending) {
|
||
|
|
g_chld_pending = 0;
|
||
|
|
int cst = 0;
|
||
|
|
pid_t w;
|
||
|
|
while ((w = waitpid(-1, &cst, WNOHANG)) > 0) {
|
||
|
|
if (w == g_vowifi_pid) {
|
||
|
|
g_vowifi_pid = -1;
|
||
|
|
g_vowifi_running_mode[0] = '\0';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
reconcile_all();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (g_reload) {
|
||
|
|
g_reload = 0;
|
||
|
|
reconcile_all();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (prc == 0) {
|
||
|
|
reconcile_all();
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (pfds[srv_idx].revents & POLLIN) {
|
||
|
|
handle_ctl_client(srv_fd);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
stop_vowifi_child();
|
||
|
|
if (g_bearer_up) {
|
||
|
|
char d_if[32], d_ip[64];
|
||
|
|
(void)run_bearer_cmd("down", g_cfg.ims_apn, d_if, sizeof(d_if), d_ip, sizeof(d_ip));
|
||
|
|
}
|
||
|
|
if (g_imsdcm_ready) {
|
||
|
|
ims_qrtr_close(&g_imsdcm);
|
||
|
|
}
|
||
|
|
if (g_qrtr_ready) {
|
||
|
|
ims_qrtr_close(&g_qrtr);
|
||
|
|
}
|
||
|
|
close(srv_fd);
|
||
|
|
char sock_path[PATH_MAX];
|
||
|
|
ims_state_file_path(sock_path, sizeof(sock_path), "imsd.sock");
|
||
|
|
unlink(sock_path);
|
||
|
|
return 0;
|
||
|
|
}
|