blob: 4a87721e2ecfce3f65e070c3fd3e22077f3f2cab [file] [log] [blame]
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001/*
2 * Neighbor Discovery snooping for Proxy ARP
3 * Copyright (c) 2014, Qualcomm Atheros, Inc.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10#include <netinet/ip6.h>
11#include <netinet/icmp6.h>
12
13#include "utils/common.h"
14#include "l2_packet/l2_packet.h"
15#include "hostapd.h"
16#include "sta_info.h"
17#include "ap_drv_ops.h"
18#include "list.h"
19#include "x_snoop.h"
20
21struct ip6addr {
22 struct in6_addr addr;
23 struct dl_list list;
24};
25
26struct icmpv6_ndmsg {
27 struct ip6_hdr ipv6h;
28 struct icmp6_hdr icmp6h;
29 struct in6_addr target_addr;
30 u8 opt_type;
31 u8 len;
32 u8 opt_lladdr[0];
33} STRUCT_PACKED;
34
35#define ROUTER_ADVERTISEMENT 134
36#define NEIGHBOR_SOLICITATION 135
37#define NEIGHBOR_ADVERTISEMENT 136
38#define SOURCE_LL_ADDR 1
39
40static int sta_ip6addr_add(struct sta_info *sta, struct in6_addr *addr)
41{
42 struct ip6addr *ip6addr;
43
44 ip6addr = os_zalloc(sizeof(*ip6addr));
45 if (!ip6addr)
46 return -1;
47
48 os_memcpy(&ip6addr->addr, addr, sizeof(*addr));
49
50 dl_list_add_tail(&sta->ip6addr, &ip6addr->list);
51
52 return 0;
53}
54
55
56void sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
57{
58 struct ip6addr *ip6addr, *prev;
59
60 dl_list_for_each_safe(ip6addr, prev, &sta->ip6addr, struct ip6addr,
61 list) {
62 hostapd_drv_br_delete_ip_neigh(hapd, 6, (u8 *) &ip6addr->addr);
63 os_free(ip6addr);
64 }
65}
66
67
68static int sta_has_ip6addr(struct sta_info *sta, struct in6_addr *addr)
69{
70 struct ip6addr *ip6addr;
71
72 dl_list_for_each(ip6addr, &sta->ip6addr, struct ip6addr, list) {
73 if (ip6addr->addr.s6_addr32[0] == addr->s6_addr32[0] &&
74 ip6addr->addr.s6_addr32[1] == addr->s6_addr32[1] &&
75 ip6addr->addr.s6_addr32[2] == addr->s6_addr32[2] &&
76 ip6addr->addr.s6_addr32[3] == addr->s6_addr32[3])
77 return 1;
78 }
79
80 return 0;
81}
82
83
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070084static void ucast_to_stas(struct hostapd_data *hapd, const u8 *buf, size_t len)
85{
86 struct sta_info *sta;
87
88 for (sta = hapd->sta_list; sta; sta = sta->next) {
89 if (!(sta->flags & WLAN_STA_AUTHORIZED))
90 continue;
91 x_snoop_mcast_to_ucast_convert_send(hapd, sta, (u8 *) buf, len);
92 }
93}
94
95
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080096static void handle_ndisc(void *ctx, const u8 *src_addr, const u8 *buf,
97 size_t len)
98{
99 struct hostapd_data *hapd = ctx;
100 struct icmpv6_ndmsg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101 struct in6_addr saddr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800102 struct sta_info *sta;
103 int res;
104 char addrtxt[INET6_ADDRSTRLEN + 1];
105
106 if (len < ETH_HLEN + sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr))
107 return;
108 msg = (struct icmpv6_ndmsg *) &buf[ETH_HLEN];
109 switch (msg->icmp6h.icmp6_type) {
110 case NEIGHBOR_SOLICITATION:
111 if (len < ETH_HLEN + sizeof(*msg))
112 return;
113 if (msg->opt_type != SOURCE_LL_ADDR)
114 return;
115
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116 /*
117 * IPv6 header may not be 32-bit aligned in the buffer, so use
118 * a local copy to avoid unaligned reads.
119 */
120 os_memcpy(&saddr, &msg->ipv6h.ip6_src, sizeof(saddr));
121 if (!(saddr.s6_addr32[0] == 0 && saddr.s6_addr32[1] == 0 &&
122 saddr.s6_addr32[2] == 0 && saddr.s6_addr32[3] == 0)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800123 if (len < ETH_HLEN + sizeof(*msg) + ETH_ALEN)
124 return;
125 sta = ap_get_sta(hapd, msg->opt_lladdr);
126 if (!sta)
127 return;
128
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800129 if (sta_has_ip6addr(sta, &saddr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800130 return;
131
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800132 if (inet_ntop(AF_INET6, &saddr, addrtxt,
133 sizeof(addrtxt)) == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800134 addrtxt[0] = '\0';
135 wpa_printf(MSG_DEBUG, "ndisc_snoop: Learned new IPv6 address %s for "
136 MACSTR, addrtxt, MAC2STR(sta->addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800137 hostapd_drv_br_delete_ip_neigh(hapd, 6, (u8 *) &saddr);
138 res = hostapd_drv_br_add_ip_neigh(hapd, 6,
139 (u8 *) &saddr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800140 128, sta->addr);
141 if (res) {
142 wpa_printf(MSG_ERROR,
143 "ndisc_snoop: Adding ip neigh failed: %d",
144 res);
145 return;
146 }
147
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800148 if (sta_ip6addr_add(sta, &saddr))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800149 return;
150 }
151 break;
152 case ROUTER_ADVERTISEMENT:
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700153 if (hapd->conf->disable_dgaf)
154 ucast_to_stas(hapd, buf, len);
155 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800156 case NEIGHBOR_ADVERTISEMENT:
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700157 if (hapd->conf->na_mcast_to_ucast)
158 ucast_to_stas(hapd, buf, len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800159 break;
160 default:
161 break;
162 }
163}
164
165
166int ndisc_snoop_init(struct hostapd_data *hapd)
167{
168 hapd->sock_ndisc = x_snoop_get_l2_packet(hapd, handle_ndisc,
169 L2_PACKET_FILTER_NDISC);
170 if (hapd->sock_ndisc == NULL) {
171 wpa_printf(MSG_DEBUG,
172 "ndisc_snoop: Failed to initialize L2 packet processing for NDISC packets: %s",
173 strerror(errno));
174 return -1;
175 }
176
177 return 0;
178}
179
180
181void ndisc_snoop_deinit(struct hostapd_data *hapd)
182{
183 l2_packet_deinit(hapd->sock_ndisc);
184}