Skip to content
Snippets Groups Projects
Commit 42434ac1 authored by Anjan Chanda's avatar Anjan Chanda
Browse files

guard wifi functions with HAS_WIFI cflags

parent c5c5f910
Branches
No related tags found
No related merge requests found
......@@ -8,10 +8,6 @@ ifeq ($(CC),clang)
CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
endif
LIBS = -leasy -lubus -lubox -ljson-c -lblobmsg_json -luci
LIBS += -lnl-3 -lnl-genl-3 -lnl-route-3
LIBS += -lnetfilter_conntrack -lnfnetlink -lmnl
OBJS = lookup3_hash.o \
util.o \
debug.o \
......@@ -24,6 +20,19 @@ OBJS = lookup3_hash.o \
hostmngr.o \
main.o
ifneq (,$(findstring HAS_WIFI,$(CFLAGS)))
OBJS += wifi_api.o
endif
LIBS = -leasy -lubus -lubox -ljson-c -lblobmsg_json -luci
LIBS += -lnl-3 -lnl-genl-3 -lnl-route-3
LIBS += -lnetfilter_conntrack -lnfnetlink -lmnl
ifneq (,$(findstring HAS_WIFI,$(CFLAGS)))
LIBS += -lwifi-7
endif
.PHONY: all clean
all: $(EXECS)
......
......@@ -31,6 +31,7 @@
#include <easy/easy.h>
#include "wifi_api.h"
#include "util.h"
#include "useropts.h"
#include "debug.h"
......@@ -102,10 +103,13 @@ void enum_interfaces_cb(struct nl_object *obj, void *arg)
}
}
if (is_wifi_interface(iface->ifname))
if (is_wifi_interface(iface->ifname)) {
iface->mediatype = IF_MEDIA_WIFI;
else
if (is_ap_interface(iface->ifname))
iface->is_ap = true;
} else {
iface->mediatype = IF_MEDIA_ETH;
}
list_add_tail(&iface->list, argp->iflist);
*argp->n += 1;
......
......@@ -18,6 +18,7 @@
#include "util.h"
#include "timer.h"
#include "neigh.h"
#include "wifi_api.h"
#include "topology.h"
#include "lookup3_hash.h"
......@@ -619,6 +620,37 @@ bool is_neigh_1905(void *nq, uint8_t *macaddr)
return is1905;
}
int neigh_is_wifi_type(void *priv, uint8_t *macaddr)
{
struct topologyd_private *p = (struct topologyd_private *)priv;
struct local_interface *iface;
if (!p || list_empty(&p->iflist))
return -1;
list_for_each_entry(iface, &p->iflist, list) {
if (iface->is_ap) {
uint8_t stas[6*128] = {0};
int num = 128;
int ret;
int i;
ret = ap_get_assoclist(iface->ifname, stas, &num);
if (ret)
continue;
for (i = 0; i < num; i++) {
if (!memcmp(&stas[i*6], macaddr, 6)) {
return 1;
}
}
}
}
return 0;
}
int neigh_enqueue(void *nq, uint8_t *macaddr, uint16_t state, const char *ifname,
enum neigh_type type, struct ip_address *ip, uint32_t timeout,
void *cookie)
......
......@@ -154,6 +154,8 @@ int neigh_set_1905(void *q, uint8_t *macaddr);
int neigh_set_1905_slave(void *q, uint8_t *macaddr);
bool is_neigh_1905(void *q, uint8_t *macaddr);
int neigh_is_wifi_type(void *priv, uint8_t *macaddr);
struct neigh_entry *neigh_lookup(void *q, uint8_t *macaddr);
struct neigh_entry *neigh_lookup_by_ipaddr(void *priv, struct ip_address *ip);
struct neigh_ip_entry *neigh_ip_entry_lookup(void *priv, struct ip_address *ip);
......
......@@ -174,51 +174,6 @@ int topologyd_update_neigh_brport(struct topologyd_private *priv, char *brname)
return 0;
}
static int topologyd_neigh_is_wifi_type(struct topologyd_private *priv, char *ifname,
uint8_t *macaddr)
{
struct local_interface *iface;
enum if_mediatype mtype;
#if 0
struct ieee80211_info *wifi;
uint8_t stas[768] = {0};
int num = 128;
int ret;
int i;
#endif
if (if_isbridge(ifname)) {
warn("Only non-composite ifname is allowed in this function\n");
return 0;
}
if_getmediatype(ifname, &mtype);
if (mtype != IF_MEDIA_WIFI)
return 0;
iface = topologyd_ifname_to_interface(priv, ifname);
if (!iface || iface->mediainfo == NULL)
return 0;
#if 0 //TODO
wifi = (struct ieee80211_info *)iface->mediainfo;
if (wifi->role != IEEE80211_ROLE_AP)
return 0;
ret = platform_wifi_get_assoclist(ifname, stas, &num);
if (ret)
return 0;
for (i = 0; i < num; i++) {
if (!memcmp(&stas[i*6], macaddr, 6)) {
return 1;
}
}
#endif
return 0;
}
static int topologyd_handle_neigh_tbl_change(struct topologyd_private *priv, bool add,
char *ifname, uint8_t *macaddr,
......@@ -269,11 +224,14 @@ static int topologyd_handle_neigh_tbl_change(struct topologyd_private *priv, boo
}
}
#if 0
/* if the new neigh is of wifi type, mark it accordingly */
if (/* ret > 0 && */ ifname) {
if (topologyd_neigh_is_wifi_type(priv, ifname, macaddr))
if (neigh_is_wifi_type(priv, macaddr))
neigh_set_type(&priv->neigh_q, macaddr, NEIGH_TYPE_WIFI);
}
#endif
#if 1 //def NEIGH_DEBUG
if (priv->neigh_q.pending_cnt > 0) {
......@@ -644,6 +602,7 @@ int topologyd_get_known_neighbors(struct topologyd_private *priv, char *ifname)
struct ip_address ip = {0};
uint8_t hwaddr[6] = {0};
uint16_t state;
enum neigh_type type = NEIGH_TYPE_UNKNOWN;
nl_object_get((struct nl_object *)neigh);
......@@ -677,9 +636,12 @@ int topologyd_get_known_neighbors(struct topologyd_private *priv, char *ifname)
}
}
ret = neigh_enqueue(&priv->neigh_q, hwaddr, state, ifname,
NEIGH_TYPE_UNKNOWN, &ip,
NEIGH_AGEOUT_DEFAULT, NULL);
if (neigh_is_wifi_type(priv, hwaddr))
type = NEIGH_TYPE_WIFI;
ret = neigh_enqueue(&priv->neigh_q, hwaddr, state,
ifname, type, &ip,
NEIGH_AGEOUT_DEFAULT, NULL);
if (ret > 0) {
/* new neigh added */
ret = topology_get_neigh_hostname(&priv->neigh_q, hwaddr); //TODO: cond
......
......@@ -153,6 +153,7 @@ struct local_interface {
uint8_t operstate;
bool is_bridge;
bool is_brif;
bool is_ap;
uint32_t brport;
int br_ifindex;
uint32_t num_ipaddrs;
......
......@@ -35,6 +35,7 @@
#include <easy/easy.h>
#include "debug.h"
#include "wifi_api.h"
#include "util.h"
......@@ -184,34 +185,6 @@ int if_brportnum(const char *ifname)
return portnum;
}
int is_wifi_interface(const char *ifname)
{
//char parent[16] = {0};
char path[512] = {0};
char rpath[PATH_MAX] = {0};
struct stat s;
memset(&s, 0, sizeof(struct stat));
snprintf(path, 512, "/sys/class/net/%s/phy80211", ifname);
realpath(path, rpath);
if (lstat(rpath, &s) != -1) {
if (S_ISDIR(s.st_mode)) {
return 1;
}
}
#if 0 //TODO
/* WDS interface also has WiFi mediatype */
if (platform_wifi_get_4addr_parent(ifname, parent) == 0 &&
strlen(parent)) {
return 1;
}
#endif
return 0;
}
int if_getmediatype(const char *ifname, enum if_mediatype *mtype)
{
*mtype = IF_MEDIA_ETH;
......@@ -231,11 +204,4 @@ int ipaddr_equal(struct ip_address *a, struct ip_address *b)
return !memcmp(&a->addr, &b->addr, a->family == AF_INET ?
sizeof(struct in_addr) :
sizeof(struct in6_addr));
/*
if (a && b && a->family == b->family)
return !memcmp(a->addr, b->addr, sizeof(a->addr));
return 0;
*/
}
......@@ -36,8 +36,6 @@ int ipaddr_equal(struct ip_address *a, struct ip_address *b);
int if_brportnum(const char *ifname);
int if_getmediatype(const char *ifname, enum if_mediatype *mtype);
int is_wifi_interface(const char *ifname);
#ifndef BIT
#define BIT(x) (1 << (x))
#endif
......
/*
* wifi_api.c - WiFi API implementation using libwifi.so
*
* Copyright (C) 2023 IOPSYS Software Solutions AB. All rights reserved.
*
* See LICENSE file for license related information.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <net/if_arp.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <easy/easy.h>
#include <wifi.h>
#include "debug.h"
#include "util.h"
#include "timer.h"
#include "wifi_api.h"
uint8_t wifi_rssi_to_rcpi(int rssi)
{
if (!rssi)
return 255;
if (rssi < -110)
return 0;
if (rssi > 0)
return 220;
return (rssi + 110) * 2;
}
int ap_get_assoclist(const char *ifname, uint8_t *sta_macaddrs, int *num)
{
return wifi_get_assoclist(ifname, sta_macaddrs, num);
}
int interface_get_4addr_parent(const char *ifname, char *parent)
{
return wifi_get_4addr_parent(ifname, parent);
}
int is_wifi_interface(const char *ifname)
{
char parent[16] = {0};
char path[512] = {0};
char rpath[PATH_MAX] = {0};
struct stat s;
memset(&s, 0, sizeof(struct stat));
snprintf(path, 512, "/sys/class/net/%s/phy80211", ifname);
realpath(path, rpath);
if (lstat(rpath, &s) != -1) {
if (S_ISDIR(s.st_mode)) {
return 1;
}
}
/* WDS interface also has WiFi mediatype */
if (interface_get_4addr_parent(ifname, parent) == 0 &&
strlen(parent)) {
return 1;
}
return 0;
}
int is_ap_interface(const char *ifname)
{
enum wifi_mode mode;
int ret;
ret = wifi_get_mode(ifname, &mode);
if (!ret) {
if (mode == WIFI_MODE_AP || mode == WIFI_MODE_AP_VLAN)
return 1;
}
return 0;
}
/*
* wifi_api.h - WiFi APIs
*
* Copyright (C) 2023 IOPSYS Software Solutions AB. All rights reserved.
*
* See LICENSE file for license related information.
*
*/
#ifndef WIFI_API_H
#define WIFI_API_H
#ifdef HAS_WIFI
int is_wifi_interface(const char *ifname);
int is_ap_interface(const char *ifname);
int ap_get_assoclist(const char *ifname, uint8_t *sta_macaddrs, int *num);
int interface_get_4addr_parent(const char *ifname, char *parent);
#else
static inline int is_wifi_interface(const char *ifname)
{
return -1;
}
static inline int is_ap_interface(const char *ifname)
{
return -1;
}
static inline int ap_get_assoclist(const char *ifname, uint8_t *sta_macaddrs, int *num)
{
return -1;
}
static inline int interface_get_4addr_parent(const char *ifname, char *parent)
{
return -1;
}
#endif /* HAS_WIFI */
#endif /* WIFI_API_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment