xref: /freebsd/contrib/wpa/src/ap/ndisc_snoop.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * Neighbor Discovery snooping for Proxy ARP
35b9c547cSRui Paulo  * Copyright (c) 2014, Qualcomm Atheros, Inc.
45b9c547cSRui Paulo  *
55b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
65b9c547cSRui Paulo  * See README for more details.
75b9c547cSRui Paulo  */
85b9c547cSRui Paulo 
95b9c547cSRui Paulo #include "utils/includes.h"
105b9c547cSRui Paulo #include <netinet/ip6.h>
115b9c547cSRui Paulo #include <netinet/icmp6.h>
125b9c547cSRui Paulo 
135b9c547cSRui Paulo #include "utils/common.h"
145b9c547cSRui Paulo #include "l2_packet/l2_packet.h"
155b9c547cSRui Paulo #include "hostapd.h"
165b9c547cSRui Paulo #include "sta_info.h"
175b9c547cSRui Paulo #include "ap_drv_ops.h"
185b9c547cSRui Paulo #include "list.h"
195b9c547cSRui Paulo #include "x_snoop.h"
20780fb4a2SCy Schubert #include "ndisc_snoop.h"
215b9c547cSRui Paulo 
225b9c547cSRui Paulo struct ip6addr {
235b9c547cSRui Paulo 	struct in6_addr addr;
245b9c547cSRui Paulo 	struct dl_list list;
255b9c547cSRui Paulo };
265b9c547cSRui Paulo 
275b9c547cSRui Paulo struct icmpv6_ndmsg {
285b9c547cSRui Paulo 	struct ip6_hdr ipv6h;
295b9c547cSRui Paulo 	struct icmp6_hdr icmp6h;
305b9c547cSRui Paulo 	struct in6_addr target_addr;
315b9c547cSRui Paulo 	u8 opt_type;
325b9c547cSRui Paulo 	u8 len;
335b9c547cSRui Paulo 	u8 opt_lladdr[0];
345b9c547cSRui Paulo } STRUCT_PACKED;
355b9c547cSRui Paulo 
365b9c547cSRui Paulo #define ROUTER_ADVERTISEMENT	134
375b9c547cSRui Paulo #define NEIGHBOR_SOLICITATION	135
385b9c547cSRui Paulo #define NEIGHBOR_ADVERTISEMENT	136
395b9c547cSRui Paulo #define SOURCE_LL_ADDR		1
405b9c547cSRui Paulo 
sta_ip6addr_add(struct sta_info * sta,struct in6_addr * addr)415b9c547cSRui Paulo static int sta_ip6addr_add(struct sta_info *sta, struct in6_addr *addr)
425b9c547cSRui Paulo {
435b9c547cSRui Paulo 	struct ip6addr *ip6addr;
445b9c547cSRui Paulo 
455b9c547cSRui Paulo 	ip6addr = os_zalloc(sizeof(*ip6addr));
465b9c547cSRui Paulo 	if (!ip6addr)
475b9c547cSRui Paulo 		return -1;
485b9c547cSRui Paulo 
495b9c547cSRui Paulo 	os_memcpy(&ip6addr->addr, addr, sizeof(*addr));
505b9c547cSRui Paulo 
515b9c547cSRui Paulo 	dl_list_add_tail(&sta->ip6addr, &ip6addr->list);
525b9c547cSRui Paulo 
535b9c547cSRui Paulo 	return 0;
545b9c547cSRui Paulo }
555b9c547cSRui Paulo 
565b9c547cSRui Paulo 
sta_ip6addr_del(struct hostapd_data * hapd,struct sta_info * sta)575b9c547cSRui Paulo void sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
585b9c547cSRui Paulo {
595b9c547cSRui Paulo 	struct ip6addr *ip6addr, *prev;
605b9c547cSRui Paulo 
615b9c547cSRui Paulo 	dl_list_for_each_safe(ip6addr, prev, &sta->ip6addr, struct ip6addr,
625b9c547cSRui Paulo 			      list) {
635b9c547cSRui Paulo 		hostapd_drv_br_delete_ip_neigh(hapd, 6, (u8 *) &ip6addr->addr);
64*a90b9d01SCy Schubert 		dl_list_del(&ip6addr->list);
655b9c547cSRui Paulo 		os_free(ip6addr);
665b9c547cSRui Paulo 	}
675b9c547cSRui Paulo }
685b9c547cSRui Paulo 
695b9c547cSRui Paulo 
sta_has_ip6addr(struct sta_info * sta,struct in6_addr * addr)705b9c547cSRui Paulo static int sta_has_ip6addr(struct sta_info *sta, struct in6_addr *addr)
715b9c547cSRui Paulo {
725b9c547cSRui Paulo 	struct ip6addr *ip6addr;
735b9c547cSRui Paulo 
745b9c547cSRui Paulo 	dl_list_for_each(ip6addr, &sta->ip6addr, struct ip6addr, list) {
755b9c547cSRui Paulo 		if (ip6addr->addr.s6_addr32[0] == addr->s6_addr32[0] &&
765b9c547cSRui Paulo 		    ip6addr->addr.s6_addr32[1] == addr->s6_addr32[1] &&
775b9c547cSRui Paulo 		    ip6addr->addr.s6_addr32[2] == addr->s6_addr32[2] &&
785b9c547cSRui Paulo 		    ip6addr->addr.s6_addr32[3] == addr->s6_addr32[3])
795b9c547cSRui Paulo 			return 1;
805b9c547cSRui Paulo 	}
815b9c547cSRui Paulo 
825b9c547cSRui Paulo 	return 0;
835b9c547cSRui Paulo }
845b9c547cSRui Paulo 
855b9c547cSRui Paulo 
ucast_to_stas(struct hostapd_data * hapd,const u8 * buf,size_t len)86325151a3SRui Paulo static void ucast_to_stas(struct hostapd_data *hapd, const u8 *buf, size_t len)
87325151a3SRui Paulo {
88325151a3SRui Paulo 	struct sta_info *sta;
89325151a3SRui Paulo 
90325151a3SRui Paulo 	for (sta = hapd->sta_list; sta; sta = sta->next) {
91325151a3SRui Paulo 		if (!(sta->flags & WLAN_STA_AUTHORIZED))
92325151a3SRui Paulo 			continue;
93325151a3SRui Paulo 		x_snoop_mcast_to_ucast_convert_send(hapd, sta, (u8 *) buf, len);
94325151a3SRui Paulo 	}
95325151a3SRui Paulo }
96325151a3SRui Paulo 
97325151a3SRui Paulo 
handle_ndisc(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)985b9c547cSRui Paulo static void handle_ndisc(void *ctx, const u8 *src_addr, const u8 *buf,
995b9c547cSRui Paulo 			 size_t len)
1005b9c547cSRui Paulo {
1015b9c547cSRui Paulo 	struct hostapd_data *hapd = ctx;
1025b9c547cSRui Paulo 	struct icmpv6_ndmsg *msg;
103325151a3SRui Paulo 	struct in6_addr saddr;
1045b9c547cSRui Paulo 	struct sta_info *sta;
1055b9c547cSRui Paulo 	int res;
1065b9c547cSRui Paulo 	char addrtxt[INET6_ADDRSTRLEN + 1];
1075b9c547cSRui Paulo 
1085b9c547cSRui Paulo 	if (len < ETH_HLEN + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
1095b9c547cSRui Paulo 		return;
1105b9c547cSRui Paulo 	msg = (struct icmpv6_ndmsg *) &buf[ETH_HLEN];
1115b9c547cSRui Paulo 	switch (msg->icmp6h.icmp6_type) {
1125b9c547cSRui Paulo 	case NEIGHBOR_SOLICITATION:
1135b9c547cSRui Paulo 		if (len < ETH_HLEN + sizeof(*msg))
1145b9c547cSRui Paulo 			return;
1155b9c547cSRui Paulo 		if (msg->opt_type != SOURCE_LL_ADDR)
1165b9c547cSRui Paulo 			return;
1175b9c547cSRui Paulo 
118325151a3SRui Paulo 		/*
119325151a3SRui Paulo 		 * IPv6 header may not be 32-bit aligned in the buffer, so use
120325151a3SRui Paulo 		 * a local copy to avoid unaligned reads.
121325151a3SRui Paulo 		 */
122325151a3SRui Paulo 		os_memcpy(&saddr, &msg->ipv6h.ip6_src, sizeof(saddr));
123325151a3SRui Paulo 		if (!(saddr.s6_addr32[0] == 0 && saddr.s6_addr32[1] == 0 &&
124325151a3SRui Paulo 		      saddr.s6_addr32[2] == 0 && saddr.s6_addr32[3] == 0)) {
1255b9c547cSRui Paulo 			if (len < ETH_HLEN + sizeof(*msg) + ETH_ALEN)
1265b9c547cSRui Paulo 				return;
1275b9c547cSRui Paulo 			sta = ap_get_sta(hapd, msg->opt_lladdr);
1285b9c547cSRui Paulo 			if (!sta)
1295b9c547cSRui Paulo 				return;
1305b9c547cSRui Paulo 
131325151a3SRui Paulo 			if (sta_has_ip6addr(sta, &saddr))
1325b9c547cSRui Paulo 				return;
1335b9c547cSRui Paulo 
134325151a3SRui Paulo 			if (inet_ntop(AF_INET6, &saddr, addrtxt,
135325151a3SRui Paulo 				      sizeof(addrtxt)) == NULL)
1365b9c547cSRui Paulo 				addrtxt[0] = '\0';
1375b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG, "ndisc_snoop: Learned new IPv6 address %s for "
1385b9c547cSRui Paulo 				   MACSTR, addrtxt, MAC2STR(sta->addr));
139325151a3SRui Paulo 			hostapd_drv_br_delete_ip_neigh(hapd, 6, (u8 *) &saddr);
140325151a3SRui Paulo 			res = hostapd_drv_br_add_ip_neigh(hapd, 6,
141325151a3SRui Paulo 							  (u8 *) &saddr,
1425b9c547cSRui Paulo 							  128, sta->addr);
1435b9c547cSRui Paulo 			if (res) {
1445b9c547cSRui Paulo 				wpa_printf(MSG_ERROR,
1455b9c547cSRui Paulo 					   "ndisc_snoop: Adding ip neigh failed: %d",
1465b9c547cSRui Paulo 					   res);
1475b9c547cSRui Paulo 				return;
1485b9c547cSRui Paulo 			}
1495b9c547cSRui Paulo 
150325151a3SRui Paulo 			if (sta_ip6addr_add(sta, &saddr))
1515b9c547cSRui Paulo 				return;
1525b9c547cSRui Paulo 		}
1535b9c547cSRui Paulo 		break;
1544b72b91aSCy Schubert #ifdef CONFIG_HS20
1555b9c547cSRui Paulo 	case ROUTER_ADVERTISEMENT:
156325151a3SRui Paulo 		if (hapd->conf->disable_dgaf)
157325151a3SRui Paulo 			ucast_to_stas(hapd, buf, len);
158325151a3SRui Paulo 		break;
1594b72b91aSCy Schubert #endif /* CONFIG_HS20 */
1605b9c547cSRui Paulo 	case NEIGHBOR_ADVERTISEMENT:
161325151a3SRui Paulo 		if (hapd->conf->na_mcast_to_ucast)
162325151a3SRui Paulo 			ucast_to_stas(hapd, buf, len);
1635b9c547cSRui Paulo 		break;
1645b9c547cSRui Paulo 	default:
1655b9c547cSRui Paulo 		break;
1665b9c547cSRui Paulo 	}
1675b9c547cSRui Paulo }
1685b9c547cSRui Paulo 
1695b9c547cSRui Paulo 
ndisc_snoop_init(struct hostapd_data * hapd)1705b9c547cSRui Paulo int ndisc_snoop_init(struct hostapd_data *hapd)
1715b9c547cSRui Paulo {
1725b9c547cSRui Paulo 	hapd->sock_ndisc = x_snoop_get_l2_packet(hapd, handle_ndisc,
1735b9c547cSRui Paulo 						 L2_PACKET_FILTER_NDISC);
1745b9c547cSRui Paulo 	if (hapd->sock_ndisc == NULL) {
1755b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
1765b9c547cSRui Paulo 			   "ndisc_snoop: Failed to initialize L2 packet processing for NDISC packets: %s",
1775b9c547cSRui Paulo 			   strerror(errno));
1785b9c547cSRui Paulo 		return -1;
1795b9c547cSRui Paulo 	}
1805b9c547cSRui Paulo 
1815b9c547cSRui Paulo 	return 0;
1825b9c547cSRui Paulo }
1835b9c547cSRui Paulo 
1845b9c547cSRui Paulo 
ndisc_snoop_deinit(struct hostapd_data * hapd)1855b9c547cSRui Paulo void ndisc_snoop_deinit(struct hostapd_data *hapd)
1865b9c547cSRui Paulo {
1875b9c547cSRui Paulo 	l2_packet_deinit(hapd->sock_ndisc);
18885732ac8SCy Schubert 	hapd->sock_ndisc = NULL;
1895b9c547cSRui Paulo }
190