| 1 | #include <stdint.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <sys/socket.h> |
| 4 | #include <netlink/genl/genl.h> |
| 5 | #include <netlink/genl/family.h> |
| 6 | #include <netlink/genl/ctrl.h> |
| 7 | #include <linux/rtnetlink.h> |
| 8 | #include <netpacket/packet.h> |
| 9 | #include <linux/filter.h> |
| 10 | #include <linux/errqueue.h> |
| 11 | |
| 12 | #include <linux/pkt_sched.h> |
| 13 | #include <netlink/object-api.h> |
| 14 | #include <netlink/netlink.h> |
| 15 | #include <netlink/socket.h> |
| 16 | #include <netlink-types.h> |
| 17 | |
| 18 | #include "nl80211_copy.h" |
| 19 | #include "sync.h" |
| 20 | |
| 21 | #define LOG_TAG "WifiHAL" |
| 22 | |
| 23 | #include <utils/Log.h> |
| 24 | |
| 25 | #include "wifi_hal.h" |
| 26 | #include "common.h" |
| 27 | #include "cpp_bindings.h" |
| 28 | |
| 29 | #ifndef SLSI_WLAN_UNIT_TEST |
| 30 | using namespace android; |
| 31 | #endif |
| 32 | |
| 33 | typedef enum { |
| 34 | MKEEP_ALIVE_ATTRIBUTE_ID, |
| 35 | MKEEP_ALIVE_ATTRIBUTE_IP_PKT, |
| 36 | MKEEP_ALIVE_ATTRIBUTE_IP_PKT_LEN, |
| 37 | MKEEP_ALIVE_ATTRIBUTE_SRC_MAC_ADDR, |
| 38 | MKEEP_ALIVE_ATTRIBUTE_DST_MAC_ADDR, |
| 39 | MKEEP_ALIVE_ATTRIBUTE_PERIOD_MSEC |
| 40 | } WIFI_MKEEP_ALIVE_ATTRIBUTE; |
| 41 | |
| 42 | typedef enum { |
| 43 | START_MKEEP_ALIVE, |
| 44 | STOP_MKEEP_ALIVE, |
| 45 | } GetCmdType; |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | class MKeepAliveCommand : public WifiCommand |
| 49 | { |
| 50 | u8 mIndex; |
| 51 | u8 *mIpPkt; |
| 52 | u16 mIpPktLen; |
| 53 | u8 *mSrcMacAddr; |
| 54 | u8 *mDstMacAddr; |
| 55 | u32 mPeriodMsec; |
| 56 | GetCmdType mType; |
| 57 | |
| 58 | public: |
| 59 | |
| 60 | // constructor for start sending |
| 61 | MKeepAliveCommand(wifi_interface_handle iface, u8 index, u8 *ip_packet, u16 ip_packet_len, |
| 62 | u8 *src_mac_addr, u8 *dst_mac_addr, u32 period_msec, GetCmdType cmdType) |
| 63 | : WifiCommand(iface, 0), mIndex(index), mIpPkt(ip_packet), |
| 64 | mIpPktLen(ip_packet_len), mSrcMacAddr(src_mac_addr), mDstMacAddr(dst_mac_addr), |
| 65 | mPeriodMsec(period_msec), mType(cmdType) |
| 66 | { } |
| 67 | |
| 68 | // constructor for stop sending |
| 69 | MKeepAliveCommand(wifi_interface_handle iface, u8 index, GetCmdType cmdType) |
| 70 | : WifiCommand(iface, 0), mIndex(index), mType(cmdType) |
| 71 | { } |
| 72 | |
| 73 | int createRequest(WifiRequest &request) { |
| 74 | int result; |
| 75 | |
| 76 | switch (mType) { |
| 77 | case START_MKEEP_ALIVE: |
| 78 | { |
| 79 | result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_START_KEEP_ALIVE_OFFLOAD); |
| 80 | if (result != WIFI_SUCCESS) { |
| 81 | ALOGE("Failed to create start keep alive request; result = %d", result); |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA); |
| 86 | |
| 87 | result = request.put_u8(MKEEP_ALIVE_ATTRIBUTE_ID, mIndex); |
| 88 | if (result < 0) { |
| 89 | ALOGE("Failed to put id request; result = %d", result); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | result = request.put_u16(MKEEP_ALIVE_ATTRIBUTE_IP_PKT_LEN, mIpPktLen); |
| 94 | if (result < 0) { |
| 95 | ALOGE("Failed to put ip pkt len request; result = %d", result); |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | result = request.put(MKEEP_ALIVE_ATTRIBUTE_IP_PKT, (u8*)mIpPkt, mIpPktLen); |
| 100 | if (result < 0) { |
| 101 | ALOGE("Failed to put ip pkt request; result = %d", result); |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | result = request.put_addr(MKEEP_ALIVE_ATTRIBUTE_SRC_MAC_ADDR, mSrcMacAddr); |
| 106 | if (result < 0) { |
| 107 | ALOGE("Failed to put src mac address request; result = %d", result); |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | result = request.put_addr(MKEEP_ALIVE_ATTRIBUTE_DST_MAC_ADDR, mDstMacAddr); |
| 112 | if (result < 0) { |
| 113 | ALOGE("Failed to put dst mac address request; result = %d", result); |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | result = request.put_u32(MKEEP_ALIVE_ATTRIBUTE_PERIOD_MSEC, mPeriodMsec); |
| 118 | if (result < 0) { |
| 119 | ALOGE("Failed to put period request; result = %d", result); |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | request.attr_end(data); |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | case STOP_MKEEP_ALIVE: |
| 128 | { |
| 129 | result = request.create(GOOGLE_OUI, SLSI_NL80211_VENDOR_SUBCMD_STOP_KEEP_ALIVE_OFFLOAD); |
| 130 | if (result != WIFI_SUCCESS) { |
| 131 | ALOGE("Failed to create stop keep alive request; result = %d", result); |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA); |
| 136 | |
| 137 | result = request.put_u8(MKEEP_ALIVE_ATTRIBUTE_ID, mIndex); |
| 138 | if (result < 0) { |
| 139 | ALOGE("Failed to put id request; result = %d", result); |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | request.attr_end(data); |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | default: |
| 148 | ALOGE("Unknown wifi keep alive command"); |
| 149 | result = WIFI_ERROR_UNKNOWN; |
| 150 | } |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | int start() { |
| 155 | ALOGD("Start mkeep_alive command"); |
| 156 | WifiRequest request(familyId(), ifaceId()); |
| 157 | int result = createRequest(request); |
| 158 | if (result != WIFI_SUCCESS) { |
| 159 | ALOGE("Failed to create keep alive request; result = %d", result); |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | result = requestResponse(request); |
| 164 | if (result != WIFI_SUCCESS) { |
| 165 | ALOGE("Failed to register keep alive response; result = %d", result); |
| 166 | } |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | virtual int handleResponse(WifiEvent& reply) { |
| 171 | ALOGD("In MKeepAliveCommand::handleResponse"); |
| 172 | |
| 173 | if (reply.get_cmd() != NL80211_CMD_VENDOR) { |
| 174 | ALOGD("Ignoring reply with cmd = %d", reply.get_cmd()); |
| 175 | return NL_SKIP; |
| 176 | } |
| 177 | |
| 178 | switch (mType) { |
| 179 | case START_MKEEP_ALIVE: |
| 180 | case STOP_MKEEP_ALIVE: |
| 181 | break; |
| 182 | |
| 183 | default: |
| 184 | ALOGW("Unknown mkeep_alive command"); |
| 185 | } |
| 186 | return NL_OK; |
| 187 | } |
| 188 | |
| 189 | virtual int handleEvent(WifiEvent& event) { |
| 190 | /* NO events! */ |
| 191 | return NL_SKIP; |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | |
| 196 | /* API to send specified mkeep_alive packet periodically. */ |
| 197 | wifi_error wifi_start_sending_offloaded_packet(wifi_request_id index, wifi_interface_handle iface, |
| 198 | u8 *ip_packet, u16 ip_packet_len, u8 *src_mac_addr, u8 *dst_mac_addr, u32 period_msec) |
| 199 | { |
| 200 | if ((index > 0 && index <= N_AVAIL_ID) && (ip_packet != NULL) && (src_mac_addr != NULL) |
| 201 | && (dst_mac_addr != NULL) && (period_msec > 0) |
| 202 | && (ip_packet_len <= MKEEP_ALIVE_IP_PKT_MAX)) { |
| 203 | MKeepAliveCommand *cmd = new MKeepAliveCommand(iface, index, ip_packet, ip_packet_len, |
| 204 | src_mac_addr, dst_mac_addr, period_msec, START_MKEEP_ALIVE); |
| 205 | wifi_error result = (wifi_error)cmd->start(); |
| 206 | delete cmd; |
| 207 | return result; |
| 208 | } else { |
| 209 | ALOGE("Invalid mkeep_alive parameters"); |
| 210 | return WIFI_ERROR_INVALID_ARGS; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* API to stop sending mkeep_alive packet. */ |
| 215 | wifi_error wifi_stop_sending_offloaded_packet(wifi_request_id index, wifi_interface_handle iface) |
| 216 | { |
| 217 | if (index > 0 && index <= N_AVAIL_ID) { |
| 218 | MKeepAliveCommand *cmd = new MKeepAliveCommand(iface, index, STOP_MKEEP_ALIVE); |
| 219 | wifi_error result = (wifi_error)cmd->start(); |
| 220 | delete cmd; |
| 221 | return result; |
| 222 | } else { |
| 223 | ALOGE("Invalid mkeep_alive parameters"); |
| 224 | return WIFI_ERROR_INVALID_ARGS; |
| 225 | } |
| 226 | } |