2014-11-01 07:33:41 +01:00
|
|
|
/*
|
|
|
|
* Neighbor Discovery snooping for Proxy ARP
|
|
|
|
* Copyright (c) 2014, Qualcomm Atheros, Inc.
|
|
|
|
*
|
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils/includes.h"
|
2014-11-25 15:58:21 +01:00
|
|
|
#include <netinet/ip6.h>
|
|
|
|
#include <netinet/icmp6.h>
|
2014-11-01 07:33:41 +01:00
|
|
|
|
|
|
|
#include "utils/common.h"
|
|
|
|
#include "l2_packet/l2_packet.h"
|
|
|
|
#include "hostapd.h"
|
|
|
|
#include "sta_info.h"
|
|
|
|
#include "ap_drv_ops.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "x_snoop.h"
|
|
|
|
|
|
|
|
struct ip6addr {
|
|
|
|
struct in6_addr addr;
|
|
|
|
struct dl_list list;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct icmpv6_ndmsg {
|
2014-11-25 15:58:21 +01:00
|
|
|
struct ip6_hdr ipv6h;
|
|
|
|
struct icmp6_hdr icmp6h;
|
2014-11-01 07:33:41 +01:00
|
|
|
struct in6_addr target_addr;
|
|
|
|
u8 opt_type;
|
|
|
|
u8 len;
|
|
|
|
u8 opt_lladdr[0];
|
2014-11-26 00:05:24 +01:00
|
|
|
} STRUCT_PACKED;
|
2014-11-01 07:33:41 +01:00
|
|
|
|
2014-11-03 23:10:24 +01:00
|
|
|
#define ROUTER_ADVERTISEMENT 134
|
2014-11-01 07:33:41 +01:00
|
|
|
#define NEIGHBOR_SOLICITATION 135
|
|
|
|
#define NEIGHBOR_ADVERTISEMENT 136
|
|
|
|
#define SOURCE_LL_ADDR 1
|
|
|
|
|
|
|
|
static int sta_ip6addr_add(struct sta_info *sta, struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
struct ip6addr *ip6addr;
|
|
|
|
|
|
|
|
ip6addr = os_zalloc(sizeof(*ip6addr));
|
|
|
|
if (!ip6addr)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
os_memcpy(&ip6addr->addr, addr, sizeof(*addr));
|
|
|
|
|
|
|
|
dl_list_add_tail(&sta->ip6addr, &ip6addr->list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
|
|
|
|
{
|
|
|
|
struct ip6addr *ip6addr, *prev;
|
|
|
|
|
|
|
|
dl_list_for_each_safe(ip6addr, prev, &sta->ip6addr, struct ip6addr,
|
|
|
|
list) {
|
|
|
|
hostapd_drv_br_delete_ip_neigh(hapd, 6, (u8 *) &ip6addr->addr);
|
|
|
|
os_free(ip6addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int sta_has_ip6addr(struct sta_info *sta, struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
struct ip6addr *ip6addr;
|
|
|
|
|
|
|
|
dl_list_for_each(ip6addr, &sta->ip6addr, struct ip6addr, list) {
|
|
|
|
if (ip6addr->addr.s6_addr32[0] == addr->s6_addr32[0] &&
|
|
|
|
ip6addr->addr.s6_addr32[1] == addr->s6_addr32[1] &&
|
|
|
|
ip6addr->addr.s6_addr32[2] == addr->s6_addr32[2] &&
|
|
|
|
ip6addr->addr.s6_addr32[3] == addr->s6_addr32[3])
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-27 11:30:09 +02:00
|
|
|
static void ucast_to_stas(struct hostapd_data *hapd, const u8 *buf, size_t len)
|
|
|
|
{
|
|
|
|
struct sta_info *sta;
|
|
|
|
|
|
|
|
for (sta = hapd->sta_list; sta; sta = sta->next) {
|
|
|
|
if (!(sta->flags & WLAN_STA_AUTHORIZED))
|
|
|
|
continue;
|
|
|
|
x_snoop_mcast_to_ucast_convert_send(hapd, sta, (u8 *) buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-01 07:33:41 +01:00
|
|
|
static void handle_ndisc(void *ctx, const u8 *src_addr, const u8 *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
struct hostapd_data *hapd = ctx;
|
|
|
|
struct icmpv6_ndmsg *msg;
|
2015-07-07 14:51:05 +02:00
|
|
|
struct in6_addr saddr;
|
2014-11-01 07:33:41 +01:00
|
|
|
struct sta_info *sta;
|
|
|
|
int res;
|
2014-11-26 00:06:56 +01:00
|
|
|
char addrtxt[INET6_ADDRSTRLEN + 1];
|
2014-11-01 07:33:41 +01:00
|
|
|
|
2014-11-28 18:44:58 +01:00
|
|
|
if (len < ETH_HLEN + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
|
2014-11-01 07:33:41 +01:00
|
|
|
return;
|
|
|
|
msg = (struct icmpv6_ndmsg *) &buf[ETH_HLEN];
|
|
|
|
switch (msg->icmp6h.icmp6_type) {
|
|
|
|
case NEIGHBOR_SOLICITATION:
|
2014-11-28 18:44:58 +01:00
|
|
|
if (len < ETH_HLEN + sizeof(*msg))
|
|
|
|
return;
|
2014-11-01 07:33:41 +01:00
|
|
|
if (msg->opt_type != SOURCE_LL_ADDR)
|
|
|
|
return;
|
|
|
|
|
2015-07-07 14:51:05 +02:00
|
|
|
/*
|
|
|
|
* IPv6 header may not be 32-bit aligned in the buffer, so use
|
|
|
|
* a local copy to avoid unaligned reads.
|
|
|
|
*/
|
|
|
|
os_memcpy(&saddr, &msg->ipv6h.ip6_src, sizeof(saddr));
|
|
|
|
if (!(saddr.s6_addr32[0] == 0 && saddr.s6_addr32[1] == 0 &&
|
|
|
|
saddr.s6_addr32[2] == 0 && saddr.s6_addr32[3] == 0)) {
|
2014-11-01 07:33:41 +01:00
|
|
|
if (len < ETH_HLEN + sizeof(*msg) + ETH_ALEN)
|
|
|
|
return;
|
|
|
|
sta = ap_get_sta(hapd, msg->opt_lladdr);
|
|
|
|
if (!sta)
|
|
|
|
return;
|
|
|
|
|
2015-07-07 14:51:05 +02:00
|
|
|
if (sta_has_ip6addr(sta, &saddr))
|
2014-11-01 07:33:41 +01:00
|
|
|
return;
|
|
|
|
|
2015-07-07 14:51:05 +02:00
|
|
|
if (inet_ntop(AF_INET6, &saddr, addrtxt,
|
|
|
|
sizeof(addrtxt)) == NULL)
|
2014-11-26 00:06:56 +01:00
|
|
|
addrtxt[0] = '\0';
|
|
|
|
wpa_printf(MSG_DEBUG, "ndisc_snoop: Learned new IPv6 address %s for "
|
|
|
|
MACSTR, addrtxt, MAC2STR(sta->addr));
|
2015-07-07 14:51:05 +02:00
|
|
|
hostapd_drv_br_delete_ip_neigh(hapd, 6, (u8 *) &saddr);
|
|
|
|
res = hostapd_drv_br_add_ip_neigh(hapd, 6,
|
|
|
|
(u8 *) &saddr,
|
2014-11-01 07:33:41 +01:00
|
|
|
128, sta->addr);
|
|
|
|
if (res) {
|
|
|
|
wpa_printf(MSG_ERROR,
|
|
|
|
"ndisc_snoop: Adding ip neigh failed: %d",
|
|
|
|
res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:51:05 +02:00
|
|
|
if (sta_ip6addr_add(sta, &saddr))
|
2014-11-01 07:33:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2014-11-03 23:10:24 +01:00
|
|
|
case ROUTER_ADVERTISEMENT:
|
2015-04-27 11:30:09 +02:00
|
|
|
if (hapd->conf->disable_dgaf)
|
|
|
|
ucast_to_stas(hapd, buf, len);
|
|
|
|
break;
|
2014-11-01 07:33:41 +01:00
|
|
|
case NEIGHBOR_ADVERTISEMENT:
|
2015-04-27 11:30:09 +02:00
|
|
|
if (hapd->conf->na_mcast_to_ucast)
|
|
|
|
ucast_to_stas(hapd, buf, len);
|
2014-11-01 07:33:41 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ndisc_snoop_init(struct hostapd_data *hapd)
|
|
|
|
{
|
|
|
|
hapd->sock_ndisc = x_snoop_get_l2_packet(hapd, handle_ndisc,
|
|
|
|
L2_PACKET_FILTER_NDISC);
|
|
|
|
if (hapd->sock_ndisc == NULL) {
|
|
|
|
wpa_printf(MSG_DEBUG,
|
|
|
|
"ndisc_snoop: Failed to initialize L2 packet processing for NDISC packets: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ndisc_snoop_deinit(struct hostapd_data *hapd)
|
|
|
|
{
|
|
|
|
l2_packet_deinit(hapd->sock_ndisc);
|
|
|
|
}
|