Move hostapd-to-driver flag mapping to be within ap_drv_ops.c
[libeap.git] / hostapd / ap_drv_ops.c
1 /*
2  * hostapd - Driver operations
3  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "hostapd.h"
19 #include "sta_info.h"
20 #include "driver_i.h"
21
22
23 static int hostapd_sta_flags_to_drv(int flags)
24 {
25         int res = 0;
26         if (flags & WLAN_STA_AUTHORIZED)
27                 res |= WPA_STA_AUTHORIZED;
28         if (flags & WLAN_STA_WMM)
29                 res |= WPA_STA_WMM;
30         if (flags & WLAN_STA_SHORT_PREAMBLE)
31                 res |= WPA_STA_SHORT_PREAMBLE;
32         if (flags & WLAN_STA_MFP)
33                 res |= WPA_STA_MFP;
34         return res;
35 }
36
37
38 static int hostapd_set_ap_wps_ie(struct hostapd_data *hapd,
39                                  const struct wpabuf *beacon,
40                                  const struct wpabuf *proberesp)
41 {
42         if (hapd->driver == NULL || hapd->driver->set_ap_wps_ie == NULL)
43                 return 0;
44         return hapd->driver->set_ap_wps_ie(hapd->conf->iface, hapd->drv_priv,
45                                            beacon, proberesp);
46 }
47
48
49 static int hostapd_send_mgmt_frame(struct hostapd_data *hapd, const void *msg,
50                            size_t len)
51 {
52         if (hapd->driver == NULL || hapd->driver->send_mlme == NULL)
53                 return 0;
54         return hapd->driver->send_mlme(hapd->drv_priv, msg, len);
55 }
56
57
58 static int hostapd_send_eapol(struct hostapd_data *hapd, const u8 *addr,
59                               const u8 *data, size_t data_len, int encrypt)
60 {
61         if (hapd->driver == NULL || hapd->driver->hapd_send_eapol == NULL)
62                 return 0;
63         return hapd->driver->hapd_send_eapol(hapd->drv_priv, addr, data,
64                                              data_len, encrypt,
65                                              hapd->own_addr);
66 }
67
68
69 static int hostapd_set_authorized(struct hostapd_data *hapd,
70                                   struct sta_info *sta, int authorized)
71 {
72         if (authorized) {
73                 return hostapd_sta_set_flags(hapd, sta->addr,
74                                              hostapd_sta_flags_to_drv(
75                                                      sta->flags),
76                                              WPA_STA_AUTHORIZED, ~0);
77         }
78
79         return hostapd_sta_set_flags(hapd, sta->addr,
80                                      hostapd_sta_flags_to_drv(sta->flags),
81                                      0, ~WPA_STA_AUTHORIZED);
82 }
83
84
85 static int hostapd_set_key(const char *ifname, struct hostapd_data *hapd,
86                            wpa_alg alg, const u8 *addr, int key_idx,
87                            int set_tx, const u8 *seq, size_t seq_len,
88                            const u8 *key, size_t key_len)
89 {
90         if (hapd->driver == NULL || hapd->driver->set_key == NULL)
91                 return 0;
92         return hapd->driver->set_key(ifname, hapd->drv_priv, alg, addr,
93                                      key_idx, set_tx, seq, seq_len, key,
94                                      key_len);
95 }
96
97
98 static int hostapd_read_sta_data(struct hostapd_data *hapd,
99                                  struct hostap_sta_driver_data *data,
100                                  const u8 *addr)
101 {
102         if (hapd->driver == NULL || hapd->driver->read_sta_data == NULL)
103                 return -1;
104         return hapd->driver->read_sta_data(hapd->drv_priv, data, addr);
105 }
106
107
108 static int hostapd_sta_clear_stats(struct hostapd_data *hapd, const u8 *addr)
109 {
110         if (hapd->driver == NULL || hapd->driver->sta_clear_stats == NULL)
111                 return 0;
112         return hapd->driver->sta_clear_stats(hapd->drv_priv, addr);
113 }
114
115
116 static int hostapd_set_sta_flags(struct hostapd_data *hapd,
117                                  struct sta_info *sta)
118 {
119         int set_flags, total_flags, flags_and, flags_or;
120         total_flags = hostapd_sta_flags_to_drv(sta->flags);
121         set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
122         if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
123             sta->flags & WLAN_STA_AUTHORIZED)
124                 set_flags |= WPA_STA_AUTHORIZED;
125         flags_or = total_flags & set_flags;
126         flags_and = total_flags | ~set_flags;
127         return hostapd_sta_set_flags(hapd, sta->addr, total_flags,
128                                      flags_or, flags_and);
129 }
130
131
132 void hostapd_set_driver_ops(struct hostapd_driver_ops *ops)
133 {
134         ops->set_ap_wps_ie = hostapd_set_ap_wps_ie;
135         ops->send_mgmt_frame = hostapd_send_mgmt_frame;
136         ops->send_eapol = hostapd_send_eapol;
137         ops->set_authorized = hostapd_set_authorized;
138         ops->set_key = hostapd_set_key;
139         ops->read_sta_data = hostapd_read_sta_data;
140         ops->sta_clear_stats = hostapd_sta_clear_stats;
141         ops->set_sta_flags = hostapd_set_sta_flags;
142 }