blob: a7060246755ffe82619a19bed564a18aa89907ea [file] [log] [blame]
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001/*
2 * DHCP 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/ip.h>
11#include <netinet/udp.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 "x_snoop.h"
19#include "dhcp_snoop.h"
20
21struct bootp_pkt {
22 struct iphdr iph;
23 struct udphdr udph;
24 u8 op;
25 u8 htype;
26 u8 hlen;
27 u8 hops;
28 be32 xid;
29 be16 secs;
30 be16 flags;
31 be32 client_ip;
32 be32 your_ip;
33 be32 server_ip;
34 be32 relay_ip;
35 u8 hw_addr[16];
36 u8 serv_name[64];
37 u8 boot_file[128];
38 u8 exten[312];
39} STRUCT_PACKED;
40
41#define DHCPACK 5
42static const u8 ic_bootp_cookie[] = { 99, 130, 83, 99 };
43
44
45static void handle_dhcp(void *ctx, const u8 *src_addr, const u8 *buf,
46 size_t len)
47{
48 struct hostapd_data *hapd = ctx;
49 const struct bootp_pkt *b;
50 struct sta_info *sta;
51 int exten_len;
52 const u8 *end, *pos;
53 int res, msgtype = 0, prefixlen = 32;
54 u32 subnet_mask = 0;
55 u16 tot_len;
56
57 exten_len = len - ETH_HLEN - (sizeof(*b) - sizeof(b->exten));
58 if (exten_len < 4)
59 return;
60
61 b = (const struct bootp_pkt *) &buf[ETH_HLEN];
62 tot_len = ntohs(b->iph.tot_len);
63 if (tot_len > (unsigned int) (len - ETH_HLEN))
64 return;
65
66 if (os_memcmp(b->exten, ic_bootp_cookie, ARRAY_SIZE(ic_bootp_cookie)))
67 return;
68
69 /* Parse DHCP options */
70 end = (const u8 *) b + tot_len;
71 pos = &b->exten[4];
72 while (pos < end && *pos != 0xff) {
73 const u8 *opt = pos++;
74
75 if (*opt == 0) /* padding */
76 continue;
77
78 pos += *pos + 1;
79 if (pos >= end)
80 break;
81
82 switch (*opt) {
83 case 1: /* subnet mask */
84 if (opt[1] == 4)
85 subnet_mask = WPA_GET_BE32(&opt[2]);
86 if (subnet_mask == 0)
87 return;
88 while (!(subnet_mask & 0x1)) {
89 subnet_mask >>= 1;
90 prefixlen--;
91 }
92 break;
93 case 53: /* message type */
94 if (opt[1])
95 msgtype = opt[2];
96 break;
97 default:
98 break;
99 }
100 }
101
102 if (msgtype == DHCPACK) {
103 if (b->your_ip == 0)
104 return;
105
106 /* DHCPACK for DHCPREQUEST */
107 sta = ap_get_sta(hapd, b->hw_addr);
108 if (!sta)
109 return;
110
111 wpa_printf(MSG_DEBUG, "dhcp_snoop: Found DHCPACK for " MACSTR
112 " @ IPv4 address %X/%d",
113 MAC2STR(sta->addr), ntohl(b->your_ip), prefixlen);
114
115 if (sta->ipaddr == b->your_ip)
116 return;
117
118 if (sta->ipaddr != 0) {
119 wpa_printf(MSG_DEBUG,
120 "dhcp_snoop: Removing IPv4 address %X from the ip neigh table",
121 sta->ipaddr);
122 hostapd_drv_br_delete_ip_neigh(hapd, 4,
123 (u8 *) &sta->ipaddr);
124 }
125
126 res = hostapd_drv_br_add_ip_neigh(hapd, 4, (u8 *) &b->your_ip,
127 prefixlen, sta->addr);
128 if (res) {
129 wpa_printf(MSG_DEBUG,
130 "dhcp_snoop: Adding ip neigh table failed: %d",
131 res);
132 return;
133 }
134 sta->ipaddr = b->your_ip;
135 }
136
137 if (hapd->conf->disable_dgaf && is_broadcast_ether_addr(buf)) {
138 for (sta = hapd->sta_list; sta; sta = sta->next) {
139 if (!(sta->flags & WLAN_STA_AUTHORIZED))
140 continue;
141 x_snoop_mcast_to_ucast_convert_send(hapd, sta,
142 (u8 *) buf, len);
143 }
144 }
145}
146
147
148int dhcp_snoop_init(struct hostapd_data *hapd)
149{
150 hapd->sock_dhcp = x_snoop_get_l2_packet(hapd, handle_dhcp,
151 L2_PACKET_FILTER_DHCP);
152 if (hapd->sock_dhcp == NULL) {
153 wpa_printf(MSG_DEBUG,
154 "dhcp_snoop: Failed to initialize L2 packet processing for DHCP packet: %s",
155 strerror(errno));
156 return -1;
157 }
158
159 return 0;
160}
161
162
163void dhcp_snoop_deinit(struct hostapd_data *hapd)
164{
165 l2_packet_deinit(hapd->sock_dhcp);
166}